Practical Bit Tricks
Common techniques.
Practical Bit Tricks is a free C Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
Useful Bit Tricks
Once you understand bitwise operators, a set of compact tricks becomes available. They are fast and appear often in real code.
Let us walk through the most common ones.
#include <stdio.h>
int main(void) {
unsigned x = 6;
printf("x & 1 = %u (odd if 1)\n", x & 1);
return 0;
}Even or Odd
The lowest bit tells parity. x & 1 is 1 for odd numbers and 0 for even numbers.
#include <stdio.h>
int main(void) {
for (unsigned x = 0; x < 5; x++) {
printf("%u is %s\n", x, (x & 1) ? "odd" : "even");
}
return 0;
}Power of Two Check
A positive number is a power of two if it has exactly one bit set. The expression x & (x - 1) is 0 only in that case.
#include <stdio.h>
int is_pow2(unsigned x) {
return x != 0 && (x & (x - 1)) == 0;
}
int main(void) {
printf("%d %d %d\n", is_pow2(16), is_pow2(18), is_pow2(1));
return 0;
}Clear the Lowest Set Bit
x & (x - 1) clears the lowest set bit. This is the basis of the fast set-bit count.
#include <stdio.h>
int main(void) {
unsigned x = 0b10110;
printf("before: %u\n", x);
printf("after: %u\n", x & (x - 1));
return 0;
}Isolate the Lowest Set Bit
x & (-x) isolates the lowest set bit, leaving only that single bit on.
This relies on two's complement representation.
#include <stdio.h>
int main(void) {
unsigned x = 0b10110;
printf("lowest set bit = %u\n", x & (-x));
return 0;
}Counting Set Bits
Repeatedly clearing the lowest set bit counts how many bits are set, in as many steps as there are set bits.
#include <stdio.h>
int popcount(unsigned x) {
int n = 0;
while (x) { x &= (x - 1); n++; }
return n;
}
int main(void) {
printf("%d\n", popcount(255));
printf("%d\n", popcount(0b1010));
return 0;
}Swapping Without a Temp
The XOR swap exchanges two integers using no extra storage.
#include <stdio.h>
int main(void) {
int a = 3, b = 8;
a ^= b; b ^= a; a ^= b;
printf("a=%d b=%d\n", a, b);
return 0;
}Rounding Up to Power of Two
You can round an unsigned value up to the next power of two by smearing the highest bit downward, then adding 1.
#include <stdio.h>
unsigned next_pow2(unsigned v) {
v--;
v |= v >> 1; v |= v >> 2; v |= v >> 4;
v |= v >> 8; v |= v >> 16;
return v + 1;
}
int main(void) {
printf("%u\n", next_pow2(17));
printf("%u\n", next_pow2(100));
return 0;
}Checking if Bits Differ
Two values have different bits exactly where a ^ b has set bits. Counting those gives the Hamming distance.
#include <stdio.h>
int main(void) {
unsigned a = 0b1101, b = 0b1011;
unsigned diff = a ^ b;
int count = 0;
while (diff) { diff &= diff - 1; count++; }
printf("hamming distance = %d\n", count);
return 0;
}Absolute Value Without Branch
Using the sign bit, you can compute an absolute value without an if. The mask is all ones for negatives, all zeros for non-negatives.
#include <stdio.h>
int main(void) {
int x = -42;
int mask = x >> 31;
int abs = (x + mask) ^ mask;
printf("abs = %d\n", abs);
return 0;
}Setting a Bit by Index
Combine shifting and OR to set an arbitrary bit position computed at runtime.
#include <stdio.h>
int main(void) {
unsigned v = 0;
int positions[] = {1, 3, 5};
for (int i = 0; i < 3; i++) {
v |= (1u << positions[i]);
}
printf("v = %u\n", v);
return 0;
}Quick Check
Test your knowledge of bit tricks.
Recap
You learned practical bit tricks:
x & 1tests odd/even.x & (x - 1)clears the lowest set bit and detects powers of two.x & (-x)isolates the lowest set bit.- Popcount, XOR swap, Hamming distance, and branchless absolute value.
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Practical Bit Tricks” lesson free?
Yes — the full text of “Practical Bit Tricks” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “Practical Bit Tricks”?
Common techniques. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Practical Bit Tricks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Bitwise Operators
- Shifts
- Bit Masks and Flags
- Practical Bit Tricks