Bit Manipulation की मूल बातें
In this page:
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
- एक signed negative number को right shift करना और एक zero fill मान लेना, जब sign bit copy हो सकती है।
&(bitwise) को&&(logical) के साथ confuse करना, जो result बदल देता है।- एक 32-bit
intपर 32 या ज़्यादा bits shift करना, जो undefined behavior है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: