← Back to DSA Course | Chapter 17: Bit Manipulation | Lesson 2 of 5

Common Bit Tricks

Check Odd or Even

Checking a positive integer's lowest bit (using n & 1) instantly tells you whether it's odd (bit is 1) or even (bit is 0), avoiding a division or modulo operation entirely.

Example: Check Odd or Even

#include <iostream>
using namespace std;
int main() {
	int n = 7;
	cout << n << " is " << ((n & 1) ? "odd" : "even") << " (checked via n & 1, no division needed)";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int n = 7;
		System.out.println(n + " is " + ((n & 1) != 0 ? "odd" : "even") + " (checked via n & 1, no division needed)");
	}
}
n = 7
print(f"{n} is {'odd' if n & 1 else 'even'} (checked via n & 1, no division needed)")
#include <stdio.h>
int main() {
	int n = 7;
	printf("%d is %s (checked via n & 1, no division needed)", n, (n & 1) ? "odd" : "even");
	return 0;
}

Set and Clear

OR-ing a number with a mask that has a single bit set turns that specific bit on without disturbing any other bits, while AND-ing with the complement of that same mask turns the bit off, again leaving everything else untouched.

Example: Set and Clear

#include <iostream>
using namespace std;
int main() {
	int n = 5;
	int setBit2 = n | (1 << 2);
	int clearBit0 = n & ~(1 << 0);
	cout << "Set bit 2: " << setBit2 << ", clear bit 0: " << clearBit0;
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int n = 5;
		int setBit2 = n | (1 << 2);
		int clearBit0 = n & ~(1 << 0);
		System.out.println("Set bit 2: " + setBit2 + ", clear bit 0: " + clearBit0);
	}
}
n = 5
set_bit2 = n | (1 << 2)
clear_bit0 = n & ~(1 << 0)
print(f"Set bit 2: {set_bit2}, clear bit 0: {clear_bit0}")
#include <stdio.h>
int main() {
	int n = 5;
	int setBit2 = n | (1 << 2);
	int clearBit0 = n & ~(1 << 0);
	printf("Set bit 2: %d, clear bit 0: %d", setBit2, clearBit0);
	return 0;
}

Toggle Bit

XOR-ing a number with a mask that has exactly one bit set flips just that bit — a 0 becomes 1, and a 1 becomes 0 — which is why XOR is the standard tool for 'toggle this flag' operations.

Example: Toggle Bit

#include <iostream>
using namespace std;
int main() {
	int n = 5;
	int toggled = n ^ (1 << 1);
	cout << n << " with bit 1 toggled: " << toggled;
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int n = 5;
		int toggled = n ^ (1 << 1);
		System.out.println(n + " with bit 1 toggled: " + toggled);
	}
}
n = 5
toggled = n ^ (1 << 1)
print(f"{n} with bit 1 toggled: {toggled}")
#include <stdio.h>
int main() {
	int n = 5;
	int toggled = n ^ (1 << 1);
	printf("%d with bit 1 toggled: %d", n, toggled);
	return 0;
}

Read a Bit

AND-ing a number with a mask that isolates one bit position, then checking whether the result is zero or non-zero, tells you the current state of that specific bit without needing to shift or convert to binary manually.

Example: Read a Bit

#include <iostream>
using namespace std;
int main() {
	int n = 5;
	bool bit1set = (n & (1 << 1)) != 0;
	cout << "Bit 1 of " << n << " is " << (bit1set ? "1" : "0");
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int n = 5;
		boolean bit1set = (n & (1 << 1)) != 0;
		System.out.println("Bit 1 of " + n + " is " + (bit1set ? "1" : "0"));
	}
}
n = 5
bit1_set = (n & (1 << 1)) != 0
print(f"Bit 1 of {n} is {'1' if bit1_set else '0'}")
#include <stdio.h>
int main() {
	int n = 5;
	int bit1set = (n & (1 << 1)) != 0;
	printf("Bit 1 of %d is %s", n, bit1set ? "1" : "0");
	return 0;
}

Useful Tricks

These small bit tricks replace what would otherwise be multi-step conditional logic with a single fast operation, which is why they show up constantly in performance-sensitive code, flag handling, and low-level systems programming.

Example: Useful Tricks

#include <iostream>
using namespace std;
int main() {
	cout << "Bit tricks replace multi-step conditionals with a single fast op -- flags, hashing, perf code";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		System.out.println("Bit tricks replace multi-step conditionals with a single fast op -- flags, hashing, perf code");
	}
}
print("Bit tricks replace multi-step conditionals with a single fast op -- flags, hashing, perf code")
#include <stdio.h>
int main() {
	printf("Bit tricks replace multi-step conditionals with a single fast op -- flags, hashing, perf code");
	return 0;
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.