logo

Směnoví operátoři v C

Tato část se bude zabývat operátory bitového posunu v programovacím jazyce c. Operátor bitového posunu se používá k posunu binárních bitů buď ve směru doleva nebo doprava podle požadavku programu.

Směnoví operátoři v C

Operátory posunu jsou rozděleny do dvou typů na základě polohy posunu bitů.

  1. Operátor levé směny
  2. Operátor pravé směny

Operátor levé směny

Operátor posunu vlevo je typem operátoru bitového posunu, který provádí operace s binárními bity. Je to binární operátor, který vyžaduje dva operandy k posunutí nebo přesunutí pozice bitů na levou stranu a přidání nul do prázdného prostoru vytvořeného na pravé straně po posunutí bitů.

Syntax

 var_name << no_of_position 

Ve výše uvedené syntaxi představuje var_name celočíselný název proměnné, na které se posune doleva (<<) 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>

Například hodnota celočíselné proměnné num je 22 a její binární tvar je 10110. Nyní použijeme operátor posunu vlevo k posunutí binárních bitů 2, num = num << 2 se rovná num = num * (2 ^2). A nová hodnota num je 22* (2 ^ 2) = 88, což se rovná binárnímu tvaru 1011000.

Příklad 1: Program demonstrující použití operátoru Left Shift v C

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

Výstup

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

Příklad 2: Program pro použití operátoru Left Shift v datech unsigned int C

stream java filtru
 #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; } 

Výstup

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

Příklad 3: Program pro zadání kladného čísla od uživatele pro provedení operátoru řazení vlevo

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

Výstup

 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 

Ve výše uvedeném příkladu je binární bit uživatelem definovaného kladného čísla 40 101000. Poté vezmeme 4 jako číslo pro posunutí binárních bitů na levou stranu. A pak operátor posunu doleva posune 4 binární bity na levou stranu a pak se na pravé straně vytvoří prostor, který se vyplní nebo přidá 4 nuly na pravou stranu, což vrátí binární hodnotu 1010000000, což je ekvivalent desetinné číslo 640.

Operátor pravé směny

Operátor posunu vpravo je typ operátoru bitového posunu, který se používá k přesunutí bitů na pravé straně a je reprezentován jako symbol dvojité (>>) šipky. Stejně jako operátor posunu vlevo, operátor posunu vpravo také vyžaduje dva operandy k posunutí bitů na pravé straně a poté k vložení nul na prázdné místo vytvořené na levé straně po posunutí bitů.

Syntax

 var_name &gt;&gt; no_of_position 

Ve výše uvedené syntaxi představuje var_name celočíselnou proměnnou, na které se má provést operace posunu doprava (>>) pro posunutí binárních bitů na pravou stranu. A proměnná no_of_position představuje počet bitů, které mají být umístěny nebo posunuty na pravou stranu. Jinými slovy, operátor posunu vpravo posouvá binární bity prvního operandu na pravou stranu definováním celkového počtu bitů do druhého operandu.

Příklad 1: Program demonstrující použití operátoru Right Shift v C

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

Výstup

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

Příklad 2: Program pro použití operátoru Right Shift v datech unsigned int 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; } 

Výstup

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

Příklad 3: Program pro zadání kladného čísla od uživatele za účelem provedení operátoru posunu doprava

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

Výstup

 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