Bits and binary · ביטים וקידום בינארי
Bits: the smallest piece of data
- A bit is one digit that is either
0or1. - Computers store everything — numbers, text, images — as bits.
- One bit alone is small, so we group many bits together.
ביטים: החלק הקטן ביותר של נתונים
- ביט הוא ספרת אחד שהיא או
0או1. - מחשבים מאחסנים הכל – מספרים, טקסט, תמונות – כביטים.
- ביט בודד הוא קטן מאוד, ולכן אנו מקבוצים מספר רב של ביטים יחד.
Why computers use binary
- A computer chip has tiny switches. Each switch is on or off.
on=1andoff=0. So two states fit a chip perfectly.- Counting with only
0and1is called binary (base 2).
מדוע מחשבים משתמשים במערכת בין-ספרתית
- שבב מחשב מכיל מתגים זעירים. כל מתג הוא פעיל או כבוי.
on=1וoff=0. לכן שני מצבים מתאימים לשבב בצורה מושלמת.- ספירה עם רק
0ו1נקראת בינרי (בסיס 2).
How binary counts
- Our normal numbers use base 10 (digits
0–9). - Binary uses base 2. Each place is worth twice the place on its right.
- The places are
8 4 2 1. So1101means8 + 4 + 0 + 1 = 13.
איך ספירה דו-אחידית פועלת
- המספרים הרגילים שלנו משתמשים בבסיס 10 (ספרות
0–9). - בין-ארי משתמש בבסיס 2. כל מקום שווה פי שניים מהמקום מימינו.
- המקומות הם
8 4 2 1. לכן1101פירושו8 + 4 + 0 + 1 = 13.
place: 8 4 2 1
bits: 1 1 0 1 -> 8 + 4 + 0 + 1 = 13
Python can convert for you
bin(13)gives the string'0b1101'. The0bjust means "this is binary".int("1101", 2)reads"1101"as base 2 and gives13.- These two functions go in opposite directions.
Python יכול לבצע ההמרה עבורך
bin(13)נותן את המחרוזת'0b1101'. ה-0bפשוט אומר "זהו מערכת ספרותית דו-ספרתית".int("1101", 2)קורא את"1101"כבסיס 2 ונותן13.- הפונקציות הללו עובדות בכיוונים הפוכים.
print(bin(13)) # decimal -> binary
print(int("1101", 2)) # binary -> decimal
Fixed bits and overflow
- A computer keeps a number in a fixed number of bits.
- With 4 bits you can store
0to15— that is2**4 = 16different values. - If a result is too big, it wraps around back to
0. This is overflow.
ביטים קבועים והתפשטות (OverFlow)
- המחשב שומר על מספר בתוך כמות קבועה של ביטים.
- עם 4 ביטים ניתן לאחסן ערכים מ-
0עד15— כלומר2**4 = 16ערכים שונים. - אם התוצאה גדולה מדי, היא מתעגרת חזרה ל-
0. תופעה זו נקראת התפשטות.
print(2 ** 4) # how many values 4 bits hold = 16
print((15 + 3) % 16) # 18 is too big, so it wraps to 2
Common mistakes
nbits store2**ndifferent values, from0to2**n - 1.- Binary is base 2: each place is worth twice the one to its right.
טעויות נפוצות
nביטים מאחסנים2**nערכים שונים, מה-0ועד ה-2**n - 1.- מערכת דו-ספרתית היא בסיס 2: לכל מקום יש ערך כפול מזה שמימין לו.
Now you try
- Type your code below and press Run to see the output.
- Press Check answer to test it.
כעת תנסו בעצמכם
- הקלד את הקוד שלך למטה ולחץ על Run כדי לראות את התוצאה.
- לחץ על בדיקת תשובה כדי לבדוק אותה.
Binary counts in powers of 2 · קידום בינארי סופר בכוחות של 2
Each bit is a power of two; lit bits add up to the value. · כל ביט הוא כוח של שתיים; ביטים מודלקים מתווספים כדי ליצור את הערך.
Use bin(...) to print the binary form of 20. The output should look like 0b10100. · השתמש בbin(...) כדי להדפיס את הצורה הבינארית של 20. התוצאה צריכה להיראות כמו 0b10100.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
The binary string "110" is a number. Use int(s, 2) to print it as a normal (decimal) number. · המחרוזת הבינארית "110" היא מספר. השתמש בint(s, 2) כדי להדפיס אותו כמספר רגיל (עשרוני).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
A counter has 5 bits, so it holds 2**5 = 32 values. Store 2**5 in size. Then store (30 + 5) % size in result to show how the value wraps around (overflow). · סופר יש לו 5 ביטים, ולכן הוא אחסן 2**5 = 32 ערכים. אחסן 2**5 בsize. לאחר מכן אחסן (30 + 5) % size בresult כדי להראות איך הערך מתחבר על עצמו (שפלף).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.