Vuorooperaattorit C

Vuorooperaattorit C

Tässä osiossa käsitellään Bitwise shift -operaattoreita c-ohjelmointikielessä. Bitwise shift -operaattoria käytetään siirtämään binääribittejä joko vasempaan tai oikeaan suuntaan ohjelman vaatimuksen mukaan.

Vuorooperaattorit C

Siirtooperaattorit luokitellaan kahteen tyyppiin bittien siirtopaikan perusteella.

  1. Vasen vuorooperaattori
  2. Oikean vuoron kuljettaja

Vasen vuorooperaattori

Vasemmanpuoleinen siirtooperaattori on eräänlainen Bitwise shift -operaattori, joka suorittaa operaatioita binääribiteille. Se on binäärioperaattori, joka vaatii kaksi operandia siirtääkseen tai siirtääkseen bittien sijaintia vasemmalle ja lisäämään nollia oikealle syntyvään tyhjään tilaan bittien siirron jälkeen.

Syntaksi

 var_name << no_of_position  

Yllä olevassa syntaksissa var_name edustaa kokonaislukumuuttujan nimeä, jossa vasemmalle siirretään ( < <) operation is to be performed shift the binary bits at left side. and no_of_position variable represents number of placed or shifted in other words, operator shifts first operand on side by defined second operand. < p>

Esimerkiksi kokonaislukumuuttujan num arvo on 22 ja sen binäärimuoto on 10110. Nyt siirretään vasemmalle shift-operaattorilla binääribitit 2, num = num < < 2 yhtä suuri kuin num = num * (2 ^2). Ja luvun uusi arvo on 22* (2 ^ 2) = 88, mikä on yhtä suuri kuin binäärimuoto 1011000.

Esimerkki 1: Ohjelma, joka havainnollistaa vasemman vaihdon operaattorin käyttöä C:ssä

 #include int main () { // declare local variable int num; printf (&apos; Enter a positive number: &apos;); scanf (&apos; %d&apos;, &amp;num); // use left shift operator to shift the bits num = (num &lt;&lt; 2); // It shifts two bits at the left side printf (&apos; 
 After shifting the binary bits to the left side. &apos;); printf (&apos; 
 The new value of the variable num = %d&apos;, num); return 0; }  

Lähtö

 Enter a positive number: 25 After shifting the binary bits to the left side. The new value of the variable num = 100  

Esimerkki 2: Ohjelma käyttää vasen vaihto -operaattoria C:n allekirjoittamattomissa int-tiedoissa

 #include int main () { // declare local variable unsigned int num = 0xff; // use left shift operator to shift the bits num = (num &lt;&lt; 2); printf (&apos; 
 After shifting the binary bits to the left side. &apos;); printf (&apos; 
 The new value of the unsigned variable num = %d&apos;, num); return 0; }  

Lähtö

 After shifting the binary bits to the left side. The new value of the unsigned variable num = 1020  

Esimerkki 3: Ohjelma syöttää käyttäjän positiivisen luvun suorittaakseen Vasemman siirtooperaation

 #include int main () { // declare local variable int num, bit; printf (&apos; Enter a positive number: &apos;); scanf (&apos; %d&apos;, &amp;num); printf (&apos; No. of binary bits shifted to the left side: &apos;); scanf (&apos; %d&apos;, &amp;bit); // use left shift operator to shift the bits num = (num &lt;&lt; bit); printf (&apos; 
 After shifting the bits to the left side. &apos;); printf (&apos; 
 The new value of the num = %d&apos;, num); return 0; }  

Lähtö

 Enter a positive number: 40 No. of binary bits shifted to the left side: 4 After shifting the bits to the left side. The new value of the num = 640  

Yllä olevassa esimerkissä käyttäjän määrittämän positiivisen luvun 40 binääribitti on 101000. Tämän jälkeen otamme numeroksi 4 vasemman puolen binääribittien siirtämiseksi. Ja sitten vasen-siirtooperaattori siirtää 4 binaaribittiä vasemmalle puolelle, ja sitten oikealle puolelle luodaan tilaa, joka täytetään tai lisätään 4 nollalla oikealle puolelle, joka palauttaa binääriarvon 1010000000, joka vastaa desimaaliluku 640.

Oikean vaihteen kuljettaja

Oikean siirtooperaattori on bittikohtaisen siirtooperaattorin tyyppi, jota käytetään siirtämään bittejä oikealla puolella, ja se esitetään kaksoisnuolisymbolina (>>). Vasemman siirtooperaattorin tavoin myös Oikean siirtooperaattori vaatii kaksi operandia siirtämään bittejä oikealla puolella ja lisäämään sitten nollat ​​vasemmalle puolelle luotuun tyhjään tilaan bittien siirron jälkeen.

Syntaksi

 var_name &gt;&gt; no_of_position  

Yllä olevassa syntaksissa var_name edustaa kokonaislukumuuttujaa, jolle siirto oikealle (>>) tulee suorittaa oikean puolen binääribittien siirtämiseksi. Ja no_of_position -muuttuja edustaa oikealle sijoitettavien tai siirrettävien bittien määrää. Toisin sanoen oikealle siirtooperaattori siirtää ensimmäisen operandin binääribittejä oikealla puolella määrittelemällä bittien kokonaismäärän toiselle operandille.

Esimerkki 1: Ohjelma, joka havainnollistaa oikean vaihdon operaattorin käyttöä C:ssä

 #include int main () { // declare local variable int num; printf (&apos; Enter a positive number: &apos;); scanf (&apos; %d&apos;, &amp;num); // use right shift operator to shift the bits num = (num &gt;&gt; 2); // It shifts two bits at the right side printf (&apos; 
 After shifting the binary bits to the right side. &apos;); printf (&apos; 
 The new value of the variable num = %d&apos;, num); return 0; }  

Lähtö

 Enter a positive number: 25 After shifting the binary bits to the right side. The new value of the variable num = 6  

Esimerkki 2: Ohjelma käyttää Right Shift -operaattoria C:n allekirjoittamattomissa int-tiedoissa

 #include int main () { // declare local variable unsigned int num = 0xff; // use right shift operator to shift the bits num = (num &gt;&gt; 2); printf (&apos; 
 After shifting the binary bits to the right side. &apos;); printf (&apos; 
 The new value of the unsigned variable num = %d&apos;, num); return 0; }  

Lähtö

 After shifting the binary bits to the right side. The new value of the unsigned variable num = 63  

Esimerkki 3: Ohjelma syöttää käyttäjän positiivisen luvun oikealle siirtooperaattorin suorittamiseksi

 #include int main () { // declare local variable int num, bit; printf (&apos; Enter a positive number: &apos;); scanf (&apos; %d&apos;, &amp;num); printf (&apos; No. of binary bits shifted to the right side: &apos;); scanf (&apos; %d&apos;, &amp;bit); // use right shift operator to shift the bits num = (num &gt;&gt; bit); printf (&apos; 
 After using the right shift operator to shift the bits at the right side. &apos;); printf (&apos; 
 New value of the num = %d&apos;, num); return 0; }  

Lähtö

 Enter a positive number: 40 No. of binary bits shifted to the right side: 4 After using the right shift operator to shift the bits to the right. The new value of the num = 2