logo

מפעילי משמרות ב-C

חלק זה ידון באופרטורים של משמרת Bitwise בשפת התכנות c. אופרטור העברה סיביות משמש להזזת הסיביות הבינאריות בכיוון שמאלה או ימינה בהתאם לדרישת התוכנית.

מפעילי משמרות ב-C

מפעילי משמרת מסווגים לשני סוגים על סמך מיקום ההסטה של ​​הביטים.

  1. מפעיל משמרת שמאלה
  2. מפעיל הילוך ימני

מפעיל משמרת שמאלה

אופרטור המשמרת השמאלי הוא סוג של אופרטור משמרת Bitwise, המבצע פעולות על הביטים הבינאריים. זהו אופרטור בינארי שדורש שני אופרנדים כדי להזיז או להזיז את מיקום הביטים לצד שמאל ולהוסיף אפסים לחלל הריק שנוצר בצד ימין לאחר הזזת הביטים.

תחביר

 var_name << no_of_position 

בתחביר שלמעלה, var_name מייצג את שם המשתנה השלם שבו ההזזה שמאלה (<<) 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>

לדוגמה, הערך של המשתנה השלם num הוא 22, וצורתו הבינארית היא 10110. כעת אנו משתמשים באופרטור ההזזה השמאלי כדי להזיז את הסיביות הבינאריות 2, המספר = num << 2 שווה ל-num = num * (2 ^2). והערך החדש של num הוא 22* (2 ^ 2) = 88, ששווה לצורה הבינארית 1011000.

דוגמה 1: תוכנית להדגמת השימוש באופרטור ה-Left Shift ב-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; } 

תְפוּקָה

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

דוגמה 2: תוכנית להשתמש באופרטור Left Shift בנתוני int לא חתומים של 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; } 

תְפוּקָה

מיון בחירת java
 After shifting the binary bits to the left side. The new value of the unsigned variable num = 1020 

דוגמה 3: תוכנית להזנת המספר החיובי מהמשתמש לביצוע אופרטור המשמרת שמאלה

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

תְפוּקָה

 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 

בדוגמה שלמעלה, הסיביות הבינאריות של המספר החיובי 40 המוגדר על ידי המשתמש הוא 101000. לאחר מכן, ניקח את 4 כמספר להזזת הסיביות הבינאריות בצד שמאל. ואז, האופרטור ההזזה שמאלה מעביר 4 ​​ביטים בינאריים בצד שמאל, ואז נוצר רווח בצד ימין, שמתמלא או מתווסף על ידי 4 אפסים לצד ימין שמחזיר את הערך הבינארי 1010000000, שהוא שווה ערך ל המספר העשרוני 640.

מפעיל משמרת ימין

אופרטור העברה ימני הוא סוג של אופרטור העברה בכיוון סיביות המשמש להזזת הביטים בצד ימין, והוא מיוצג כסמל החץ הכפול (>>). כמו האופרטור Left shift, גם האופרטור Right shift דורש שני אופרנדים כדי להזיז את הביטים בצד ימין ולאחר מכן להכניס את האפסים בחלל הריק שנוצר בצד שמאל לאחר הזזת הביטים.

תחביר

 var_name &gt;&gt; no_of_position 

בתחביר הנ'ל, var_name מייצג את המשתנה השלם שעליו יש לבצע את פעולת ההסטה הימנית (>>) כדי להזיז את הסיביות הבינאריות בצד ימין. והמשתנה no_of_position מייצג את מספר הביטים שיש למקם או להזיז לצד ימין. במילים אחרות, אופרטור ההזזה הימני מעביר את הסיביות הבינאריות של האופרנד הראשון בצד ימין על ידי הגדרת המספר הכולל של סיביות לאופרנד השני.

דוגמה 1: תוכנית להדגמת השימוש באופרטור Shift Right ב-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; } 

תְפוּקָה

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

דוגמה 2: תוכנית להשתמש באופרטור Shift Right בנתוני 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; } 

תְפוּקָה

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

דוגמה 3: תוכנית להזין את המספר החיובי מהמשתמש לביצוע אופרטור המשמרת ימינה

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

תְפוּקָה

 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