Every integer in memory is a row of binary digits, and C’s bitwise operators let you read and change those digits one position at a time. They are how C programs pack several on/off settings into one number, talk to hardware registers, compress data and perform some calculations very quickly. This lesson shows each of the six operators on real bit patterns and then the everyday tricks built from them: setting, clearing, toggling and testing bits.
In this lesson you will learn
- The six bitwise operators:
&,|,^,~,<<,>> - How to print a number in binary
- Bit masks and flags: set, clear, toggle and test a bit
- Fast tricks: odd/even, multiply and divide by powers of two
- Counting set bits and checking for a power of two
You should know the basic operators in C and data types.
The Six Operators
#include <stdio.h>
static void print_bits(unsigned char x) {
for (int i = 7; i >= 0; i--)
putchar((x >> i) & 1 ? '1' : '0');
}
int main(void) {
unsigned char a = 12, b = 10; /* 00001100 and 00001010 */
printf("a = "); print_bits(a); printf(" (%u)\n", a);
printf("b = "); print_bits(b); printf(" (%u)\n", b);
printf("a & b = "); print_bits(a & b); printf(" (%u)\n", a & b);
printf("a | b = "); print_bits(a | b); printf(" (%u)\n", a | b);
printf("a ^ b = "); print_bits(a ^ b); printf(" (%u)\n", a ^ b);
printf("~a = "); print_bits((unsigned char)~a); printf(" (%u)\n", (unsigned char)~a);
printf("a << 2 = "); print_bits((unsigned char)(a << 2)); printf(" (%u)\n", (unsigned char)(a << 2));
printf("a >> 2 = "); print_bits(a >> 2); printf(" (%u)\n", a >> 2);
return 0;
}
Output
a = 00001100 (12)
b = 00001010 (10)
a & b = 00001000 (8)
a | b = 00001110 (14)
a ^ b = 00000110 (6)
~a = 11110011 (243)
a << 2 = 00110000 (48)
a >> 2 = 00000011 (3)
print_bitsshifts each bit to the right end withx >> iand keeps only that bit with& 1.- AND
&gives 1 only where both bits are 1: 1100 & 1010 = 1000. - OR
|gives 1 where either bit is 1: 1110. - XOR
^gives 1 where the bits differ: 0110. - NOT
~flips every bit. The result is cast back tounsigned charbecause~first promotes the value toint. - Left shift
<< nmoves bits left and fills with zeros, multiplying by 2n. Right shift>> nmoves them right, dividing by 2n.
| A | B | A & B | A | B | A ^ B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Flags and Common Tricks
#include <stdio.h>
#define FLAG_READ (1u << 0)
#define FLAG_WRITE (1u << 1)
#define FLAG_EXEC (1u << 2)
int main(void) {
unsigned int perms = 0;
perms |= FLAG_READ | FLAG_WRITE; /* set two bits */
printf("perms = %u\n", perms);
printf("can write? %s\n", (perms & FLAG_WRITE) ? "yes" : "no");
printf("can exec? %s\n", (perms & FLAG_EXEC) ? "yes" : "no");
perms &= ~FLAG_WRITE; /* clear a bit */
perms ^= FLAG_EXEC; /* toggle a bit */
printf("after clear write and toggle exec: %u\n", perms);
int n = 37;
printf("%d is %s\n", n, (n & 1) ? "odd" : "even");
printf("%d * 8 = %d, %d / 4 = %d\n", n, n << 3, n, n >> 2);
unsigned int x = 0xB6; /* 1011 0110 */
int count = 0;
while (x) { count += x & 1; x >>= 1; }
printf("set bits in 0xB6: %d\n", count);
unsigned int p = 64, q = 48;
printf("64 power of two? %d, 48 power of two? %d\n",
p && !(p & (p - 1)), q && !(q & (q - 1)));
int s = 5, t = 9;
s ^= t; t ^= s; s ^= t; /* XOR swap */
printf("after swap: s=%d t=%d\n", s, t);
return 0;
}
Output
perms = 3
can write? yes
can exec? no
after clear write and toggle exec: 5
37 is odd
37 * 8 = 296, 37 / 4 = 9
set bits in 0xB6: 5
64 power of two? 1, 48 power of two? 0
after swap: s=9 t=5
- Each flag is a single bit:
1u << 0is 1,1u << 1is 2,1u << 2is 4. Combining them with|stores several yes/no values in one integer, the way Unix file permissions work. - Set a bit with
x |= MASK, clear it withx &= ~MASK, toggle it withx ^= MASKand test it withx & MASK. n & 1is the lowest bit, so it is 1 for odd numbers.n & (n - 1)removes the lowest set bit. It is zero only for powers of two, which gives a one-line check.- The XOR swap exchanges two variables without a temporary. It is a classic interview trick, but a normal swap with a temporary variable is clearer and just as fast.
Bit Manipulation Cheat Sheet
| Task | Expression |
|---|---|
| Set bit k | x |= 1u << k |
| Clear bit k | x &= ~(1u << k) |
| Toggle bit k | x ^= 1u << k |
| Test bit k | (x >> k) & 1 |
| Is x odd? | x & 1 |
| Is x a power of two? | x && !(x & (x - 1)) |
Common Mistakes
- Confusing
&with&&:2 & 1is 0, but2 && 1is 1. - Forgetting precedence:
x & 1 == 0meansx & (1 == 0). Always add parentheses:(x & 1) == 0. - Shifting signed negative numbers or shifting by the type’s width or more, which is undefined or implementation-defined. Use unsigned types for bit work.
- Overflowing with
1 << 31on a 32-bitint; write1u << 31.
Practice Exercises
Exercise 1. Write a function that returns 1 if bit k of x is set.
Show answer
int is_set(unsigned int x, int k) {
return (x >> k) & 1u;
}
Exercise 2. Without using %, compute n % 8 for a non-negative integer.
Show answer
unsigned int r = n & 7; /* 8 = 1000, so the low 3 bits are the remainder */
Frequently Asked Questions
Are bitwise tricks faster than arithmetic?
Sometimes, but modern compilers already turn n * 8 into a shift and n % 8 into a mask for unsigned values. Write the clearer version unless you are working with bits on purpose.
Where are bitwise operators used in real programs?
In device drivers and embedded systems (hardware registers), network protocols (packet headers), graphics (colour channels), compression, cryptography, and permission flags such as Unix rwx.
Summary
&,|and^combine bits;~flips them;<<and>>shift them.- Masks with
1u << kset, clear, toggle and test individual bits. - Use unsigned types and parentheses to avoid surprises.
Next: typedef in C.
