C ++ Bitweise Operatoren

In diesem Tutorial lernen wir anhand von Beispielen bitweise Operatoren in C ++ kennen.

In C ++ führen bitweise Operatoren Operationen an ganzzahligen Daten auf der Ebene der einzelnen Bits aus. Diese Operationen umfassen das Testen, Setzen oder Verschieben der tatsächlichen Bits. Beispielsweise,

 a & b; a | b;

Hier ist eine Liste von 6 bitweisen Operatoren, die in C ++ enthalten sind.

Operator Beschreibung
& Bitweiser UND-Operator
| Bitweiser ODER-Operator
^ Bitweiser XOR-Operator
~ Bitweiser Komplementoperator
<< Bitweise Verschiebung nach links
>> Bitweiser Shift-Rechts-Operator

Diese Operatoren sind erforderlich, da die in der CPU des Computers vorhandene Arithmetic-Logic Unit (ALU) arithmetische Operationen auf Bitebene ausführt.

Hinweis: Bitweise Operatoren können nur neben charund intDatentypen verwendet werden.

1. C ++ Bitweiser UND-Operator

Der bitweise AND- & Operator gibt genau dann 1 zurück, wenn beide Operanden 1 sind . Andernfalls wird 0 zurückgegeben .

Die folgende Tabelle zeigt die Funktionsweise des bitweisen AND- Operators. Lassen Sie ein und b zwei Operanden, die nur binäre Werte , dh nehmen 1 und 0 .

ein b a & b
0 0 0
0 1 0
1 0 0
1 1 1

Hinweis: Die obige Tabelle wird als "Wahrheitstabelle" für den bitweisen UND- Operator bezeichnet.

Schauen wir uns die bitweise UND- Verknüpfung zweier Ganzzahlen 12 und 25 an:

 12 = 00001100 (binär) 25 = 00011001 (binär) // Bitweise UND-Operation von 12 und 25 00001100 & 00011001 _________ 00001000 = 8 (dezimal)

Beispiel 1: Bitweises UND

 #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; )

Ausgabe

 a = 12 b = 25 a & b = 8

Im obigen Beispiel haben wir zwei Variablen a und b deklariert. Beachten Sie hier die Linie,

 cout << "a & b = " << (a & b) << endl;

Hier führen wir bitweises UND zwischen den Variablen a und b durch.

2. C ++ Bitweiser ODER-Operator

Der bitweise ODER- | Operator gibt 1 zurück, wenn mindestens einer der Operanden 1 ist . Andernfalls wird 0 zurückgegeben .

Die folgende Wahrheitstabelle zeigt die Arbeitsweise des bitweisen ODER- Operators. Sei a und b zwei Operanden, die nur Binärwerte annehmen können, dh 1 oder 0 .

ein b a | b
0 0 0
0 1 1
1 0 1
1 1 1

Betrachten wir die bitweise ODER- Verknüpfung zweier Ganzzahlen 12 und 25 :

12 = 00001100 (binär) 25 = 00011001 (binär) Bitweise ODER Betrieb von 12 und 25 00001100 | 00011001 _________ 00011101 = 29 (dezimal)

Beispiel 2: Bitweises ODER

 #include int main() ( int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a | b = " << (a | b) << endl; return 0; )

Ausgabe

a = 12 b = 25 a | b = 29

Das bitweise ODER von a = 12und b = 25gibt 29.

3. C ++ Bitweiser XOR-Operator

Der bitweise XOR ^ - Operator gibt 1 , wenn und nur , wenn einer der Operanden 1 . Wenn jedoch beide Operanden 0 sind oder wenn beide 1 sind , ist das Ergebnis 0 .

Die folgende Wahrheitstabelle zeigt die Arbeitsweise des bitweisen XOR- Operators. Sei a und b zwei Operanden, die nur Binärwerte annehmen können, dh 1 oder 0 .

ein b a b
0 0 0
0 1 1
1 0 1
1 1 0

Betrachten wir die bitweise XOR- Operation zweier Ganzzahlen 12 und 25:

 12 = 00001100 (binär) 25 = 00011001 (binär) Bitweiser XOR-Betrieb von 12 und 25 00001100 00011001 _________ 00010101 = 21 (dezimal)

Beispiel 3: Bitweises 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; )

Ausgabe

 a = 12 b = 25 a b = 21

Das bitweise XOR von a = 12und b = 25gibt 21.

4. C ++ Bitweiser Komplementoperator

Der bitweise Komplementoperator ist ein unärer Operator (funktioniert nur mit einem Operanden). Es wird dadurch bezeichnet, ~dass die Binärziffern 1 auf 0 und 0 auf 1 geändert werden .

Bitweise Ergänzung

Es ist wichtig zu beachten, dass das bitweise Komplement einer ganzen Zahl N gleich - (N + 1) ist . Beispielsweise,

Betrachten Sie eine ganze Zahl 35 . Gemäß der Regel das bitweise Komplement von 35 sollte - (35 + 1) = -36 . Nun wollen wir sehen, ob wir die richtige Antwort bekommen oder nicht.

 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 >>.

Wenn wir eine Zahl nach rechts verschieben, werden die niedrigstwertigen Bits verworfen, während die höchstwertigen Bits durch Nullen ersetzt werden.

ein Bit Rechtsverschiebung

Wie wir aus dem obigen Bild sehen können, haben wir eine 4-Bit-Nummer . Wenn wir eine Ein-Bit- Rechtsverschiebungsoperation ausführen, wird jedes einzelne Bit um 1 Bit nach rechts verschoben.

Infolgedessen wird das Bit ganz rechts verworfen, während das Bit ganz links frei bleibt. Diese Stelle wird durch eine 0 ersetzt .

6. C ++ Left Shift Operator

Der Linksverschiebungsoperator verschiebt alle Bits um eine bestimmte Anzahl spezifizierter Bits nach links . Es wird mit bezeichnet <<.

ein Bit Linksverschiebung

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 

Infolgedessen kann die bitweise Linksverschiebungsoperation für 13 (und jede andere Zahl) abhängig von der Anzahl der Bits, durch die sie dargestellt werden, unterschiedlich sein.

Denn bei der 32-Bit- Darstellung können im Vergleich zur 4-Bit- Darstellung viel mehr Bits nach links verschoben werden .

Interessante Beiträge...