Bit Manipulation· 15 min
Bitwise Operations
AND, OR, XOR and shifts — one bit column at a time
Setup
Step 1 / 10
Current step
1 / 10AND (45 & 27): compare the numbers column by column — 1 only when BOTH bits are 1.
Step log
1286432168421
a = 4545
0
0
1
0
1
1
0
1
b = 2727
0
0
0
1
1
0
1
1
a & b
·
·
·
·
·
·
·
·
AND: 1 only when BOTH bits are 1
Bit being computed
Input bit = 1
Result bit = 1
bitwise.java
1public class Main {2 public static void demonstrateBitwiseOperations(int a, int b) {3 System.out.println("AND: " + (a & b));4 System.out.println("OR: " + (a | b));5 System.out.println("XOR: " + (a ^ b));6 System.out.println("Left shift: " + (a << 1));7 System.out.println("Right shift: " + (a >> 1));8 }9 10}● Java
Time
O(1)
a (binary)
00101101
b (binary)
00011011
Result
…
