सामान्य Bit Tricks
In this page:
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
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;
}
Login to try C/C++/Java code in the editor
- एक
intके साथ एक बड़ेiपर1 << iउपयोग करना, जो 31 bits से आगे overflow करता है। n & ~(1 << i)के बजायn & (1 << i)से एक bit clear करना।- Parentheses भूल जाना, जैसे
n & 1 == 0, जोn & (1 == 0)के रूप में parse होता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: