„C ++“ operatoriai bitų pavidalu

Šioje pamokoje pavyzdžių pagalba sužinosime apie bitų kaupimo operatorius C ++ formatu.

C ++ operacijose bitų operacijomis operacijos atliekamos su sveikaisiais skaičiais atskirų bitų lygiu. Šios operacijos apima tikrųjų bitų testavimą, nustatymą arba perkėlimą. Pavyzdžiui,

 a & b; a | b;

Čia yra 6 bitų operatorių, įtrauktų į C ++, sąrašas.

operatorius apibūdinimas
& Bitais IR Operatorius
| ARBA operatorius
^ „Bitwise XOR“ operatorius
~ „Bitwise“ papildymo operatorius
<< Perkelkite kairįjį operatorių
>> Perkelkite dešinę operatorių

Šie operatoriai yra būtini, nes kompiuterio procesoriuje esantis aritmetinis-loginis vienetas (ALU) atlieka aritmetines operacijas bitų lygiu.

Pastaba: Bitinis operatorių gali būti naudojamas tik kartu charir intduomenų tipai.

1. „C ++“ bitais ir operatorius

Į Bitinis IR & operatorius grąžina 1 , jei ir tik jei abu operandai yra 1 . Kitu atveju jis grąžina 0 .

Ši lentelė parodo bitų IR operatoriaus veikimą . Tegul a ir b yra du operandai, galintys gauti tik dvejetaines reikšmes, ty 1 ir 0 .

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

Pastaba: aukščiau pateikta lentelė yra žinoma kaip „tiesos lentelė“, skirta operatoriui bitais IR .

Pažvelkime į dviejų sveikų skaičių 12 ir 25 bitų ir operaciją:

 12 = 00001100 (dvejetainiu būdu) 25 = 00011001 (dvejetainiu) // 12 ir 25 bitu ir dviem veiksmais 00001100 ir 00011001 _________ 00001000 = 8 (dešimtainiu kableliu)

1 pavyzdys: Bitais IR

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

Rezultatas

 a = 12 b = 25 a & b = 8

Ankstesniame pavyzdyje mes paskelbėme du kintamuosius a ir b. Čia atkreipkite dėmesį į eilutę,

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

Čia mes atliekame kintamuosius IR tarp kintamųjų a ir b.

2. „C ++“ operatorius, veikiantis pagal bitų dydį

Į Bitinis ARBA | operatorius grąžina 1 , jei bent vienas iš operandų yra 1 . Kitu atveju jis grąžina 0 .

Ši tiesų lentelė parodo bitų ARBA operatoriaus veikimą . Tegul a ir b yra du operandai, galintys gauti tik dvejetaines reikšmes, ty 1 arba 0 .

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

Pažvelkime į dviejų skaičių 12 ir 25 bitų ARBA operaciją :

12 = 00001100 (dvejetainiu būdu) 25 = 00011001 (dvejetainiu būdu) Bitais ARBA 12 ir 25 operacijos 00001100 | 00011001 _________ 00011101 = 29 (dešimtųjų tikslumu)

2 pavyzdys

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

Rezultatas

a = 12 b = 25 a | b = 29

Bitinis ARBAa = 12ir b = 25suteikia 29.

3. Operatorius „C ++ Bitwise XOR“

Bitinis XOR ^ operatorius grąžina 1 , jei ir tik jei vienas iš operandų yra 1 . Tačiau jei abu operandai yra 0 arba jei abu yra 1 , tada rezultatas yra 0 .

Ši tiesų lentelė parodo bitų XOR operatoriaus darbą. Tegul a ir b yra du operandai, galintys gauti tik dvejetaines reikšmes, ty 1 arba 0 .

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

Pažvelkime į dviejų sveikų skaičių 12 ir 25 bitų XOR operaciją:

 12 = 00001100 (dvejetainiu būdu) 25 = 00011001 (dvejetainiu) XOR operacija bitų pavidalu 12 ir 25 00001100 00011001 _________ 00010101 = 21 (dešimtųjų tikslumu)

3 pavyzdys: XOR bitais

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

Rezultatas

 a = 12 b = 25 a b = 21

Bitinis XORa = 12ir b = 25suteikia 21.

4. „C ++“ papildomas operatorius, veikiantis bitais

Bitų komplemento operatorius yra vienarūšis operatorius (veikia tik su vienu operandu). Tai žymima tuo, ~kad dvejetainiai skaitmenys 1 keičiami į 0 ir 0 į 1 .

Bitų papildymas

Svarbu pažymėti, kad bet kurio sveiko skaičiaus N bitų papildymas yra lygus - (N + 1) . Pavyzdžiui,

Apsvarstykite sveikąjį skaičių 35 . Pagal taisyklę, bitų papildymas 35 turėtų būti - (35 + 1) = -36 . Dabar pažiūrėkime, ar teisingai atsakysime, ar ne.

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

Kai bet kurį skaičių perkeliame į dešinę, mažiausiai reikšmingi bitai atmetami, o reikšmingiausi bitai pakeičiami nuliais.

vieną bitą „Right Shift“

Kaip matome iš aukščiau esančio paveikslėlio, turime 4 bitų skaičių . Kai atliekame vieno bitų dešiniojo poslinkio operaciją, kiekvienas atskiras bitas perkeliamas į dešinę po 1 bitą.

Todėl kairysis dešinysis bitas atmetamas, o kairysis - tuščias. Šią laisvą vietą keičia 0 .

6. „C ++“ kairiojo poslinkio operatorius

Kairysis SHIFT operatorius pastumia visas bitus į kairę tam tikru skaičiumi nurodytas bitai . Tai žymima <<.

vieną bitą „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 

Dėl to 13 (ir bet kokio kito skaičiaus) bitų kairiojo poslinkio operacija gali skirtis, atsižvelgiant į tai, kiek bitų jie vaizduoja.

Nes 32 bitų vaizdavime yra daug daugiau bitų, kuriuos galima perkelti į kairę, palyginti su 4 bitų vaizdavimu.

Įdomios straipsniai...