Bitwise operators in Java work directly on the binary representation of integers. They are mainly used for low-level programming, performance optimization, and manipulating individual bits.
int and long data typesJava provides six main bitwise operators:
& Bitwise AND| Bitwise OR^ Bitwise XOR~ Bitwise NOT<< Left Shift>> Right Shift
// Demonstrating Java bitwise operators
int a = 5;
int b = 3;
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
System.out.println(~a);
System.out.println(a << 1);
System.out.println(a >> 1);
a = 5 → 0101
b = 3 → 0011
a & b → 1 (0001)a | b → 7 (0111)a ^ b → 6 (0110)~a → -6 (two’s complement)a << 1 → 10 (left shift)a >> 1 → 2 (right shift)Change the numbers to see the binary logic update instantly.