WELCOME
Bitwise Operators
21st Century Skills
Agenda
Bitwise Operators
Bitwise AND, OR, NOT XOR, NAND,
Operators Operators NOR Operators
Shift
Applications Practice Problems
Operators
Bitwise Operators
Introduction
Bitwise Operators
Bitwise operators are used to perform operations
on individual bits of integer values. They are
mainly used in low-level programming for
manipulating data at the bit level.
Bitwise Operators
Operators
The important bitwise
The main logical operators
operators are: are:
AND OR XOR NOT
(&) (|) (^) (~)
Left Shift Right Shift
NAND NOR
(<<) (>>)
Bitwise AND, OR, and NOT
Operators
Logical and Bitwise Operators
Bitwise AND (&)
A B Output
→ The AND operation compares each
0 0 0
corresponding bit of two numbers.
0 1 0
→ The result is 1 only if both bits are 1,
1 0 0
otherwise, the result is 0.
1 1 1
Truth Table of AND ( & )
Bitwise AND (&)
Example
Perform AND (&) operation:
Number A (in binary): 1101
Number B (in binary): 1011
Bitwise AND (&)
Example
→ The first bit: 1 & 1 = 1
1 1 0 1
→ The second bit: 0 & 1 = 0
& 1 0 1 1
→ The third bit: 1 & 0 = 0
1 0 0 1 → The fourth bit: 1 & 1 = 1
So, AND operation of 1101 & 1011 is 1001
Introduction
Bitwise OR (|)
A B Output
→ The OR operation compares
0 0 0
each bit of two numbers.
0 1 1
→ The result is 1 if at least one
1 0 1
of the bits is 1.
1 1 1
Truth Table of OR ( | )
Bitwise OR (|)
Example
Perform OR operation:
Number A (in binary): 1101
Number B (in binary): 1011
Bitwise OR (|)
Example
→ The first bit: 1 | 1 = 1
1 1 0 1
→ The second bit: 0 | 1 = 1
| 1 0 1 1
→ The third bit: 1 | 0 = 1
1 1 1 1 → The fourth bit: 1 | 1 = 1
So, OR operation of 1101 & 1011 is 1111
Introduction
Bitwise NOT (~)
→ The NOT operation flips all the
1 0
bits in a number:
A ~A
→ 1 becomes 0, and 0 becomes 1.
0 1
1 0
Bitwise NOT (~)
Example
Perform NOT operation:
→ The first bit: 1 becomes 0
~ 1 1 0 1
→ The second bit: 0 becomes 1
0 0 1 0 → The third bit: 1 becomes 0
→ The fourth bit: 1 becomes 0
So, NOT operation of 1101 is 0010
XOR, NAND, and NOR
Operators
XOR, NAND, and NOR Operators
Bitwise XOR (^)
A B Output
→ The XOR (exclusive OR) operation
0 0 0
compares each bit of two numbers.
0 1 1
→ The result is 1 if the bits are
1 0 1
differentand 0 if the bits are the 1 1 0
same.
Truth Table of XOR ( ^ )
Bitwise XOR (^)
Example
Perform XOR operation:
Number A (in binary): 1101
Number B (in binary): 1011
Bitwise XOR (^)
Example
→ The first bit: 1 ^ 1 = 0
1 1 0 1
→ The second bit: 0 ^ 1 = 1
^ 1 0 1 1
→ The third bit: 1 ^ 0 = 1
0 1 1 0 → The fourth bit: 1 ^ 1 = 0
So, XOR operation of 1101 & 1011 is 0110
XOR, NAND, and NOR Operators
Bitwise NAND
→ The NAND (NOT AND) operation A B A&B A NAND B
is the opposite of the AND 0 0 0 1
operation. 0 1 0 1
→ It gives 1 if the result of the AND
1 0 0 1
operation is 0, and 0 if the result
1 1 1 0
of the AND operation is 1.
Truth Table of NAND
Bitwise NAND
Example
Perform NAND operation:
Number A (in binary): 1101
Number B (in binary): 1011
Bitwise NAND
Example
1 1 0 1
The AND operation
& 1 0 1 1
gives 1001.
~ 1 0 0 1 The NOT of that (~1001)
0 1 1 0 is 0110.
So,NAND operation of 1101 & 1011 is 0110
XOR, NAND, and NOR Operators
Bitwise NOR
→ The NOR (NOT OR) operation is
A B A|B A NOR B
the opposite of the OR
0 0 0 1
operation.
0 1 1 0
→ It gives 1 if the result of the OR 1 0 1 0
operation is 0, and 0 if the result 1 1 1 0
of the OR operation is 1.
Truth Table of NOR
Bitwise NOR
Example
Perform NOR operation:
Number A (in binary): 1101
Number B (in binary): 1011
Bitwise NOR
Example
1 1 0 1 The OR operation gives
| 1 0 1 1 1111.
~ 1 1 1 1 The NOT of that (~1111)
0 0 0 0 is 0000.
So, NOR operation of 1101 & 1011 is 0000
Bitwise Shift
Operators
Bitwise Shift Operators
Left Shift Operator (<<)
Left Shift (<<)
The left shift operation shifts all bits of a Rule:
number to the left by k positions.
x << k ≡ x × (2k)
Each shift is equivalent to multiplying
the number by 2k.
Left Shift (<<)
Example 1
→ Number 5 101 1 0 1
→ Shift: 5 << 2
1 0 1 0 0
Result
2
101 << 2 = 5 * 2 → 20 (decimal)
So, 5 << 2 = 20
Left Shift (<<)
Example 2
→ Number 3 11 1 1
→ Shift: 3 << 3
1 1 0 0 0
Result
3
11 << 3 = 3 * 2 → 24 (decimal)
So, 3 << 3 = 24
Bitwise Shift Operators
Right Shift Operator (>>)
Right Shift (>>)
→ The right shift operation shifts all
Rule:
bits of a number to the right by k
positions. x >> k ≡ x ÷ (2k)
→ Each shift is equivalent to integer
division by 2k.
Right Shift (>>)
Example 1
→ Number 14 1110 0 1 1 1 0
→ Shift: 14 >> 2
0 0 0 1 1 1 0
x x
Result
2
01110 >> 2 = 14 ÷ 2 → 3 (decimal)
So, 14 >> 2 = 3
Right Shift (>>)
Example 2
→ Number 56 111000 1 1 1 0 0 0
→ Shift: 56 >> 3
0 0 0 1 1 1 0 0 0
x x x
Result
3
111000 >> 3 = 56 ÷ 2 → 7 (decimal)
So, 56 >> 3 = 7
Bitwise Shift Operators
Summary
l
ion tion h em atica
Operat escrip Ma t
t
D Effec
Left shift by Multiply by
x << k
k positions 2k
Right shift by Integer divide
x >> k
k positions by 2k
Logical and Bitwise Operators
Applications in Optimization
Checking if a number is even or odd: CHECKING
✓ You can use the AND operation to
Binary of 7: 0111
check if a number is even or odd.
7 & 1 = 0111 & 0001 = 0001
✓ For example, if num & 1 == 0, the
number is even, otherwise, it's odd.
Logical and Bitwise Operators
Applications in Optimization
Swapping two numbers without Step a (binary) b (binary)
using a temporary storage: Start 0101 (5) 0011 (3)
a=a^b 0110 (6) 0011 (3)
→ You can use the XOR operation
to swap two numbers. b=a^b 0110 (6) 0101 (5)
a=a^b 0011 (3) 0101 (5)
Flow of swapping numbers
Logical and Bitwise Operators
Applications in Optimization
6 (Binary: 0110)
Counting the number of set bits (1s) in a
6 (binary 0110)
number: 6 & 1 → 0 → count = 0
6 >> 1 → 3 (binary 0011)
→ You can repeatedly check the least 3 (binary 0011)
3 & 1 → 1 → count = 1
significant bit using the AND
3 >> 1 → 1 (binary 0001)
operation with 1 and then right-shift
1 (binary 0001)
the number to examine the next bit. 1 & 1 → 1 → count = 2
1 >> 1 → 0 (binary 0000)
Practice Problems
Practice Problems
Bitwise AND ( & )
What is the result of 1100 & 1010?
1 1 0 0
& 1 0 1 0
1 0 0 0
Practice Problems
Bitwise OR ( | )
What is the result of 1100 | 1010?
1 1 0 0
| 1 0 1 0
1 1 1 0
Practice Problems
Bitwise NOT ( ~ )
What is the result of ~1010 ?
~ 1 0 1 0
0 1 0 1
Practice Problems
Bitwise XOR (^)
What is the result of 1100 ^ 1010 ?
1 1 0 0
^ 1 0 1 0
0 1 1 0
Practice Problems
Bitwise NAND
What is NAND of 1100 and 1010 ?
1 1 0 0
& 1 0 1 0
~ 1 0 0 0
0 1 1 1
Practice Problems
Bitwise NOR
What is NOR of 1100 and 1010 ?
1 1 0 0
& 1 0 1 0
~ 1 1 1 0
0 0 0 1
Practice Problems
Left Shift (<<)
What is 8 << 3 ?
→ Binary of 8 = 1000
→ Shift left by 3 → 1000000 = 64
Practice Problems
Right Shift (>>)
What is 48 >> 2 ?
→ Binary of 48 = 110000
→ Shift right by 2 → 001100 = 12
Key Takeaways
● Bitwise Operators:
➢ AND (&), OR (|), XOR (^), NOT (~), NAND, NOR,
Left Shift (<<), Right Shift (>>).
● Shift Operators:
➢ Left shift (<<) multiplies by powers of 2, right shift
(>>) divides by powers of 2.
● Applications:
➢ Check even/odd using AND, swap numbers with
XOR, and count set bits using AND and right shift.
THANK YOU
ALL THE BEST