C ++ operatori pa bitēm

Šajā apmācībā mēs uzzināsim par bitkuru operatoriem C ++, izmantojot piemērus.

Programmā C ++ operatori ar bitiem veic darbības ar veseliem skaitļiem individuālā bitu līmenī. Šīs darbības ietver faktisko bitu testēšanu, iestatīšanu vai pārvietošanu. Piemēram,

 a & b; a | b;

Šeit ir saraksts ar 6 operatoriem, kas iekļauti C ++.

Operators Apraksts
& Bitwise UN operators
| Bitwise OR operators
^ Operators Bitwise XOR
~ Bitwise papildināšanas operators
<< Bitu kustības pārslēgšana pa kreisi
>> Bitu kustības pārslēgšana pa labi Operators

Šie operatori ir nepieciešami, jo datora procesorā esošā aritmētiskās-loģiskās vienības (ALU) bitu līmenī veic aritmētiskās darbības.

Piezīme: Bitu līmeņa operatoriem var izmantot tikai kopā charun intdatu tipi.

1. C ++ Bitwise UN operators

Par Bitu līmeņa un & operators atgriež 1 , ja un tikai tad, ja abi operandi ir 1 . Pretējā gadījumā tas atgriež 0 .

Nākamajā tabulā parādīts, kā darbojas operētājsistēma bitā UN . Ļaujiet a un b būt diviem operandiem, kas var iegūt tikai bināras vērtības, ti, 1 un 0 .

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

Piezīme. Iepriekš minētā tabula ir pazīstama kā “Patiesības tabula” operatoram bit bit un

Apskatīsim divu veselu skaitļu 12 un 25 bitu darbību:

 12 = 00001100 (binārā formātā) 25 = 00011001 (binārā formātā) // 12 un 25 darbības pa bitēm UN darbība 00001100 & 00011001 _________ 00001000 = 8 (aiz komata)

1. piemērs

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

Rezultāts

 a = 12 b = 25 a & b = 8

Iepriekš minētajā piemērā mēs esam deklarējuši divus mainīgos a un b. Lūk, pamaniet līniju,

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

Šeit mēs veicam bitu kustības JA starp mainīgajiem a un b.

2. C ++ Operators pa bitiem VAI

Par Bitu līmeņa VAI | operators atgriež 1 , ja vismaz viens no operandiem ir 1 . Pretējā gadījumā tas atgriež 0 .

Šajā patiesības tabulā parādīts operētājsistēmas OR bitu darbība . Ļaujiet a un b būt diviem operandiem, kas var iegūt tikai bināras vērtības, ti, 1 vai 0 .

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

Apskatīsim divu veselu skaitļu 12 un 25 bitu darbību OR :

12 = 00001100 (binārā formātā) 25 = 00011001 (binārā formātā) pa bitiem VAI 12 un 25 darbības 00001100 | 00011001 _________ 00011101 = 29 (aiz komata)

2. piemērs

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

Rezultāts

a = 12 b = 25 a | b = 29

Bitu līmeņa vai ir a = 12un b = 25dod 29.

3. Operators C ++ Bitwise XOR

Bitu līmeņa XOR ^ operators atgriež 1 , ja un tikai tad, ja viens no operandiem ir 1 . Tomēr, ja abi operandi ir 0 vai ja abi ir 1 , tad rezultāts ir 0 .

Šajā patiesības tabulā parādīts XOR operatora bitu darbība . Ļaujiet a un b būt diviem operandiem, kas var iegūt tikai bināras vērtības, ti, 1 vai 0 .

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

Apskatīsim divu veselu skaitļu 12 un 25 bitu darbību XOR :

 12 = 00001100 (binārā formātā) 25 = 00011001 (binārā formātā) Darbība pa 12 un 25 ar bitiem XOR 00001100 00011001 _________ 00010101 = 21 (aiz komata)

3. piemērs: Bitor 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; )

Rezultāts

 a = 12 b = 25 a b = 21

Bitu līmeņa XOR uz a = 12un b = 25dod 21.

4. Operators C ++ Bitwise Complement

Bitu papildinājuma operators ir vienots operators (darbojas tikai vienā operandā). To apzīmē ar to, ~ka bināros ciparus 1 no 0 maina uz 0 un no 0 uz 1 .

Bitwise papildinājums

Ir svarīgi atzīmēt, ka jebkura vesela skaitļa N bituma papildinājums ir vienāds ar - (N + 1) . Piemēram,

Apsveriet veselu skaitli 35 . Saskaņā ar noteikumu 35 bitu papildinājumam jābūt - (35 + 1) = -36 . Tagad redzēsim, vai mēs saņemam pareizo atbildi vai nē.

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

Pārvietojot jebkuru skaitli pa labi, mazāk nozīmīgie biti tiek izmesti, bet nozīmīgākie biti tiek aizstāti ar nulli.

vienu bitu Right Shift

Kā redzam no attēla iepriekš, mums ir 4 bitu skaitlis . Kad mēs veicam viena bita labās nobīdes darbību, katrs atsevišķais bits tiek nobīdīts pa labi par 1 bitu.

Tā rezultātā visvairāk labais bits tiek izmests, bet kreisais bits paliek brīvs. Šo vakanci aizstāj ar 0 .

6. C ++ kreisās maiņas operators

Kreiso maiņu operators pārslēdz visus bitus pret kreisi ar noteiktu skaitu noteiktajā bitiem . To apzīmē ar <<.

vienu bitu Left Shift

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 

Tā rezultātā, Bitu līmeņa kreisi-Shift operāciju 13 (un jebkuru citu numuru), var atšķirties būt atkarīgs no tā, cik bitu viņi pārstāv.

Tā kā 32 bitu attēlojumā ir daudz vairāk bitu, kurus var pārvietot pa kreisi, salīdzinot ar 4 bitu attēlojumu.

Interesanti raksti...