Bits, binary, and bitwise operators · ביטים, בינארי ותפעולי ביט
Numbers are bits
- Inside the computer, every
intis a row of bits — each bit is a0or a1. - We usually read numbers in base 10, but the machine stores them in base 2 (binary).
- Bitwise operators let you look at and change individual bits.
מספרים הם ביטים
- בתוך המחשב, כל
intהוא שורה של ביטים — כל ביט הוא0או1. - אנחנו רואים בדרך כלל מספרים בעשרוני (בסיס 10), אך המאגר מאחסן אותם ב-בסיס 2 (בינארי).
- מקרי ביט מאפשרים לך לבדוק ולשנות ביטים בודדים.
Place value and powers of two
- In binary, each position is a power of two: 1, 2, 4, 8, 16, ...
- The byte
00010100is16 + 4 = 20. The rightmost bit is the 1s place. - An 8-bit byte can hold
0to255.
ערך מקום וחזקות של שניים
- בבינארי, כל מקום הוא חזקה של שניים: 1, 2, 4, 8, 16, ...
- הבייט
00010100הוא16 + 4 = 20. הביט הימני ביותר הוא מקום ה-1s. - בייט בן 8 ביטים יכול להכיל ערכים מ
0עד255.
Reading a bit with >> and & 1
- The shift
n >> imoves the bits right byiplaces, so bitilands in the 1s place. - The mask
& 1keeps only that lowest bit, giving0or1. - So
(n >> i) & 1reads the value of bit numberi.
קריאת ביט עם >> ו & 1
- ההזזה shift
n >> iמזיזה את הביטים ימינה בiמקומות, כך שביטiנופל במקום ה-1s. - המסכה mask
& 1משארת רק את הביט הנמוך ביותר, ומניבה0או1. - לכן
(n >> i) & 1קורא את הערך של ביט מספרi.
The bitwise operators
&(and),|(or),^(xor),~(not) combine numbers bit by bit.<<and>>shift bits left or right. Shifting left by 1 doubles a number.- These are the tools for masks, flags, and low-level tricks.
מפעילי ביט
&(AND),|(OR),^(XOR),~(NOT) משלבים מספרים ביט אחר ביט.- ⟨
<<⟩ ו->>מזזים ביטים שמאלה או ימינה. הזזה שמאלה ב-1 מכפילה במספר. - אלו הכלים למסקות, דגלים ולטרקים ברמת הבסיס.
#include <stdio.h>
int main(void) {
int n = 20;
for (int i = 7; i >= 0; i--) { // from the highest bit down
printf("%d", (n >> i) & 1);
}
printf("\n"); // 00010100
return 0;
}
Common mistakes
&is bitwise AND and&&is logical AND — do not confuse them.<< 1doubles a value;>> 1halves it.
טעויות נפוצות
&הוא AND בינארי ו&&הוא AND לוגי — אל תבלבנו ביניהם.<< 1מכפיל ערך פי שניים;>> 1מחלק אותו בחצי.
Now you try
- Use
(n >> i) & 1to read biti, and a loop to look at every bit. - For
count_bitsandget_bit, the parameter isunsigned int. Do not write amainfor those.
כעת תנסו בעצמכם
- השתמשו ב
(n >> i) & 1לקריאת ביטi, ובלולאה לבדיקת כל הביט. - עבור
count_bitsוget_bit, הפרמטר הואunsigned int. אל תכתבוmainעבורם.
Bitwise operators · תפעולי ביט
& | ^ ~ << >> work on each bit of a byte independently. · & | ^ ~ << >> פועלים על כל ביט של בייט באופן עצמאי.
Complete int count_bits(unsigned int n) so it returns how many bits of n are 1. Use & 1 and >> in a loop. Do not write a main. · השלם int count_bits(unsigned int n) כדי להחזיר כמה ביטים של n הם 1. השתמש ב& 1 וב>> בתוך לולאה. אל תכתוב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete int get_bit(unsigned int n, int i) so it returns bit number i of n (0 or 1). Use (n >> i) & 1. Do not write a main. · השלם int get_bit(unsigned int n, int i) כדי להחזיר את הביט במיקום i של n (0 או 1). השתמש ב(n >> i) & 1. אל תכתוב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
The byte n is given. In main, print its 8-bit binary form (most significant bit first), then a newline. For 20 that is 00010100. · הבייט ⟨⟩n נתון. ב-main, הדפס את הצורה הבינארית שלו בעלת ⟨⟩8 ביטים (הביט המשמעותי ביותר ראשונה), ולאחר מכן שורת חדשה. עבור ⟨⟩20 שהוא ⟨⟩00010100.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.