Skip to main content
C Programming Basics
CHAPTER 25 Beginner

Bitwise Programming

Updated: May 17, 2026
5 min read

# 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 number 5 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.

c
12345
// 5 = 0101
// 3 = 0011
// ---------
// & = 0001  (which is 1)
int result = 5 & 3; // result is 1

#### 2. Bitwise OR (|) Returns 1 if AT LEAST ONE bit is 1.

c
12345
// 5 = 0101
// 3 = 0011
// ---------
// | = 0111  (which is 7)
int result = 5 | 3; // result is 7

#### 3. Bitwise XOR (^) - Exclusive OR Returns 1 if the bits are DIFFERENT. Returns 0 if they are the SAME.

c
12345
// 5 = 0101
// 3 = 0011
// ---------
// ^ = 0110  (which is 6)
int result = 5 ^ 3; // result is 6

*(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.

c
123
// 5 = 0000 0101
// ~5 = 1111 1010  (which is -6 in 2's complement representation)
int result = ~5; 

5. Shift Operators

#### Left Shift (<<) Shifts bits to the left, adding 0s to the right. This is mathematically equivalent to multiplying by 2.
c
123
// 5 = 0000 0101
// 5 << 1 = 0000 1010 (which is 10)
int result = 5 << 1; 

#### Right Shift (>>) Shifts bits to the right, discarding the rightmost bits. This is mathematically equivalent to dividing by 2.

c
123
// 10 = 0000 1010
// 10 >> 1 = 0000 0101 (which is 5)
int result = 10 >> 1; 

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. 1. Write a program to check if a number is Even or Odd using the Bitwise AND operator. (Hint: num & 1).
  1. 2. Write a program to multiply a number by 4 using the Left Shift operator.
  1. 3. Write a program to swap two variables using the XOR (^) operator.

9. MCQ Quiz with Answers

Question 1

Which operator is used for Bitwise AND?

# Answer: b) &
Question 2

What is the binary representation of 4?

Question 3

10 << 1 (Left shifting 10 by 1) results in:

Question 4

Which operator is used to toggle (flip) a bit?

Question 5

What is the difference between & and &&?

Question 6

To quickly divide a positive integer by 2, you can use:

Question 7

Which operator flips all 1s to 0s and 0s to 1s?

Question 8

How do you set the 1st bit (index 1) of a variable x to 1?

Question 9

Bitwise operators can only operate on:

Question 10

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.

12. Next Chapter Recommendation

In Chapter 26: Function Pointers, we will learn how to point a pointer not at a variable, but at executable code (a function), enabling powerful callback architectures.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·