·  JAVA – Syntax – Operators – Bitwise

 

- Info:

  - Bitwise Operators operate on individual bits of int and long values: & | ^ ~ << >> >>>.

    If an operand is shorter than an int, it is promoted to int before doing the operations.

 

- Procedure:

  - Create "JAVA Console Application" Test.java.

 

    Test.java

    public class Test {

   

      public static void main(String[] args) { 

        

        //BITWISE OPERATORS.--------------------------------------------------------------------------

        int left        = Integer.parseInt ("11011", 2);

        int right       = Integer.parseInt ("10010", 2);

                                          

        int and         =  left & right; //10010.    1 if both   bits are 1.

        int or          =  left | right; //11011.    1 if either bit  is  1.

        int xor         =  left ^ right; //01001.    1 if        bits are different.

        int invert      = ~left;         //00100.    Invert bits.

        int shiftLeft   =  left <<  3;   //11011000. Shift bits to the left  by 3. Fill with 0.

        int shiftRight  =  left >>  2;   //110.      Shift bits to the right by 2. Fill with 0.

        int shiftRight2 =  left >>> 2;   //????

        

        System.out.println ( Integer.toBinaryString (and        ) );

        System.out.println ( Integer.toBinaryString (or         ) );

        System.out.println ( Integer.toBinaryString (xor        ) );

        System.out.println ( Integer.toBinaryString (invert     ) );

        System.out.println ( Integer.toBinaryString (shiftLeft  ) );

        System.out.println ( Integer.toBinaryString (shiftRight ) );

        System.out.println ( Integer.toBinaryString (shiftRight2) );

        

      }

   

    }