Monday, August 8, 2011

JAVA CODE FOR LEFT SHIFT AND RIGHT SHIFT AND BITWISE OPERATOR


public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] rk) {
       
        // range of int -2,147,483,648 to 2,147,483,647
        // int is 32bit so below i have taken 32bits
        int a=10; // a= 00000000 00000000 00000000 00001010 = 10 [ten]
        a=a<<2;  // left shift operator
        //now a=00000000 00000000 00000000 00101000 = 40 [fourty]
        System.out.println("a="+a);
        // same way right shift operator
       
        int b=2; // ..... 00000010
        int c=2; // ..... 00000010
        int d= c& b; //...00000010      
        System.out.println("[logican AND] c ="+c);
        d=c|b;
        System.out.println("[logical OR]  c ="+c);

        // same way change value of b=1 and c=2 then you could uderstand how it works bit by bit.
    }

}

No comments:

Post a Comment