C Bitwise Operators
Inside the computer every integer is a pattern of bits — zeros and ones. Most of the time you don't care, but C lets you work on those bits directly. Bitwise operators are used for flags and permissions, compact data formats, hardware registers in embedded systems, hashing, graphics and fast arithmetic tricks.
Binary in a nutshell
Each bit position stands for a power of two. The 8-bit pattern 00101101 is:
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Bit | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
32 + 8 + 4 + 1 = 45. Since printf has no binary specifier in C17, the examples below use a small helper that prints the lowest 8 bits.
The operators
| Operator | Name | Result bit is 1 when… |
|---|---|---|
& |
AND | both bits are 1 |
| |
OR | at least one bit is 1 |
^ |
XOR (exclusive or) | the bits are different |
~ |
NOT (complement) | the bit was 0 (flips every bit) |
<< |
left shift | bits move left, zeros come in on the right |
>> |
right shift | bits move right |
#include <stdio.h>
void print_bits(unsigned value) {
for (int bit = 7; bit >= 0; bit--) {
putchar((value >> bit) & 1 ? '1' : '0');
}
}
void show(const char *label, unsigned value) {
printf("%-8s ", label);
print_bits(value);
printf(" (%3u)\n", value & 0xFF);
}
int main(void) {
unsigned a = 0x5C; // 0101 1100 = 92
unsigned b = 0x3A; // 0011 1010 = 58
show("a", a);
show("b", b);
show("a & b", a & b);
show("a | b", a | b);
show("a ^ b", a ^ b);
show("~a", ~a);
show("a << 1", a << 1);
show("a >> 2", a >> 2);
return 0;
}a 01011100 ( 92)
b 00111010 ( 58)
a & b 00011000 ( 24)
a | b 01111110 (126)
a ^ b 01100110 (102)
~a 10100011 (163)
a << 1 10111000 (184)
a >> 2 00010111 ( 23)
Compare the columns bit by bit to see each rule in action. (~a actually flips all 32 bits of the unsigned; the helper only shows the lowest eight.)
Don't confuse:
&and|work on each bit, while&&and||work on whole true/false values.6 & 1is 0, but6 && 1is 1.
Shifts are fast multiplication and division
Shifting left by n multiplies by 2ⁿ; shifting an unsigned value right by n divides by 2ⁿ (rounding down):
#include <stdio.h>
int main(void) {
unsigned x = 13;
printf("13 << 1 = %u\n", x << 1);
printf("13 << 3 = %u\n", x << 3);
printf("13 >> 1 = %u\n", x >> 1);
printf("1 << 10 = %u\n", 1u << 10);
return 0;
}13 << 1 = 26
13 << 3 = 104
13 >> 1 = 6
1 << 10 = 1024
Note: Use unsigned types for bit manipulation. Shifting negative signed numbers, or shifting by as many bits as the type has (or more), is undefined or implementation-defined behaviour.
Working with single bits: set, clear, toggle, test
The expression 1u << n creates a mask with only bit n set. Combined with the operators above it gives four classic idioms:
| Task | Code |
|---|---|
Set bit n |
x |= 1u << n |
Clear bit n |
x &= ~(1u << n) |
Toggle bit n |
x ^= 1u << n |
Test bit n |
(x >> n) & 1 or x & (1u << n) |
A typical use is a set of on/off options packed into one integer:
#include <stdio.h>
enum {
OPT_BOLD = 1u << 0,
OPT_ITALIC = 1u << 1,
OPT_UNDERLINE = 1u << 2,
OPT_STRIKE = 1u << 3
};
void describe(unsigned style) {
printf("style 0x%X:", style);
if (style & OPT_BOLD) printf(" bold");
if (style & OPT_ITALIC) printf(" italic");
if (style & OPT_UNDERLINE) printf(" underline");
if (style & OPT_STRIKE) printf(" strike");
if (style == 0) printf(" plain");
printf("\n");
}
int main(void) {
unsigned style = 0;
describe(style);
style |= OPT_BOLD | OPT_UNDERLINE; // turn two options on
describe(style);
style ^= OPT_ITALIC; // toggle italic on
describe(style);
style &= ~OPT_BOLD; // turn bold off
describe(style);
return 0;
}style 0x0: plain
style 0x5: bold underline
style 0x7: bold italic underline
style 0x6: italic underline
Packing and extracting fields
Shifts and masks can store several small numbers in one integer. An RGB colour, for example, packs three 8-bit channels into 24 bits:
#include <stdio.h>
unsigned rgb(unsigned r, unsigned g, unsigned b) {
return (r << 16) | (g << 8) | b;
}
int main(void) {
unsigned orange = rgb(255, 165, 0);
printf("packed: #%06X\n", orange);
unsigned r = (orange >> 16) & 0xFF;
unsigned g = (orange >> 8) & 0xFF;
unsigned b = orange & 0xFF;
printf("unpacked: r=%u g=%u b=%u\n", r, g, b);
// Other classic tricks
unsigned n = 40;
printf("%u is %s\n", n, (n & 1) ? "odd" : "even");
printf("%u is a power of two? %s\n", n, (n & (n - 1)) == 0 ? "yes" : "no");
printf("64 is a power of two? %s\n", (64 & 63) == 0 ? "yes" : "no");
return 0;
}packed: #FFA500
unpacked: r=255 g=165 b=0
40 is even
40 is a power of two? no
64 is a power of two? yes
n & (n - 1) clears the lowest set bit; if the result is 0, n had only one bit set, so it is a power of two.
XOR's special property
XOR-ing a value with the same key twice gives back the original: (x ^ k) ^ k == x. This is the basis of simple checksums and toy encryption — and of the famous "swap without a temporary" trick, which is cute but less readable than using a temporary variable.
Precedence pitfall
Bitwise operators have lower precedence than comparisons. x & 1 == 0 means x & (1 == 0), which is always 0! Always parenthesise: (x & 1) == 0.
Test yourself
What is 12 & 10? (12 = 1100, 10 = 1010)
- 8
- 14
- 6
- 2
What is 5 << 2?
- 7
- 10
- 20
- 1
Which expression clears bit 3 of flags?
- flags |= 1u << 3
- flags &= ~(1u << 3)
- flags ^= 3
- flags >>= 3