The Magic of Bitwise operators in Java. Know the secrets.

5-The Magic of Bitwise operators in Java. Know the secrets.

๐Ÿง  What Are Bitwise Operators?

๐Ÿ“Œ Definition

Bitwise operators perform operations bit by bit on integer data types (byte, short, int, long).

Java internally represents integers in binary format, and bitwise operators directly manipulate those binary values.


๐Ÿ“Š List of Bitwise Operators in Java

OperatorName
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT (Complement)
<<Left Shift
>>Right Shift
>>>Unsigned Right Shift

๐Ÿงฉ Binary Representation (Very Important)

๐Ÿ“Œ How Numbers Are Stored

Example:

5  โ†’  00000101
3  โ†’  00000011

Bitwise operators compare each corresponding bit.


๐Ÿ”— 1. Bitwise AND (&)

๐Ÿ“Œ Working Rule
  • Result bit is 1 only if both bits are 1
  • Otherwise result is 0
๐Ÿงช Example
int a = 5;   // 0101
int b = 3;   // 0011
System.out.println(a & b);
๐Ÿง  Binary Calculation
0101
0011
----
0001  โ†’ 1

โœ” Output:

1

๐Ÿ“Œ Practical Use
  • Checking odd/even numbers
if ((n & 1) == 0)
    System.out.println("Even");

๐Ÿ”€ 2. Bitwise OR (|)

๐Ÿ“Œ Working Rule
  • Result bit is 1 if at least one bit is 1
๐Ÿงช Example
int a = 5;
int b = 3;
System.out.println(a | b);
๐Ÿง  Binary Calculation
0101
0011
----
0111 โ†’ 7

โœ” Output:

7

๐Ÿ” 3. Bitwise XOR (^)

๐Ÿ“Œ Working Rule
  • Result bit is 1 if bits are different
  • Result is 0 if bits are same
๐Ÿงช Example
int a = 5;
int b = 3;
System.out.println(a ^ b);
๐Ÿง  Binary Calculation
0101
0011
----
0110 โ†’ 6

โœ” Output:

6

โญ Important Property of XOR
x ^ x = 0
x ^ 0 = x

๐Ÿ“Œ Swapping Without Temp Variable
int x = 5, y = 7;
x = x ^ y;
y = x ^ y;
x = x ^ y;

๐Ÿ”„ 4. Bitwise NOT (~)

๐Ÿ“Œ Working Rule
  • Converts 1 to 0
  • Converts 0 to 1
๐Ÿงช Example
int a = 5;
System.out.println(~a);
๐Ÿง  Binary Explanation (Twoโ€™s Complement)
5  โ†’ 00000101
~5 โ†’ 11111010

This equals:

-6

โœ” Output:

-6

โš ๏ธ Common Confusion

Many expect ~5 = -5, but the correct result is -6.


โฌ…๏ธ 5. Left Shift Operator (<<)

๐Ÿ“Œ Working Rule
  • Shifts bits to the left
  • Rightmost bits are filled with 0
  • Equivalent to multiplying by 2โฟ
๐Ÿงช Example
int a = 5;
System.out.println(a << 1);
๐Ÿง  Binary Calculation
0101 << 1 โ†’ 1010 โ†’ 10

โœ” Output:

10

๐Ÿ“Œ Mathematical Relation
a << n  =  a ร— (2โฟ)

โžก๏ธ 6. Right Shift Operator (>>)

๐Ÿ“Œ Working Rule
  • Shifts bits to the right
  • Leftmost bit is filled with sign bit
  • Equivalent to dividing by 2โฟ
๐Ÿงช Example
int a = 10;
System.out.println(a >> 1);

โœ” Output:

5

๐Ÿ“Œ With Negative Numbers
int a = -10;
System.out.println(a >> 1);

Sign bit is preserved.


โžก๏ธโžก๏ธ 7. Unsigned Right Shift (>>>)

๐Ÿ“Œ Working Rule
  • Shifts bits right
  • Always fills leftmost bits with 0
  • Ignores sign bit
๐Ÿงช Example
int a = -10;
System.out.println(a >>> 1);

โœ” Produces a large positive number


๐Ÿง  Adding Two Numbers Using Bitwise Operators

๐Ÿ“Œ Logic Behind Addition
  • XOR (^) โ†’ sum without carry
  • AND (&) โ†’ carry
  • Left shift carry

๐Ÿงช Program: Add Two Numbers Without +
int a = 5;
int b = 7;

while (b != 0) {
    int carry = a & b;
    a = a ^ b;
    b = carry << 1;
}

System.out.println(a);

โœ” Output:

12

๐Ÿง  Why This Works
  • XOR adds bits
  • AND finds carry
  • Left shift moves carry to correct position
  • Repeats until no carry remains

This is how processors internally perform addition.


โš ๏ธ Common Mistakes with Bitwise Operators

โŒ Confusing Logical and Bitwise Operators
if (a && b)   // ERROR

โœ” Correct:

if ((a & b) != 0)

โŒ Expecting Boolean Output

Bitwise operators return integers, not boolean values.


โŒ Using Bitwise Operators on Floating Types
double x = 5.0;
x & 3;   // ERROR

Bitwise operators work only on integer types.


โŒ Misunderstanding ~

~x โ‰  -x

Correct formula:

~x = -(x + 1)

๐Ÿ“Š Summary of Bitwise Operators

  • & โ†’ AND
  • | โ†’ OR
  • ^ โ†’ XOR
  • ~ โ†’ Complement
  • << โ†’ Multiply by powers of 2
  • >> โ†’ Divide by powers of 2
  • >>> โ†’ Unsigned shift

๐ŸŽฏ Why Bitwise Operators Are Required

โญ Key Reasons
  • Faster execution
  • Low-level memory control
  • Efficient calculations
  • Used in cryptography, compression, OS design
  • Common in competitive programming

๐Ÿ Conclusion

๐Ÿ“ Final Summary

Bitwise operators allow Java programs to work directly at the binary level, providing powerful and efficient ways to manipulate data. While they may seem complex at first, understanding their binary behavior makes them predictable and extremely useful. A strong grasp of bitwise operators not only improves programming skills but also deepens understanding of how computers work internally

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *