logo

Operator pe biți în C

Operatorii pe biți sunt operatorii utilizați pentru a efectua operațiunile asupra datelor la nivel de biți. Când efectuăm operațiunile pe biți, atunci este cunoscută și ca programare la nivel de biți. Este format din două cifre, fie 0, fie 1. Este folosit în principal în calcule numerice pentru a face calculele mai rapide.

Avem diferite tipuri de operatori pe biți în limbajul de programare C. Următoarea este lista operatorilor pe biți:

Operator Înţeles operator
& operator AND pe biți
| Operator SAU pe biți
^ Operator OR exclusiv pe biți
~ Operatorul complementar al unu (operator unar)
<< Operator de schimbare la stânga
>> Operator schimbare dreapta

Să ne uităm la tabelul de adevăr al operatorilor pe biți.

X ȘI 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

Operatorul AND pe biți

Operatorul AND pe biți este notat cu un singur semn și (&). Doi operanzi întregi sunt scrieți pe ambele părți ale operatorului (&). Dacă biții corespunzători ambilor operanzi sunt 1, atunci ieșirea operației AND pe biți este 1; în caz contrar, rezultatul ar fi 0.

De exemplu,

 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 

După cum putem observa din rezultatul de mai sus, biții din ambele variabile sunt comparați unul câte unul. Dacă bitul ambelor variabile este 1, atunci rezultatul ar fi 1, în caz contrar 0.

cum se convertesc șirul în int în java

Să înțelegem operatorul AND pe biți prin 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; } 

În codul de mai sus, am creat două variabile, adică „a” și „b”. Valorile lui „a” și „b” sunt 6 și, respectiv, 14. Valoarea binară a lui „a” și „b” este 0110 și, respectiv, 1110. Când aplicăm operatorul AND între aceste două variabile,

a ȘI b = 0110 și& 1110 = 0110

Ieșire

Operator pe biți în C

Operator SAU pe biți

Operatorul SAU pe biți este reprezentat printr-un singur semn vertical (|). Doi operanzi întregi sunt scriși pe ambele părți ale simbolului (|). Dacă valoarea biților a oricăruia dintre operanzi este 1, atunci rezultatul va fi 1, în caz contrar 0.

De exemplu,

 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 

După cum putem observa din rezultatul de mai sus, biții ambilor operanzi sunt comparați unul câte unul; dacă valoarea oricărui bit este 1, atunci ieșirea ar fi 1, în caz contrar, 0.

Să înțelegem operatorul OR pe biți printr-un program.

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

Ieșire

Operator pe biți în C

Operator OR exclusiv pe biți

Operatorul SAU exclusiv pe biți este notat cu simbolul (^). Doi operanzi sunt scrieți pe ambele părți ale operatorului SAU exclusiv. Dacă bitul corespunzător al oricăruia dintre operanzi este 1, atunci rezultatul va fi 1, în caz contrar 0.

De exemplu,

 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 

După cum putem observa din rezultatul de mai sus, biții ambilor operanzi sunt comparați unul câte unul; dacă valoarea biților corespunzătoare a oricăruia dintre operanzi este 1, atunci ieșirea ar fi 1, altfel 0.

Să înțelegem operatorul OR exclusiv pe biți printr-un program.

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

Ieșire

Operator pe biți în C

Operator complement pe biți

Operatorul de complement pe biți este cunoscut și ca operatorul de complement. Este reprezentată de simbolul tilde (~). Este nevoie de un singur operand sau variabilă și efectuează operația de complement pe un operand. Când aplicăm operația de complement pe orice biți, atunci 0 devine 1 și 1 devine 0.

dateformat.format java

De exemplu,

 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 

După cum putem observa din rezultatul de mai sus că, dacă bitul este 1, atunci acesta este schimbat la 0, altfel 1.

Să înțelegem operatorul complement printr-un 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; } 

Ieșire

Operator pe biți în C

Operatori de deplasare pe biți

În programarea C există două tipuri de operatori de deplasare pe biți. Operatorii de schimbare pe biți vor deplasa biții fie pe partea stângă, fie pe partea dreaptă. Prin urmare, putem spune că operatorul de deplasare pe biți este împărțit în două categorii:

  • Operator de schimbare la stânga
  • Operator de schimbare la dreapta

Operator de schimbare la stânga

Este un operator care mută numărul de biți în partea stângă.

Sintaxa operatorului de deplasare la stânga este dată mai jos:

 Operand &lt;&lt; n 

Unde,

Operandul este o expresie întreagă pe care aplicăm operația de deplasare la stânga.

n este numărul de biți care trebuie deplasați.

În cazul operatorului de deplasare la stânga, „n” biți vor fi deplasați pe partea stângă. Biții „n” din partea stângă vor fi scoși, iar „n” biți din partea dreaptă vor fi umpluți cu 0.

De exemplu,

primavara mvc
 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>

Unde,

Operandul este o expresie întreagă pe care aplicăm operația de deplasare la dreapta.

N este numărul de biți care trebuie deplasați.

În cazul operatorului de deplasare la dreapta, „n” biți vor fi deplasați pe partea dreaptă. Biții „n” din partea dreaptă vor fi scoși, iar „n” biți din partea stângă vor fi umpluți cu 0.

De exemplu,

 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 

Să înțelegem printr-un 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; } 

Ieșire

Operator pe biți în C