Bitwise Programming
# CHAPTER 25
Bitwise Programming
1. Introduction
While normal arithmetic operates on integers (5 + 3), bitwise operators operate on the underlying binary bits (00000101 + 00000011). Because these operations happen directly inside the CPU's Arithmetic Logic Unit (ALU), they are incredibly fast. Bitwise programming is the backbone of embedded systems, cryptography, and network programming.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand binary representations of numbers.
-
Use Bitwise AND (
&), OR (|), XOR (^), and NOT (~).
-
Use Left Shift (
<<) and Right Shift (>>).
- Understand Bit Masking for setting, clearing, and toggling bits.
3. Binary Refresher
In an 8-bit system, the number5 is represented as 0000 0101.
The number 3 is represented as 0000 0011.
4. Bitwise Operators
#### 1. Bitwise AND (&)
Compares bits. Returns 1 if BOTH bits are 1. Otherwise, returns 0.
#### 2. Bitwise OR (|)
Returns 1 if AT LEAST ONE bit is 1.
#### 3. Bitwise XOR (^) - Exclusive OR
Returns 1 if the bits are DIFFERENT. Returns 0 if they are the SAME.
*(Fun Fact: XOR is used heavily in basic cryptography and for swapping two variables without a temporary variable!)*
#### 4. Bitwise NOT (~) - One's Complement
Flips all 1s to 0s, and 0s to 1s.
5. Shift Operators
#### Left Shift (<<)
Shifts bits to the left, adding 0s to the right. This is mathematically equivalent to multiplying by 2.
#### Right Shift (>>)
Shifts bits to the right, discarding the rightmost bits. This is mathematically equivalent to dividing by 2.
6. Bit Masking
In embedded systems (like programming an Arduino or Raspberry Pi in C), you often need to turn a specific LED on or off without affecting the others. This is done via Bit Masking.Assuming PORTA = 0000 0000:
-
Setting a bit (turning it on): Use OR (
|)
PORTA = PORTA | (1 << 3); // Turns on the 3rd bit -> 0000 1000
-
Clearing a bit (turning it off): Use AND with NOT (
& ~)
PORTA = PORTA & ~(1 << 3); // Turns off the 3rd bit -> 0000 0000
-
Toggling a bit (flipping it): Use XOR (
^)
PORTA = PORTA ^ (1 << 3); // If it was 0, it becomes 1. If 1, becomes 0.
7. Common Mistakes
-
Confusing Logical AND (
&&) with Bitwise AND (&):
5 && 3 evaluates truthiness (True AND True = 1).
5 & 3 evaluates bits (0101 & 0011 = 0001 = 1).
- Shifting negative numbers: Right shifting a negative integer invokes implementation-defined behavior in C (arithmetic vs logical shift).
8. Exercises
-
1.
Write a program to check if a number is Even or Odd using the Bitwise AND operator. (Hint:
num & 1).
- 2. Write a program to multiply a number by 4 using the Left Shift operator.
-
3.
Write a program to swap two variables using the XOR (
^) operator.
9. MCQ Quiz with Answers
Which operator is used for Bitwise AND?
What is the binary representation of 4?
10 << 1 (Left shifting 10 by 1) results in:
Which operator is used to toggle (flip) a bit?
What is the difference between & and &&?
To quickly divide a positive integer by 2, you can use:
Which operator flips all 1s to 0s and 0s to 1s?
How do you set the 1st bit (index 1) of a variable x to 1?
Bitwise operators can only operate on:
What does (x & 1) evaluate to if x is an even number?
10. Interview Questions
- Q: Write a C program to count the number of set bits (1s) in an integer using bitwise operators.
-
Q: How do you check if a number is a power of 2 using bitwise operators? (Hint:
(n & (n-1)) == 0).
-
Q: Explain why left shift (
<<) multiplies a number by 2.
11. Summary
Bitwise programming manipulates data at the lowest level.& (AND) is used for clearing bits, | (OR) for setting bits, and ^ (XOR) for toggling bits. Shift operators provide extremely fast multiplication and division by powers of 2.