# Bitwise operators in Java

#### Bitwise operators in Java

Bitwise operators are used to performing 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.

Bitwise OR (|) –
This operator is a 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 = 0111 (In Binary)

Bitwise OR Operation of 5 and 7
0101
| 0111
________
0111 = 7 (In decimal)
Bitwise AND (&) –
This operator is a 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 = 0111 (In Binary)

Bitwise AND Operation of 5 and 7
0101
& 0111
________
0101 = 5 (In decimal)
Bitwise XOR (^) –
This operator is a 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 = 0111 (In Binary)

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

Bitwise Compliment Operation of 5

~ 0101
________
1010 = 10 (In decimal)
Note – Compiler will give 2’s complement of that number, i.e., 2’s complement of 10 will be -6.

// Java program to illustrate
// bitwise operators
public class operators {
public static void main(String[] args)
{
// Initial values
int a = 5;
int b = 7;

// 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 not
// ~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);
}
}
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;
Signed Right shift operator (>>) –
Shifts the bits of the number to the right and fills the voids left with the sign bit (1 in case of negative number and 0 in case of positive number). The leftmost bit and a depends on the sign of initial number. Similar effect as of dividing the number with some power of two.
For example,
Example 1:
a = 10
a>>1 = 5

Example 2:
a = -10
a>>1 = -5
We preserve the sign bit.
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,
Example 1:
a = 10
a>>>1 = 5

Example 2:
a = -10
a>>>1 = 2147483643
DOES NOT preserve the sign bit.
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 0110

a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20

b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40
Unsigned Left shift operator (<<<) –
Unlike unsigned Right Shift, there is no “<<<” operator in Java, because the logical (<<) and arithmetic left-shift (<<<) operations are identical.

// Java program to illustrate
// shift operators
public class operators {
public static void main(String[] args)
{

int a = 5;
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));
}
}
Output
a<<2 = 20
b>>2 = -3
b>>>2 = 1073741821