इस ट्यूटोरियल में, हम उदाहरणों की मदद से C ++ में बिटवाइज़ ऑपरेटर्स के बारे में जानेंगे।
C ++ में, बिटवाइज़ ऑपरेटर व्यक्तिगत बिट-स्तर पर पूर्णांक डेटा पर संचालन करते हैं। इन ऑपरेशनों में वास्तविक बिट्स का परीक्षण, सेटिंग या स्थानांतरण शामिल है। उदाहरण के लिए,
a & b; a | b;
यहां C ++ में शामिल 6 बिटवाइज़ ऑपरेटरों की सूची दी गई है।
ऑपरेटर | विवरण |
---|---|
& | बिटवाइज़ और ऑपरेटर |
| | बिटवाइज़ या ऑपरेटर |
^ | बिटवाइज XOR ऑपरेटर |
~ | बिटवाइज़ पूरक परिचालक |
<< | बिटवाइज़ शिफ्ट लेफ्ट ऑपरेटर |
>> | बिटवाइज़ शिफ्ट राइट ऑपरेटर |
ये ऑपरेटर आवश्यक हैं क्योंकि कंप्यूटर के सीपीयू में मौजूद अरिथमेटिक-लॉजिक यूनिट (ALU) बिट-लेवल पर अंकगणितीय ऑपरेशन करता है।
नोट: बिटवाइज़ ऑपरेटरों को केवल डेटा char
और int
डेटा प्रकार के साथ ही इस्तेमाल किया जा सकता है ।
1. सी ++ बिटवाइज और ऑपरेटर
बिटवाइज़ और &
ऑपरेटर रिटर्न 1 यदि और केवल यदि दोनों ऑपरेंड हैं 1 । अन्यथा, यह 0 देता है ।
निम्न तालिका बिटवाइड और ऑपरेटर के काम को प्रदर्शित करती है । चलो एक और ख दो ऑपरेंड कि केवल ले जा सकते हैं द्विआधारी मूल्यों अर्थात् हो 1 और 0 ।
ए | बी | ए और बी |
---|---|---|
० | ० | ० |
० | 1 है | ० |
1 है | ० | ० |
1 है | 1 है | 1 है |
नोट: ऊपर दी गई तालिका को बिटवाइज़ और ऑपरेटर के लिए "ट्रूथ टेबल" के रूप में जाना जाता है ।
आइए दो पूर्णांक 12 और 25 के बिटवाइज़ और ऑपरेशन पर एक नज़र डालें :
12 = 00001100 (बाइनरी में) 25 = 00011001 (बाइनरी में) // 12 और 25 00001100 और 00011001 का बिटवाइज और ऑपरेशन _________ 00001000 = 8 (दशमलव में)
उदाहरण 1: बिटवाइज़ और
#include using namespace std; int main() ( // declare variables int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a & b = " << (a & b) << endl; return 0; )
आउटपुट
a = 12 b = 25 a & b = 8
उपरोक्त उदाहरण में, हमने दो चर a और b घोषित किए हैं। यहाँ, लाइन को नोटिस करें,
cout << "a & b = " << (a & b) << endl;
यहां, हम बिटवाइंड और वेरिएबल a और b के बीच में प्रदर्शन कर रहे हैं ।
2. सी ++ बिटवाइज या ऑपरेटर
बिटवाइज़ या |
ऑपरेटर रिटर्न 1 यदि ऑपरेंड के कम से कम एक है 1 । अन्यथा, यह 0 देता है ।
निम्न सत्य तालिका बिटवाइज या ऑपरेटर के काम को प्रदर्शित करती है । चलो एक और ख दो ऑपरेंड कि केवल ले जा सकते हैं द्विआधारी मूल्यों अर्थात् हो 1 या 0 ।
ए | बी | ए | बी |
---|---|---|
० | ० | ० |
० | 1 है | 1 है |
1 है | ० | 1 है |
1 है | 1 है | 1 है |
आइए हम दो पूर्णांक 12 और 25 के बिटवाइज या ऑपरेशन को देखें :
12 = 00001100 (बाइनरी में) 25 = 00011001 (बाइनरी में) 12 और 25 00001100 का बिटवाइज या ऑपरेशन | 00011001 _________ 00011101 = 29 (दशमलव में)
उदाहरण 2: बिटवाइज़ या
#include int main() ( int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a | b = " << (a | b) << endl; return 0; )
आउटपुट
a = 12 बी = 25 ए | बी = २ ९
बिटवाइज़ या की a = 12
और b = 25
देता है 29
।
3. C ++ बिटवाइज XOR ऑपरेटर
बिटवाइज़ XOR ^
ऑपरेटर देता है 1 अगर और ऑपरेंड के ही अगर एक है 1 । हालांकि, यदि दोनों ऑपरेंड 0 हैं , या दोनों 1 हैं , तो परिणाम 0 है ।
निम्न सत्य तालिका बिटवाइज़र XOR ऑपरेटर के कार्य को प्रदर्शित करती है । चलो एक और ख दो ऑपरेंड कि केवल ले जा सकते हैं द्विआधारी मूल्यों अर्थात् हो 1 या 0 ।
ए | बी | ए बी |
---|---|---|
० | ० | ० |
० | 1 है | 1 है |
1 है | ० | 1 है |
1 है | 1 है | ० |
आइए हम दो पूर्णांक 12 और 25 के बिट वाइज XOR ऑपरेशन को देखें:
12 = 00001100 (बाइनरी में) 25 = 00011001 (बाइनरी में) 12 और 25 00001100 में बिटवाइर XOR ऑपरेशन 00011001 _________ 00010101 = 21 (दशमलव में)
उदाहरण 3: बिटवाइज़ XOR
#include int main() ( int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a b = " << (a b) << endl; return 0; )
आउटपुट
a = 12 b = 25 a b = 21
बिटवाइज़ XOR की a = 12
और b = 25
देता है 21
।
4. सी ++ बिटवाइज पूरक परिचालक
बिटवाइज़ पूरक ऑपरेटर एक अपर ऑपरेटर (केवल एक ऑपरेंड पर काम करता है) है। यह ~
बाइनरी अंकों को 1 से 0 और 0 से 1 में बदलता है ।

यह ध्यान रखना महत्वपूर्ण है कि किसी भी पूर्णांक एन के बिटवाइज पूरक के बराबर है - (एन + 1) । उदाहरण के लिए,
एक पूर्णांक 35 पर विचार करें । नियम के अनुसार, 35 का बिटवाइज पूरक होना चाहिए - (35 + 1) = -36 । अब देखते हैं कि हमें सही उत्तर मिलता है या नहीं।
35 = 00100011 (In Binary) // Using bitwise complement operator ~ 00100011 __________ 11011100
In the above example, we get that the bitwise complement of 00100011 (35) is 11011100. Here, if we convert the result into decimal we get 220.
However, it is important to note that we cannot directly convert the result into decimal and get the desired output. This is because the binary result 11011100 is also equivalent to -36.
To understand this we first need to calculate the binary output of -36. We use 2's complement to calculate the binary of negative integers.
2's Complement
The 2's complement of a number N gives -N.
In binary arithmetic, 1's complement changes 0 to 1 and 1 to 0.
And, if we add 1 to the result of the 1's complement, we get the 2's complement of the original number.
For example,
36 = 00100100 (In Binary) 1's Complement = 11011011 2's Complement : 11011011 + 1 _________ 11011100
Here, we can see the 2's complement of 36 (i.e. -36) is 11011100. This value is equivalent to the bitwise complement of 35 that we have calculated in the previous section.
Hence, we can say that the bitwise complement of 35 = -36.
Example 4: Bitwise Complement
#include int main() ( int num1 = 35; int num2 = -150; cout << "~(" << num1 << ") = " << (~num1) << endl; cout << "~(" << num2 << ") = " << (~num2) << endl; return 0; )
Output
~(35) = -36 ~(-150) = 149
In the above example, we declared two integer variables num1 and num2, and initialized them with the values of 35
and -150
respectively.
We then computed their bitwise complement with the codes (~num1)
and (~num2)
respectively and displayed them on the screen.
The bitwise complement of 35 = - (35 + 1) = -36 i.e. ~35 = -36 The bitwise complement of -150 = - (-150 + 1) = - (-149) = 149 i.e. ~(-150) = 149
This is exactly what we got in the output.
C++ Shift Operators
There are two shift operators in C++ programming:
- Right shift operator
>>
- Left shift operator
<<
5. C++ Right Shift Operator
The right shift operator shifts all bits towards the right by a certain number of specified bits. It is denoted by >>
.
जब हम किसी संख्या को दाईं ओर शिफ्ट करते हैं, तो सबसे महत्वपूर्ण बिट्स को छोड़ दिया जाता है, जबकि सबसे महत्वपूर्ण बिट्स को शून्य से बदल दिया जाता है।

जैसा कि हम ऊपर की छवि से देख सकते हैं, हमारे पास 4-बिट संख्या है । जब हम उस पर एक-बिट राइट शिफ्ट ऑपरेशन करते हैं, तो प्रत्येक व्यक्ति बिट को दाईं ओर 1 बिट में स्थानांतरित कर दिया जाता है।
नतीजतन, दाएं-सबसे बिट को छोड़ दिया जाता है, जबकि बाएं-सबसे थोड़ा खाली रहता है। इस रिक्ति को 0 से बदल दिया गया है ।
6. सी ++ लेफ्ट शिफ्ट ऑपरेटर
बाईं पारी ऑपरेटर की एक निश्चित संख्या द्वारा छोड़ा ओर सभी बिट्स बदलाव निर्दिष्ट बिट्स । इसके द्वारा निरूपित किया जाता है <<
।

As we can see from the image above, we have a 4-bit number. When we perform a 1 bit left shift operation on it, each individual bit is shifted to the left by 1 bit.
As a result, the left-most bit is discarded, while the right-most bit remains vacant. This vacancy is replaced by a 0.
Example 5: Shift Operators
#include int main() ( // declaring two integer variables int num = 212, i; // Shift Right Operation cout << "Shift Right:" << endl; // Using for loop for shifting num right from 0 bit to 3 bits for (i = 0; i < 4; i++) ( cout <> " << i << " = " <> i) << endl; ) // Shift Left Operation cout << "Shift Left:" << endl; // Using for loop for shifting num left from 0 bit to 3 bits for (i = 0; i < 4; i++) ( cout << "212 << " << i << " = " << (212 << i) << endl; ) return 0; )
Output
Shift Right: 212>> 0 = 212 212>> 1 = 106 212>> 2 = 53 212>> 3 = 26 Shift Left: 212 << 0 = 212 212 << 1 = 424 212 << 2 = 848 212 << 3 = 1696
From the output of the program above, we can infer that, for any number N, the results of the shift right operator are:
N>> 0 = N N>> 1 = (N>> 0) / 2 N>> 2 = (N>> 1) / 2 N>> 3 = (N>> 2) / 2
and so on.
Similarly, the results of the shift left operator are:
N << 0 = N N << 1 = (N << 0) * 2 N << 2 = (N << 1) * 2 N << 3 = (N << 2) * 2
and so on.
Hence we can conclude that,
N>> m = ( N>> (m-1) ) / 2 N << m = ( N << (m-1) ) * 2
In the above example, note that the int
data type stores numbers in 32-bits i.e. an int
value is represented by 32 binary digits.
However, our explanation for the bitwise shift operators used numbers represented in 4-bits.
For example, the base-10 number 13 can be represented in 4-bit and 32-bit as:
4-bit Representation of 13 = 1101 32-bit Representation of 13 = 00000000 00000000 00000000 00001101
नतीजतन, बिटव्यू लेफ्ट-शिफ्ट ऑपरेशन 13 (और किसी भी अन्य संख्या) के लिए बिट्स की संख्या के आधार पर अलग-अलग हो सकता है जो वे द्वारा दर्शाए गए हैं।
क्योंकि 32-बिट प्रतिनिधित्व में, कई और बिट्स हैं जिन्हें 4-बिट प्रतिनिधित्व की तुलना में बाएं स्थानांतरित किया जा सकता है ।