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

सामान्य Bit Tricks

Bit tricks उन light switches पर handy shortcuts हैं, जैसे यह जानने के लिए कि कोई number odd है या even आखिरी switch जांचना।
Syntax
markup
n & 1 == 0            # even check
n | (1 << i)          # set bit i
n & ~(1 << i)         # clear bit i
n ^ (1 << i)          # toggle bit i

Check Odd or Even

किसी positive integer की सबसे lowest bit जांचना (n & 1 उपयोग करके) तुरंत बताता है यह odd है (bit 1 है) या even (bit 0 है), एक division या modulo operation को पूरी तरह avoid करते हुए।

उदाहरण: 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

एक single bit set वाले एक mask से एक number को OR करना उस specific bit को बिना किसी दूसरी bits को disturb किए on कर देता है, जबकि उसी mask के complement के साथ AND करना bit को off कर देता है, फिर बाकी सब कुछ untouched छोड़ते हुए।

उदाहरण: 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

बिल्कुल एक bit set वाले एक mask से एक number को XOR करना बस उस bit को flip करता है — एक 0, 1 बन जाता है, और एक 1, 0 बन जाता है — यही कारण है कि XOR 'toggle this flag' operations के लिए standard tool है।

उदाहरण: 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

एक bit position isolate करने वाले एक mask से एक number को AND करना, फिर यह जांचना कि result zero है या non-zero, आपको manually shift या binary में convert किए बिना उस specific bit की current state बताता है।

उदाहरण: 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

ये छोटी bit tricks अन्यथा multi-step conditional logic वाली चीज़ को एक single fast operation से replace करती हैं, यही कारण है कि वे performance-sensitive code, flag handling, और low-level systems programming में लगातार दिखती हैं।

उदाहरण: 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक int के साथ एक बड़े i पर 1 << i उपयोग करना, जो 31 bits से आगे overflow करता है।
  2. n & ~(1 << i) के बजाय n & (1 << i) से एक bit clear करना।
  3. Parentheses भूल जाना, जैसे n & 1 == 0, जो n & (1 == 0) के रूप में parse होता है।
🔒

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.