C के Bitwise Operators
In this page:
result = value1 & value2; // AND
result = value1 | value2; // OR
result = value1 ^ value2; // XOR
result = ~value; // NOT
result = value << n; // shift left
result = value >> n; // shift right
Bitwise AND (&)
& दो numbers को bit by bit compare करता है और हर उस position पर 1 produce करता है जहाँ दोनों operands में 1 हो, और बाकी हर जगह 0 -- अक्सर किसी value के अंदर specific bits check या isolate करने के लिए इस्तेमाल होता है, जैसे configuration flags पढ़ना।
उदाहरण: Bitwise AND (&)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 6, b = 3;
// Print formatted text to the screen
printf("%d", a & b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bitwise OR (|)
| दो numbers को bit by bit compare करता है और हर उस position पर 1 produce करता है जहाँ कम से कम एक operand में 1 हो -- आमतौर पर बाकियों को disturb किए बिना specific bits on करने के लिए इस्तेमाल होता है, जैसे एक bitmask के अंदर एक flag set करना।
उदाहरण: Bitwise OR (|)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 6, b = 3;
// Print formatted text to the screen
printf("%d", a | b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bitwise XOR (^)
^ (XOR) हर उस bit position पर 1 produce करता है जहाँ दो operands अलग हों, और जहाँ वे match करें वहाँ 0 -- एक classic इस्तेमाल एक bit को on या off toggle करना है, या एक temporary variable के बिना दो values को swap करना।
उदाहरण: Bitwise XOR (^)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 6, b = 3;
// Print formatted text to the screen
printf("%d", a ^ b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bitwise NOT (~)
~ एक unary operator है जो अपने single operand का हर bit flip करता है -- हर 1, 0 बन जाता है और हर 0, 1 -- जो एक signed integer के लिए effectively इसका bitwise complement produce करता है, सिर्फ इसका negative नहीं।
उदाहरण: Bitwise NOT ()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
// Print formatted text to the screen
printf("%d", ~a);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Shift Operators (<< और >>)
<< सारे bits को left से एक दी गई संख्या positions shift करता है (2 की एक power से multiply करने के बराबर), जबकि >> उन्हें right shift करता है (roughly 2 की एक power से integer division के बराबर) -- दोनों actual multiplication या division से कहीं ज़्यादा तेज़ हैं।
उदाहरण: Shift Operators (<< and >>)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 4
int a = 4;
// Print formatted text to the screen
printf("%d %d", a << 1, a >> 1);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
&&का मतलब होने पर&इस्तेमाल करना, जो एक true/false test की बजाय एक bitwise result देता है।- एक negative amount से या type में जितने bits हैं उससे ज़्यादा से shift करना, जो undefined behavior है।
- signed negative numbers पर bitwise operators इस्तेमाल करना और result से surprise होना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: