← Back to C Course | Chapter 3: Operators | Lesson 5 of 9

C के Bitwise Operators

Bitwise operators numbers के अंदर छोटे 0s और 1s पर काम करते हैं, light switches की rows को flip करने जैसा। ये आपको individual switches check या change करने देते हैं।
Syntax
c
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 (&)

c
// 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;
}

Bitwise OR (|)

| दो numbers को bit by bit compare करता है और हर उस position पर 1 produce करता है जहाँ कम से कम एक operand में 1 हो -- आमतौर पर बाकियों को disturb किए बिना specific bits on करने के लिए इस्तेमाल होता है, जैसे एक bitmask के अंदर एक flag set करना।

उदाहरण: Bitwise OR (|)

c
// 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;
}

Bitwise XOR (^)

^ (XOR) हर उस bit position पर 1 produce करता है जहाँ दो operands अलग हों, और जहाँ वे match करें वहाँ 0 -- एक classic इस्तेमाल एक bit को on या off toggle करना है, या एक temporary variable के बिना दो values को swap करना।

उदाहरण: Bitwise XOR (^)

c
// 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;
}

Bitwise NOT (~)

~ एक unary operator है जो अपने single operand का हर bit flip करता है -- हर 1, 0 बन जाता है और हर 0, 1 -- जो एक signed integer के लिए effectively इसका bitwise complement produce करता है, सिर्फ इसका negative नहीं।

उदाहरण: Bitwise NOT ()

c
// 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;
}

Shift Operators (<< और >>)

<< सारे bits को left से एक दी गई संख्या positions shift करता है (2 की एक power से multiply करने के बराबर), जबकि >> उन्हें right shift करता है (roughly 2 की एक power से integer division के बराबर) -- दोनों actual multiplication या division से कहीं ज़्यादा तेज़ हैं।

उदाहरण: Shift Operators (<< and >>)

c
// 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;
}
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. && का मतलब होने पर & इस्तेमाल करना, जो एक true/false test की बजाय एक bitwise result देता है।
  2. एक negative amount से या type में जितने bits हैं उससे ज़्यादा से shift करना, जो undefined behavior है।
  3. signed negative numbers पर bitwise operators इस्तेमाल करना और result से surprise होना।

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.