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

Bit Manipulation की मूल बातें

Bit manipulation छोटी light switches की एक row के साथ काम करने जैसा है, हर एक on या off, जहां computers numbers store करते हैं, और आप उन्हें सीधे flip या check करते हैं।
Syntax
markup
a & b     # AND
a | b     # OR
a ^ b     # XOR
~a        # NOT
a << k    # left shift
a >> k    # right shift

Binary Representation

हर integer memory में bits के एक sequence के रूप में stored है, हर एक या तो 0 या 1, और bitwise operations सीधे उन individual bits के साथ काम करते हैं बजाय number को एक single opaque value की तरह treat करने के जैसे arithmetic operations करते हैं।

उदाहरण: Binary Representation

#include <iostream>
#include <bitset>
using namespace std;
int main() {
	int n = 13;
	cout << n << " in binary: " << bitset<8>(n);
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int n = 13;
		System.out.println(n + " in binary: " + Integer.toBinaryString(n));
	}
}
n = 13
print(f"{n} in binary: {bin(n)[2:]}")
#include <stdio.h>
int main() {
	int n = 13;
	printf("%d in binary: ", n);
	for (int i = 7; i >= 0; i--) printf("%d", (n >> i) & 1);
	return 0;
}

Bitwise Operators

AND, OR, और XOR दो numbers को bit by bit compare करते हैं; NOT किसी single number की हर bit flip करता है; left shift और right shift किसी number की सभी bits को चुनी संख्या में positions से move करते हैं, खाली हुई जगहों को zeros से भरते हुए।

उदाहरण: Bitwise Operators

#include <iostream>
using namespace std;
int main() {
	int a = 12, b = 10;
	cout << "AND=" << (a&b) << " OR=" << (a|b) << " XOR=" << (a^b) << " NOT a=" << (~a);
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int a = 12, b = 10;
		System.out.println("AND=" + (a&b) + " OR=" + (a|b) + " XOR=" + (a^b) + " NOT a=" + (~a));
	}
}
a, b = 12, 10
print(f"AND={a&b} OR={a|b} XOR={a^b} NOT a={~a}")
#include <stdio.h>
int main() {
	int a = 12, b = 10;
	printf("AND=%d OR=%d XOR=%d NOT a=%d", a&b, a|b, a^b, ~a);
	return 0;
}

Left Shift

Bits को k positions left shift करना right पर k zero bits insert करता है, जो एक positive integer के लिए 2^k से multiply करने जैसा ही effect है — उदाहरण के लिए, 3 (binary 011) को 1 left shift करना 6 (binary 110) देता है।

उदाहरण: Left Shift

#include <iostream>
using namespace std;
int main() {
	int n = 3;
	cout << n << " << 1 = " << (n << 1) << " (same as multiplying by 2)";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int n = 3;
		System.out.println(n + " << 1 = " + (n << 1) + " (same as multiplying by 2)");
	}
}
n = 3
print(f"{n} << 1 = {n << 1} (same as multiplying by 2)")
#include <stdio.h>
int main() {
	int n = 3;
	printf("%d << 1 = %d (same as multiplying by 2)", n, n << 1);
	return 0;
}

Right Shift

Bits को k positions right shift करना k rightmost bits हटाता है, जो एक positive integer के लिए 2^k से divide करने और कोई remainder discard करने जैसा ही effect है — उदाहरण के लिए, 6 को 1 right shift करना 3 देता है।

उदाहरण: Right Shift

#include <iostream>
using namespace std;
int main() {
	int n = 6;
	cout << n << " >> 1 = " << (n >> 1) << " (same as dividing by 2, remainder discarded)";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int n = 6;
		System.out.println(n + " >> 1 = " + (n >> 1) + " (same as dividing by 2, remainder discarded)");
	}
}
n = 6
print(f"{n} >> 1 = {n >> 1} (same as dividing by 2, remainder discarded)")
#include <stdio.h>
int main() {
	int n = 6;
	printf("%d >> 1 = %d (same as dividing by 2, remainder discarded)", n, n >> 1);
	return 0;
}

Bit Practice

ये bit operations सीधे CPU की native hardware instructions पर चलते हैं, उन्हें बेहद fast बनाते हुए, यही कारण है कि वे flags, masks, hashing, और low-level performance-sensitive code में building blocks के रूप में दिखते हैं।

उदाहरण: Bit Practice

#include <iostream>
using namespace std;
int main() {
	cout << "Bit ops run on native CPU instructions -- extremely fast, used in flags, masks, hashing";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		System.out.println("Bit ops run on native CPU instructions -- extremely fast, used in flags, masks, hashing");
	}
}
print("Bit ops run on native CPU instructions -- extremely fast, used in flags, masks, hashing")
#include <stdio.h>
int main() {
	printf("Bit ops run on native CPU instructions -- extremely fast, used in flags, masks, hashing");
	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. एक signed negative number को right shift करना और एक zero fill मान लेना, जब sign bit copy हो सकती है।
  2. & (bitwise) को && (logical) के साथ confuse करना, जो result बदल देता है।
  3. एक 32-bit int पर 32 या ज़्यादा bits shift करना, जो undefined behavior है।
🔒

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.