logo

Bitwise operatorius C

Bitiniai operatoriai yra operatoriai, naudojami operacijoms su duomenimis atlikti bitų lygiu. Kai atliekame bitines operacijas, tai taip pat žinoma kaip bitų lygio programavimas. Jį sudaro du skaitmenys – 0 arba 1. Jis daugiausia naudojamas skaitiniams skaičiavimams, kad skaičiavimai būtų greitesni.

C programavimo kalboje turime įvairių tipų bitų operatorių. Toliau pateikiamas bitinių operatorių sąrašas:

operatorius Operatoriaus reikšmė
& Bitų IR operatorius
| Bitų OR operatorius
^ Bitiškai išskirtinis OR operatorius
~ Vieno komplemento operatorius (vieninis operatorius)
<< Kairės pamainos operatorius
>> Dešinės pamainos operatorius

Pažiūrėkime į bitinių operatorių tiesos lentelę.

X IR X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1

Bitų IR operatorius

Bitų IR operatorius žymimas vienu ampersando ženklu (&). Abiejose operatoriaus (&) pusėse užrašomi du sveikieji operandai. Jei abiejų operandų atitinkami bitai yra 1, tai bitinės IR operacijos išvestis yra 1; kitu atveju išvestis būtų 0.

Pavyzdžiui,

 We have two variables a and b. a =6; b=4; The binary representation of the above two variables are given below: a = 0110 b = 0100 When we apply the bitwise AND operation in the above two variables, i.e., a&amp;b, the output would be: Result = 0100 

Kaip matome iš aukščiau pateikto rezultato, abiejų kintamųjų bitai lyginami po vieną. Jei abiejų kintamųjų bitas yra 1, tada išvestis būtų 1, kitu atveju 0.

Supraskime bitų IR operatorių per programą.

 #include int main() { int a=6, b=14; // variable declarations printf(&apos;The output of the Bitwise AND operator a&amp;b is %d&apos;,a&amp;b); return 0; } 

Aukščiau pateiktame kode sukūrėme du kintamuosius, ty „a“ ​​ir „b“. „a“ ir „b“ reikšmės yra atitinkamai 6 ir 14. Dvejetainė „a“ ir „b“ reikšmė yra atitinkamai 0110 ir 1110. Kai tarp šių dviejų kintamųjų taikome operatorių AND,

a IR b = 0110 && 1110 = 0110

Išvestis

Bitwise operatorius C

Bitų OR operatorius

Bitų OR operatorius pavaizduotas vienu vertikaliu ženklu (|). Abiejose simbolio (|) pusėse užrašyti du sveikieji operandai. Jei kurio nors operando bitų reikšmė yra 1, tada išvestis būtų 1, kitu atveju 0.

Pavyzdžiui,

 We consider two variables, a = 23; b = 10; The binary representation of the above two variables would be: a = 0001 0111 b = 0000 1010 When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output would be: Result = 0001 1111 

Kaip matome iš aukščiau pateikto rezultato, abiejų operandų bitai lyginami po vieną; jei kurio nors bito reikšmė yra 1, tada išvestis būtų 1, priešingu atveju 0.

Supraskime bitinį OR operatorių per programą.

 #include int main() int a=23,b=10; // variable declarations printf(&apos;The output of the Bitwise OR operator a 

Išvestis

Bitwise operatorius C

Bitiškai išskirtinis OR operatorius

Bitiškai išskirtinis ARBA operatorius žymimas (^) simboliu. Abiejose išskirtinio OR operatoriaus pusėse parašyti du operandai. Jei bet kurio operando atitinkamas bitas yra 1, tada išvestis būtų 1, kitu atveju 0.

Pavyzdžiui,

 We consider two variables a and b, a = 12; b = 10; The binary representation of the above two variables would be: a = 0000 1100 b = 0000 1010 When we apply the bitwise exclusive OR operator in the above two variables (a^b), then the result would be: Result = 0000 1110 

Kaip matome iš aukščiau pateikto rezultato, abiejų operandų bitai lyginami po vieną; jei bet kurio operando atitinkama bitų reikšmė yra 1, tada išvestis būtų 1, priešingu atveju 0.

Supraskime bitų išskirtinį OR operatorių per programą.

rom
 #include int main() { int a=12,b=10; // variable declarations printf(&apos;The output of the Bitwise exclusive OR operator a^b is %d&apos;,a^b); return 0; } 

Išvestis

Bitwise operatorius C

Bitinio komplemento operatorius

Bitinio komplemento operatorius taip pat žinomas kaip komplemento operatorius. Jį žymi simbolis tildė (~). Tam reikia tik vieno operando arba kintamojo ir atlieka operando papildymo operaciją. Kai bet kuriems bitams taikome komplemento operaciją, tada 0 tampa 1, o 1 tampa 0.

Pavyzdžiui,

 If we have a variable named &apos;a&apos;, a = 8; The binary representation of the above variable is given below: a = 1000 When we apply the bitwise complement operator to the operand, then the output would be: Result = 0111 

Kaip galime pastebėti iš aukščiau pateikto rezultato, jei bitas yra 1, tada jis pakeičiamas į 0 kitu 1.

Supraskime komplemento operatorių per programą.

 #include int main() { int a=8; // variable declarations printf(&apos;The output of the Bitwise complement operator ~a is %d&apos;,~a); return 0; } 

Išvestis

Bitwise operatorius C

Bitinio poslinkio operatoriai

C programavime egzistuoja dviejų tipų bitinio poslinkio operatoriai. Bitų poslinkio operatoriai perkels bitus kairėje arba dešinėje pusėje. Todėl galime pasakyti, kad bitų poslinkio operatorius yra suskirstytas į dvi kategorijas:

  • Kairysis pamainos operatorius
  • Dešinės pamainos operatorius

Kairysis pamainos operatorius

Tai operatorius, kuris perkelia bitų skaičių į kairę pusę.

Kairiojo poslinkio operatoriaus sintaksė pateikta žemiau:

 Operand &lt;&lt; n 

kur,

Operandas yra sveikųjų skaičių išraiška, kuriai taikome poslinkio į kairę operaciją.

n yra bitų, kuriuos reikia perkelti, skaičius.

Operatoriaus „Left-shift“ atveju „n“ bitai bus perkeliami kairėje pusėje. Kairėje pusėje esantys „n“ bitai bus iššokti, o dešinėje pusėje esantys „n“ bitai užpildyti 0.

Pavyzdžiui,

 Suppose we have a statement: int a = 5; The binary representation of &apos;a&apos; is given below: a = 0101 If we want to left-shift the above representation by 2, then the statement would be: a &lt;&lt; 2; 0101&lt;<2 = 00010100 < pre> <p> <strong>Let&apos;s understand through a program.</strong> </p> <pre> #include int main() { int a=5; // variable initialization printf(&apos;The value of a&lt;<2 is : %d ', a<<2); return 0; } < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/51/bitwise-operator-c-5.webp" alt="Bitwise Operator in C"> <p> <strong>Right-shift operator</strong> </p> <p>It is an operator that shifts the number of bits to the right side.</p> <p> <strong>Syntax of the right-shift operator is given below:</strong> </p> <pre> Operand &gt;&gt; n; </pre> <p> <strong>Where,</strong> </p> <p>Operand is an integer expression on which we apply the right-shift operation.</p> <p>N is the number of bits to be shifted.</p> <p>In the case of the right-shift operator, &apos;n&apos; bits will be shifted on the right-side. The &apos;n&apos; bits on the right-side will be popped out, and &apos;n&apos; bits on the left-side are filled with 0.</p> <p> <strong>For example, </strong> </p> <pre> Suppose we have a statement, int a = 7; The binary representation of the above variable would be: a = 0111 If we want to right-shift the above representation by 2, then the statement would be: a&gt;&gt;2; 0000 0111 &gt;&gt; 2 = 0000 0001 </pre> <p> <strong>Let&apos;s understand through a program.</strong> </p> <pre> #include int main() { int a=7; // variable initialization printf(&apos;The value of a&gt;&gt;2 is : %d &apos;, a&gt;&gt;2); return 0; } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/51/bitwise-operator-c-6.webp" alt="Bitwise Operator in C"> <hr></2></pre></2>

kur,

Operandas yra sveikųjų skaičių išraiška, kuriai taikome poslinkio į dešinę operaciją.

N yra bitų, kuriuos reikia perkelti, skaičius.

Jei operatoriaus poslinkis į dešinę, „n“ bitai bus perkelti į dešinę pusę. „n“ bitai dešinėje bus iššokti, o „n“ bitai kairėje užpildyti 0.

Pavyzdžiui,

 Suppose we have a statement, int a = 7; The binary representation of the above variable would be: a = 0111 If we want to right-shift the above representation by 2, then the statement would be: a&gt;&gt;2; 0000 0111 &gt;&gt; 2 = 0000 0001 

Supraskime per programą.

 #include int main() { int a=7; // variable initialization printf(&apos;The value of a&gt;&gt;2 is : %d &apos;, a&gt;&gt;2); return 0; } 

Išvestis

Bitwise operatorius C