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
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 to1if both bits are1, otherwise, it is0.5 & 3results in1(0101 & 0011 = 0001).Bitwise OR (
|):|) compares corresponding bits of two integers. The result is a bit set to1if at least one of the bits is1.5 | 3results in7(0101 | 0011 = 0111).Bitwise XOR (
^):^) compares corresponding bits of two integers. The result is a bit set to1if exactly one of the bits is1, but not both.5 ^ 3results 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 by2to the power of the shift count.5 << 1results 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 by2to the power of the shift count.5 >> 1results 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 >>> 1results in2147483645(11111111111111111111111111111011 >>> 1 = 01111111111111111111111111111101in binary, which equals2147483645in decimal).These bitwise operations are useful for low-level programming, bit manipulation, and optimization in scenarios where performance is critical.