Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer
Bitwise operations in Java are used to manipulate individual bits of integers.
Here's a brief overview of each operation:
Bitwise AND (&):
The bitwise AND operator (&) compares corresponding bits of two integers. The result is a bit set to 1 if both bits are 1, otherwise, it is 0.
Example: 5 & 3 results in 1 (0101 & 0011 = 0001).
Bitwise OR (|):
The bitwise OR operator (|) compares corresponding bits of two integers. The result is a bit set to 1 if at least one of the bits is 1.
Example: 5 | 3 results in 7 (0101 | 0011 = 0111).
Bitwise XOR (^):
The bitwise XOR operator (^) compares corresponding bits of two integers. The result is a bit set to 1 if exactly one of the bits is 1, but not both.
Example: 5 ^ 3 results in 6 (0101 ^ 0011 = 0110).
Left Shift (<<):
The left shift operator (<<) shifts the bits of the left-hand operand to the left by several positions specified by the right-hand operand. This operation effectively multiplies the number by 2 to the power of the shift count.
Example: 5 << 1 results in 10 (0101 << 1 = 1010).
Right Shift (>>):
The right shift operator (>>) shifts the bits of the left-hand operand to the right by several positions specified by the right-hand operand. For positive numbers, this operation effectively divides the number by 2 to the power of the shift count.
Example: 5 >> 1 results in 2 (0101 >> 1 = 0010).
Unsigned Right Shift (>>>):
The unsigned right shift operator (>>>) shifts zero-filled bits to the right. Regardless of the sign bit, the result of shifting is always non-negative.
Example: -5 >>> 1 results in 2147483645 (11111111111111111111111111111011 >>> 1 = 01111111111111111111111111111101 in binary, which equals 2147483645 in decimal).
These bitwise operations are useful for low-level programming, bit manipulation, and optimization in scenarios where performance is critical.
Liked By
Write Answer
Bitwise AND, OR, XOR, and left/right shift operations.
Join MindStick Community
You have need login or register for voting of answers or question.
Ravi Vishwakarma
17-Jul-2024Bitwise operations in Java are used to manipulate individual bits of integers.
Here's a brief overview of each operation:
Bitwise AND (
&
):&
) compares corresponding bits of two integers. The result is a bit set to1
if both bits are1
, otherwise, it is0
.5 & 3
results in1
(0101 & 0011 = 0001
).Bitwise OR (
|
):|
) compares corresponding bits of two integers. The result is a bit set to1
if at least one of the bits is1
.5 | 3
results in7
(0101 | 0011 = 0111
).Bitwise XOR (
^
):^
) compares corresponding bits of two integers. The result is a bit set to1
if exactly one of the bits is1
, but not both.5 ^ 3
results in6
(0101 ^ 0011 = 0110
).Left Shift (
<<
):<<
) shifts the bits of the left-hand operand to the left by several positions specified by the right-hand operand. This operation effectively multiplies the number by2
to the power of the shift count.5 << 1
results in10
(0101 << 1 = 1010
).Right Shift (
>>
):>>
) shifts the bits of the left-hand operand to the right by several positions specified by the right-hand operand. For positive numbers, this operation effectively divides the number by2
to the power of the shift count.5 >> 1
results in2
(0101 >> 1 = 0010
).Unsigned Right Shift (
>>>
):>>>
) shifts zero-filled bits to the right. Regardless of the sign bit, the result of shifting is always non-negative.-5 >>> 1
results in2147483645
(11111111111111111111111111111011 >>> 1 = 01111111111111111111111111111101
in binary, which equals2147483645
in decimal).These bitwise operations are useful for low-level programming, bit manipulation, and optimization in scenarios where performance is critical.