Bit Manipulation· 15 min

Bitwise Operations

AND, OR, XOR and shifts — one bit column at a time

Setup

Step 1 / 10

Current step

1 / 10

AND (45 & 27): compare the numbers column by column — 1 only when BOTH bits are 1.

Step log

1286432168421
a = 45
0
0
1
0
1
1
0
1
45
b = 27
0
0
0
1
1
0
1
1
27
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
…