Bitwise and Bit Shift Operators
&
: AND
int a = 12; // binary representation is 1100
int b = 10; // binary representation is 1010
int result = a & b; // result is 1000, which is 8 in decimal
The bitwise AND operator &
performs a bitwise AND operation on two operands. A bit in the result is set to 1 only if both corresponding bits of the operands are 1.
|
: OR
int a = 12; // binary representation is 1100
int b = 10; // binary representation is 1010
int result = a | b; // result is 1110, which is 14 in decimal
The bitwise OR operator |
performs a bitwise OR operation on two operands. A bit in the result is set to 1 if at least one of the corresponding bits of the operands is 1.
^
: XOR
int a = 12; // binary representation is 1100
int b = 10; // binary representation is 1010
int result = a ^ b; // result is 0110, which is 6 in decimal
The bitwise XOR operator ^
performs a bitwise XOR operation between two operands. A bit in the result is set to 1 if the corresponding bits of the operands are different.
~
: NOT
int a = 12; // binary representation is 1100
int result = ~a; // result will be the inverted form, depending on the size of the integer (e.g., 32 bits)
The bitwise NOT operator ~
inverts all the bits of its operand.
<<
: Left Shift Operator
int a = 5; // binary representation is 0101
int result = a << 2; // result is 10100, which is 20 in decimal
The left shift operator <<
shifts the bits of its left-hand operand to the left by the number of positions specified by its right-hand operand. Vacant positions are filled with zeros.
>>
: Signed Right Shift
int a = -20; // negative numbers are represented in two's complement form
int result = a >> 2; // result maintains the sign, giving -5
The signed right shift operator >>
shifts the bits of its left-hand operand to the right by the number of positions specified by its right-hand operand. Vacant positions are filled with the sign bit.
>>>
: Unsigned Right Shift
int a = -20;
int result = a >>> 2; // result is a large number because the sign bit is not propagated
The unsigned right shift operator >>>
shifts the bits of its left-hand operand to the right by the number of positions specified by its right-hand operand. Vacant positions are filled with zeros, regardless of the sign of the initial number.
Operator | Description | Example | Example Description | Result |
---|---|---|---|---|
& |
Bitwise AND | int a = 12; int b = 10; int result = a & b; |
Performs a bitwise AND on each bit of a and b . |
result = 8 |
| |
Bitwise OR | int a = 12; int b = 10; int result = a | b; |
||
^ |
Bitwise XOR | int a = 12; int b = 10; int result = a ^ b; |
Performs a bitwise XOR on each bit of a and b . |
result = 6 |
~ |
Bitwise NOT | int a = 12; int result = ~a; |
Inverts all bits of a . |
result = -13 (depending on integer size) |
<< |
Left Shift | int a = 5; int result = a << 2; |
Shifts bits of a left by 2, filling with 0s. |
result = 20 |
>> |
Signed Right Shift | int a = -20; int result = a >> 2; |
Shifts bits of a right by 2, extending the sign bit. |
result = -5 |
>>> |
Unsigned Right Shift | int a = -20; int result = a >>> 2; |
Shifts bits of a right by 2, filling with 0s. |
result = 1073741819 |
These operators are useful for manipulating data at the lowest level, writing performance-sensitive applications, or engaging in algorithm competitions.
Reference
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html