← 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
cpp
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 और OR

Bitwise & और | एक number के पूरे value की बजाय individual binary digits पर operate करते हैं: & हर output bit को सिर्फ वहाँ 1 set करता है जहाँ दोनों operands में उस position पर 1 हो, जबकि | इसे 1 set करता है अगर कोई भी operand ऐसा करे।

ये आमतौर पर एक single integer में pack किए flags check या combine करने के लिए इस्तेमाल होते हैं, हर flag के लिए एक bit।

उदाहरण: Bitwise AND and OR

cpp
#include <iostream>

int main() {
	int a = 6, b = 3; // 110 and 011
	std::cout << (a & b) << std::endl; // 010 = 2
	std::cout << (a | b) << std::endl; // 111 = 7
	return 0;
}

Bitwise XOR

Bitwise ^ (XOR) हर output bit को exactly वहाँ 1 set करता है जहाँ दोनों operands के bits अलग हों, और जहाँ वे match करें वहाँ 0।

XOR की एक distinctive property यह है कि same value से इसे दो बार apply करना पहली application को undo कर देता है, जो कुछ simple encryption और value-swapping tricks की basis है।

उदाहरण: Bitwise XOR

cpp
#include <iostream>

int main() {
	int a = 5, b = 3;
	int x = a ^ b;
	std::cout << x << std::endl;
	std::cout << (x ^ b) << std::endl; // XOR twice undoes it: back to a
	return 0;
}

Bitwise NOT

Bitwise ~ (NOT) एक unary operator है जो अपने operand का हर bit flip करता है, हर 1 को 0 और हर 0 को 1 बनाते हुए — इसे one's complement भी कहा जाता है।

यह अक्सर एक bitmask बनाने के लिए इस्तेमाल होता है जो & के साथ combined होने पर specific bits clear करता है, क्योंकि ~mask exactly उन bits को invert करता है जिन्हें आप preserve या discard करना चाहते हैं।

उदाहरण: Bitwise NOT

cpp
#include <iostream>

int main() {
	unsigned char a = 5; // 00000101
	std::cout << (int)(unsigned char)(~a) << std::endl; // 250: every bit flipped
	return 0;
}

Left Shift और Right Shift

<< एक number के हर bit को दी गई positions की संख्या से left shift करता है, खाली हुए low bits को 0 से भरते हुए — और क्योंकि binary place values हर position के साथ double होते हैं, 1 से left shift करना 2 से multiply करने के बराबर है।

>> same तरह right shift करता है, जो shift की गई हर position के लिए 2 से integer division के बराबर है।

उदाहरण: Left Shift and Right Shift

cpp
#include <iostream>

int main() {
	int x = 4;
	std::cout << (x << 1) << std::endl; // 8: multiply by 2
	std::cout << (x >> 1) << std::endl; // 2: divide by 2
	return 0;
}

Bitwise Compound Assignment

Arithmetic operators की तरह ही, bitwise operators के compound assignment forms हैं — &=, |=, ^=, <<=, और >>= — जो operation को सीधे एक variable पर apply करते हैं और result वापस इसमें store करते हैं।

ये hardware registers या packed flag fields manage करने वाले low-level code में लगातार दिखते हैं, जहाँ बाकी को disturb किए बिना कुछ specific bits update करना एक routine operation है।

उदाहरण: Bitwise Compound Assignment

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare flags and set it to 0b0110
	int flags = 0b0110;
	flags &= 0b0100;
	// Print to the console with cout
	std::cout << flags << std::endl;
	// 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 की width जितने या उससे ज़्यादा से shift करना, जैसे 32-bit int पर 1 << 32, जो undefined behavior है।
  3. यह भूल जाना कि ~5 हर bit flip करता है और signed ints के लिए एक logical result की तरह 0 या 'not 5' की बजाय -6 देता है।

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.