logo

Shift-operatoren in C

In deze sectie worden de Bitwise shift-operatoren in de programmeertaal c besproken. De bitsgewijze shift-operator wordt gebruikt om de binaire bits naar links of naar rechts te verschuiven, afhankelijk van de vereisten van het programma.

Shift-operatoren in C

Shift-operators worden in twee typen ingedeeld op basis van de verschuivende positie van de bits.

  1. Linker Shift Operator
  2. Rechter Shift Operator

Linker Shift Operator

De linker shift-operator is een type Bitwise shift-operator, die bewerkingen uitvoert op de binaire bits. Het is een binaire operator die twee operanden nodig heeft om de positie van de bits naar de linkerkant te verschuiven of te verplaatsen en nullen toe te voegen aan de lege ruimte die aan de rechterkant ontstaat na het verschuiven van de bits.

Syntaxis

 var_name << no_of_position 

In de bovenstaande syntaxis vertegenwoordigt var_name de naam van de gehele variabele waarop de linkerverschuiving (<<) 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>

De waarde van de gehele variabele num is bijvoorbeeld 22, en de binaire vorm ervan is 10110. Nu gebruiken we de linker shift-operator om de binaire bits 2 te verschuiven, de num = num << 2 gelijk aan de num = num * (2 ^2). En de nieuwe waarde van num is 22* (2 ^ 2) = 88, wat gelijk is aan de binaire vorm 1011000.

Voorbeeld 1: Programma om het gebruik van de Left Shift-operator in C te demonstreren

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

Uitvoer

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

Voorbeeld 2: Programma om de Left Shift-operator te gebruiken in de niet-ondertekende int-gegevens van de C

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

Uitvoer

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

Voorbeeld 3: Programma om het positieve getal van de gebruiker in te voeren om de Left shift-operator uit te voeren

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

Uitvoer

converteer byte-array naar string
 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 

In het bovenstaande voorbeeld is de binaire bit van het door de gebruiker gedefinieerde positieve getal 40 101000. Daarna nemen we 4 als getal om de binaire bits naar de linkerkant te verschuiven. En dan verschuift de operator naar links 4 binaire bits aan de linkerkant, en dan wordt er ruimte gecreƫerd aan de rechterkant, die wordt opgevuld of toegevoegd door 4 nullen aan de rechterkant, wat de binaire waarde 1010000000 retourneert, wat overeenkomt met het decimale getal 640.

Rechter shift operator

De rechter shift-operator is een soort bitsgewijze shift-operator die wordt gebruikt om de bits aan de rechterkant te verplaatsen, en wordt weergegeven als het dubbele (>>) pijlsymbool. Net als de Left shift-operator heeft de Right shift-operator ook twee operanden nodig om de bits aan de rechterkant te verschuiven en vervolgens de nullen in te voegen in de lege ruimte die aan de linkerkant ontstaat na het verschuiven van de bits.

Syntaxis

 var_name &gt;&gt; no_of_position 

In de bovenstaande syntaxis vertegenwoordigt var_name de gehele variabele waarop de rechtsverschuivingsoperatie (>>) moet worden uitgevoerd om de binaire bits aan de rechterkant te verschuiven. En de variabele no_of_position vertegenwoordigt het aantal bits dat naar de rechterkant moet worden geplaatst of verschoven. Met andere woorden, de rechterverschuivingsoperator verschuift de binaire bits van de eerste operand naar de rechterkant door het totale aantal bits voor de tweede operand te definiƫren.

Voorbeeld 1: Programma om het gebruik van de Right Shift-operator in C te demonstreren

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

Uitvoer

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

Voorbeeld 2: Programma om de Right Shift-operator te gebruiken in de niet-ondertekende int-gegevens van de C

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

Uitvoer

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

Voorbeeld 3: Programma om het positieve getal van de gebruiker in te voeren om de Right shift-operator uit te voeren

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

Uitvoer

 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