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.
Siirtooperaattorit luokitellaan kahteen tyyppiin bittien siirtopaikan perusteella.
- Vasen vuorooperaattori
- 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 (' Enter a positive number: '); scanf (' %d', &num); // use left shift operator to shift the bits num = (num << 2); // It shifts two bits at the left side printf ('
After shifting the binary bits to the left side. '); printf ('
The new value of the variable num = %d', 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 << 2); printf ('
After shifting the binary bits to the left side. '); printf ('
The new value of the unsigned variable num = %d', 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 (' Enter a positive number: '); scanf (' %d', &num); printf (' No. of binary bits shifted to the left side: '); scanf (' %d', &bit); // use left shift operator to shift the bits num = (num << bit); printf ('
After shifting the bits to the left side. '); printf ('
The new value of the num = %d', 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 >> 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 (' Enter a positive number: '); scanf (' %d', &num); // use right shift operator to shift the bits num = (num >> 2); // It shifts two bits at the right side printf ('
After shifting the binary bits to the right side. '); printf ('
The new value of the variable num = %d', 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 >> 2); printf ('
After shifting the binary bits to the right side. '); printf ('
The new value of the unsigned variable num = %d', 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 (' Enter a positive number: '); scanf (' %d', &num); printf (' No. of binary bits shifted to the right side: '); scanf (' %d', &bit); // use right shift operator to shift the bits num = (num >> bit); printf ('
After using the right shift operator to shift the bits at the right side. '); printf ('
New value of the num = %d', 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
)>