1.13 Bit Manipulation in C Programming
Module 1.13 • Bitwise Logic Operations, Masking Formulas & Low-Level Data Modification
1.13.1 Introduction
Computers store all information using binary digits (bits).
A bit can have only two values:
- 0
- 1
Normally, programmers work with bytes, integers, characters, and other data types. However, some applications require direct control over individual bits. This process is called Bit Manipulation.
Bit manipulation is widely used in:
- Embedded systems
- Device drivers
- Operating systems
- Networking software
- Cryptography
- Real-time applications
1.13.2 What is a Bit?
A bit is the smallest unit of data.
Example:
Decimal Number = 45
Binary Representation
00101101
Each position represents a power of 2.
Bit Position
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
1.13.3 Why Use Bit Manipulation?
Advantages:
- Faster execution
- Reduced memory usage
- Efficient flag handling
- Better hardware control
- Useful in low-level programming
1.13.4 Bitwise Operators in C
| Operator | Name |
|---|---|
& | AND |
| | OR |
^ | XOR |
~ | NOT (Complement) |
<< | Left Shift |
>> | Right Shift |
1.13.5 Bitwise AND Operator (&)
The AND operator returns 1 only when both bits are 1.
Truth Table
| A | B | A&B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Example
12 = 1100
10 = 1010
12 & 10
Calculation:
1100
1010
----
1000
Result:
8
1.13.6 AND Program
#include <stdio.h>
int main()
{
int a = 12;
int b = 10;
printf("%d", a & b);
return 0;
}
Output:
8
1.13.7 Bitwise OR Operator (|)
Returns 1 if at least one bit is 1.
Truth Table
| A | B | A|B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Example
12 = 1100
10 = 1010
1100
1010
----
1110
Result:
14
1.13.8 OR Program
#include <stdio.h>
int main()
{
int x = 12;
int y = 10;
printf("%d", x | y);
return 0;
}
Output:
14
1.13.9 Bitwise XOR Operator (^)
Returns 1 when bits are different.
Truth Table
| A | B | A^B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example
12 = 1100
10 = 1010
1100
1010
----
0110
Result:
6
1.13.10 XOR Program
#include <stdio.h>
int main()
{
int a = 12;
int b = 10;
printf("%d", a ^ b);
return 0;
}
Output:
6
1.13.11 Bitwise NOT Operator (~)
NOT reverses every bit.
0 → 1
1 → 0
Example:
7 = 00000111
After NOT:
11111000
Result (2's complement):
-8
1.13.12 NOT Program
#include <stdio.h>
int main()
{
int num = 7;
printf("%d", ~num);
return 0;
}
Output:
-8
1.13.13 Left Shift Operator (<<)
Moves bits toward the left. Each shift multiplies the number by 2.
Example
9 = 00001001
9 << 1
Result:
00010010
Decimal:
18
1.13.14 Left Shift Program
#include <stdio.h>
int main()
{
int value = 9;
printf("%d", value << 1);
return 0;
}
Output:
18
1.13.15 Right Shift Operator (>>)
Moves bits toward the right. Each shift approximately divides by 2.
Example
20 = 00010100
20 >> 2
Result:
00000101
Decimal:
5
1.13.16 Right Shift Program
#include <stdio.h>
int main()
{
int num = 20;
printf("%d", num >> 2);
return 0;
}
Output:
5
1.13.17 Multiplication Using Shift
Number × 2ⁿ can be performed as number << n
Example:
11 << 3
Calculation:
11 × 8 = 88
Result:
88
1.13.18 Division Using Shift
80 >> 4
Calculation:
80 ÷ 16
Result:
5
1.13.19 Checking Even or Odd
The least significant bit determines parity.
Even → Last bit = 0
Odd → Last bit = 1
1.13.20 Program to Check Even/Odd
#include <stdio.h>
int main()
{
int num = 37;
if(num & 1)
printf("Odd");
else
printf("Even");
return 0;
}
Output:
Odd
1.13.21 Understanding Bit Positions
Bit Position
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|
Position numbering begins from the right.
1.13.22 Creating a Mask
A mask helps manipulate a specific bit.
Formula:
mask = 1 << position;
Example:
mask = 1 << 4;
Result:
00010000
1.13.23 Testing a Bit
Check whether a bit is ON or OFF.
if(number & mask)
Example:
int number = 26;
int mask = 1 << 3;
1.13.24 Program to Test a Bit
#include <stdio.h>
int main()
{
int number = 26;
int position = 3;
if(number & (1 << position))
printf("Bit is ON");
else
printf("Bit is OFF");
return 0;
}
Output:
Bit is ON
1.13.25 Setting a Bit
Setting means turning a bit ON.
Formula:
number |= mask;
Example
Number = 18
00010010
Set Bit 0:
00010011
Result:
19
1.13.26 Program to Set a Bit
#include <stdio.h>
int main()
{
int number = 18;
number |= (1 << 0);
printf("%d", number);
return 0;
}
Output:
19
1.13.27 Clearing a Bit
Clearing means turning a bit OFF.
Formula:
number &= ~(1 << position);
1.13.28 Example
22 = 10110
Clear bit 2:
10010
Result:
18
1.13.29 Program to Clear a Bit
#include <stdio.h>
int main()
{
int number = 22;
number &= ~(1 << 2);
printf("%d", number);
return 0;
}
Output:
18
1.13.30 Toggling a Bit
Toggle means:
0 → 1
1 → 0
Formula:
number ^= (1 << position);
1.13.31 Toggle Example
20 = 10100
Toggle bit 1:
10110
Result:
22
1.13.32 Program to Toggle a Bit
#include <stdio.h>
int main()
{
int number = 20;
number ^= (1 << 1);
printf("%d", number);
return 0;
}
Output:
22
1.13.33 Extracting a Bit
Formula:
(number >> position) & 1
Example
int value = 45;
int bit = (value >> 2) & 1;
Result:
1
1.13.34 Swapping Numbers Using XOR
No temporary variable required.
a ^= b;
b ^= a;
a ^= b;
1.13.35 Program to Swap Numbers
#include <stdio.h>
int main()
{
int a = 14;
int b = 25;
a ^= b;
b ^= a;
a ^= b;
printf("%d %d", a, b);
return 0;
}
Output:
25 14
1.13.36 Counting Set Bits
Set bit means a bit whose value is 1.
Example:
29 = 11101
Set Bits:
4
1.13.37 Program to Count Set Bits
#include <stdio.h>
int main()
{
int num = 29;
int count = 0;
while(num)
{
num &= (num - 1);
count++;
}
printf("%d", count);
return 0;
}
Output:
4
1.13.38 Binary Flag Storage
One integer can store many flags.
Example:
- Bit 0 → Read Permission
- Bit 1 → Write Permission
- Bit 2 → Execute Permission
- Bit 3 → Hidden File
1.13.39 Defining Flags
#define READ 0x01
#define WRITE 0x02
#define EXECUTE 0x04
#define HIDDEN 0x08
1.13.40 Enabling Flags
flags |= READ;
flags |= WRITE;
1.13.41 Disabling Flags
flags &= ~WRITE;
1.13.42 Checking Flags
if(flags & READ)
{
printf("Read Enabled");
}
1.13.43 Binary Pattern Display Logic
To print all bits:
for(i=15;i>=0;i--)
{
printf("%d", (num>>i)&1);
}
1.13.44 Applications of Bit Manipulation
Used in:
- Embedded controllers
- Device drivers
- Communication protocols
- Compression algorithms
- Encryption systems
- Network packet processing
1.13.45 Advantages
- Very fast execution
- Efficient memory usage
- Low-level hardware access
- Useful for flag management
- Improves performance
Summary
- Bit manipulation operates directly on binary digits.
- C provides six bitwise operators.
- AND tests and clears bits.
- OR sets bits.
- XOR toggles bits.
- NOT complements bits.
- Left shift multiplies by powers of 2.
- Right shift divides by powers of 2.
- Masks help manipulate specific bits.
- Bit manipulation is widely used in systems and embedded programming.
Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.