logo

Bitu operators C

Bitu operatori ir operatori, ko izmanto, lai veiktu darbības ar datiem bitu līmenī. Kad mēs veicam bitu darbības, to sauc arī par bitu līmeņa programmēšanu. Tas sastāv no diviem cipariem — 0 vai 1. To galvenokārt izmanto skaitliskos aprēķinos, lai aprēķini būtu ātrāki.

Mums ir dažāda veida bitu operatori C programmēšanas valodā. Šis ir bitu operatoru saraksts:

Operators Operatora nozīme
& Bitu UN operators
| Bitu VAI operators
^ Bitu ekskluzīvs VAI operators
~ Viena komplementa operators (vienkāršais operators)
<< Kreisās maiņas operators
>> Labās maiņas operators

Apskatīsim bitu operatoru patiesības tabulu.

X UN 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

Bitu UN operators

Bitu UN operators tiek apzīmēts ar vienu & zīmi. Abās operatora (&) pusēs ir ierakstīti divi veseli operandi. Ja abu operandu attiecīgie biti ir 1, tad bitu UN operācijas izvade ir 1; pretējā gadījumā izvade būtu 0.

Piemēram,

 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 

Kā mēs varam novērot no iepriekš minētā rezultāta, abu mainīgo biti tiek salīdzināti pa vienam. Ja abu mainīgo bits ir 1, tad izvade būtu 1, pretējā gadījumā 0.

Izmantojot programmu, sapratīsim bitu UN operatoru.

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

Iepriekš minētajā kodā esam izveidojuši divus mainīgos, t.i., “a” un “b”. “a” un “b” vērtības ir attiecīgi 6 un 14. “a” un “b” binārā vērtība ir attiecīgi 0110 un 1110. Lietojot operatoru UN starp šiem diviem mainīgajiem,

a UN b = 0110 && 1110 = 0110

Izvade

Bitu operators C

Bitu VAI operators

Operators bitu virzienā VAI tiek attēlots ar vienu vertikālu zīmi (|). Abās simbola (|) pusēs ir ierakstīti divi veseli skaitļi. Ja jebkura operanda bitu vērtība ir 1, tad izvade būtu 1, pretējā gadījumā 0.

Piemēram,

 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 

Kā redzams no iepriekš minētā rezultāta, abu operandu biti tiek salīdzināti pa vienam; ja jebkura bita vērtība ir 1, tad izvade būtu 1, pretējā gadījumā 0.

Sapratīsim bitu VAI operatoru, izmantojot programmu.

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

Izvade

Bitu operators C

Bitu ekskluzīvs VAI operators

Operators bitu virzienā ekskluzīvs VAI ir apzīmēts ar simbolu (^). Ekskluzīvā operatora VAI abās pusēs ir ierakstīti divi operandi. Ja jebkura operanda atbilstošais bits ir 1, tad izvade būtu 1, pretējā gadījumā 0.

Piemēram,

lateksa teksta izmēri
 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 

Kā redzams no iepriekš minētā rezultāta, abu operandu biti tiek salīdzināti pa vienam; ja jebkura operanda atbilstošā bita vērtība ir 1, tad izvade būtu 1, pretējā gadījumā 0.

Sapratīsim bitiski ekskluzīvo VAI operatoru, izmantojot programmu.

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

Izvade

Bitu operators C

Bitu komplementa operators

Bitu komplementa operators ir pazīstams arī kā viena komplementa operators. To apzīmē ar simbolu tilde (~). Tam nepieciešams tikai viens operands vai mainīgais, un operandam tiek veikta komplementa darbība. Ja mēs piemērojam komplementa darbību jebkuram bitam, tad 0 kļūst par 1 un 1 kļūst par 0.

Piemēram,

 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 

Kā mēs varam novērot no iepriekš minētā rezultāta, ja bits ir 1, tad tas tiek mainīts uz 0 un 1.

Sapratīsim komplementa operatoru, izmantojot programmu.

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

Izvade

Bitu operators C

Bitu maiņas operatori

C programmēšanā pastāv divu veidu bitu maiņas operatori. Bitu maiņas operatori pārvietos bitus kreisajā vai labajā pusē. Tāpēc mēs varam teikt, ka bitu maiņas operators ir sadalīts divās kategorijās:

  • Kreisās maiņas operators
  • Labās maiņas operators

Kreisās maiņas operators

Tas ir operators, kas novirza bitu skaitu uz kreiso pusi.

Tālāk ir norādīta kreisās puses maiņas operatora sintakse:

 Operand &lt;&lt; n 

kur,

Operands ir vesela skaitļa izteiksme, kurai mēs izmantojam kreisās puses maiņas darbību.

n ir nobīdāmo bitu skaits.

Operatora Left-shift gadījumā 'n' biti tiks pārvietoti kreisajā pusē. Kreisajā pusē esošie “n” biti tiks izlaisti, un labajā pusē esošie “n” biti tiks aizpildīti ar 0.

Piemēram,

 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,

Operands ir vesela skaitļa izteiksme, kurai mēs izmantojam labās puses nobīdes darbību.

N ir nobīdāmo bitu skaits.

java masīva kārtošana

Labās maiņas operatora gadījumā 'n' biti tiks pārvietoti labajā pusē. Labajā pusē esošie 'n' biti tiks izlaisti, un 'n' biti kreisajā pusē tiks aizpildīti ar 0.

Piemēram,

 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 

Sapratīsim caur programmu.

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

Izvade

Bitu operators C