Bitwise operators in Java

Bitwise operators are used to perform manipulation of individual bits of a number. They can be used with any of the integral types (char, short, int, etc). They are used when performing update and query operations of Binary indexed tree.

  1. Bitwise OR (|) –
    This operator is binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e, if either of the bits is 1, it gives 1, else it gives 0.
    For example,

    a = 5 = 0101 (In Binary)
    b = 7 = 00011001 (In Binary)
    
    Bitwise OR Operation of 5 and 7
      0101
    | 0111
     ________
      0111  = 7 (In decimal) 
  2. Bitwise AND (&) –
    This operator is binary operator, denoted by ‘&’. It returns bit by bit AND of input values, i.e, if both bits are 1, it gives 1, else it gives 0.
    For example,

    a = 5 = 0101 (In Binary)
    b = 7 = 00011001 (In Binary)
    
    Bitwise AND Operation of 5 and 7
      0101
    & 0111
     ________
      0101  = 5 (In decimal) 
  3. Bitwise XOR (^) –
    This operator is binary operator, denoted by ‘^’. It returns bit by bit XOR of input values, i.e, if corresponding bits are different, it gives 1, else it gives 0.
    For example,

    a = 5 = 0101 (In Binary)
    b = 7 = 00011001 (In Binary)
    
    Bitwise XOR Operation of 5 and 7
      0101
    ^ 0111
     ________
      0010  = 2 (In decimal) 
  4. Bitwise Complement (~) –
    This operator is unary operator, denoted by ‘~’. It returns the one’s compliment representation of the input value, i.e, with all bits inversed, means it makes every 0 to 1, and every 1 to 0.
    For example,



    a = 5 = 0101 (In Binary)
    b = 7 = 00011001 (In Binary)
    
    Bitwise Compliment Operation of 5 and 7
    
    ~ 0101
     ________
      1010  = 10 (In decimal) 

    Note – Compiler will give 2’s complement of that number, i.e., 2’s compliment of 10 will be -6.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// bitwise operators
public class operators {
    public static void main(String[] args)
    {
        //hexadecimal representation 
        int a = 0x0005;
        int b = 0x0007;
  
        // bitwise and
        // 0101 & 0111=0101 = 5
        System.out.println("a&b = " + (a & b));
  
        // bitwise or
        // 0101 | 0111=0111 = 7
        System.out.println("a|b = " + (a | b));
  
        // bitwise xor
        // 0101 ^ 0111=0010 = 2
        System.out.println("a^b = " + (a ^ b));
  
        // bitwise and
        // ~0101=1010
        // will give 2's complement of 1010 = -6
        System.out.println("~a = " + ~a);
  
        // can also be combined with
        // assignment operator to provide shorthand
        // assignment
        // a=a&b
        a &= b;
        System.out.println("a= " + a);
    }
}

chevron_right


Output :

a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5 

Shift Operators: These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. They can be used when we have to multiply or divide a number by two. General format:

 number shift_op number_of_places_to_shift;
  1. Left shift operator (<<) –
    Shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.
    For example,

    a = 5 = 0000 0101
    b = -10 = 1111 1010
    
    a << 1 = 0000 1010 = 10
    a << 2 = 0001 0100 = 20 
  2. Unsigned Left shift operator (<<<) -
    There is no "<<<" operator in Java, because the logical (<<) and arithmetic left-shift (<<<) operations are identical.
  3. Signed Right shift operator (>>) -
    Shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of initial number. Similar effect as of dividing the number with some power of two.
    For example,

    a = 5 = 0000 0101
    b = 10 = 0000 1010 (takes 2's compliment)
    b = -10 = 1111 0110
    
    b >> 1 = 1111 1011 
    b >> 2 = 1111 1101 (takes 2's compliment) = -3 
  4. Unsigned Right shift operator (>>>) -
    Shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit is set to 0. (>>>) is unsigned-shift; it'll insert 0. (>>) is signed, and will extend the sign bit.
    For example,

    a = 5 = 0000 0101
    b = 10 = 0000 1010 (takes 2's compliment)
    b = -10 = 1111 0110
    
    b >> 1 = 1
    b >> 2 = (takes 2's compliment) = 1073741821 
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// shift operators
public class operators {
    public static void main(String[] args)
    {
  
        int a = 0x0005;
        int b = -10;
  
        // left shift operator
        // 0000 0101<<2 =0001 0100(20)
        // similar to 5*(2^2)
        System.out.println("a<<2 = " + (a << 2));
  
        // right shift operator
        // 0000 0101 >> 2 =0000 0001(1)
        // similar to 5/(2^2)
        System.out.println("b>>2 = " + (b >> 2));
  
        // unsigned right shift operator
        System.out.println("b>>>2 = " + (b >>> 2));
    }
}

chevron_right


Output :

a<<2 = 20
b>>2 = -3
b>>>2 = 1073741821

Refer for - other Operators in Java



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.