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.
بايثون يمكنها التحويل لك
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.
البتات الثابتة والفرط
- تحتفظ الحاسوب بعدد ثابت من البتات لتخزين الرقم.
- باستخدام 4 بتات يمكنك تخزين
0إلى15— أي2**4 = 16قيم مختلفة. - إذا كان الناتج كبيراً جداً، فإنه يدور عائداً إلى
0. هذا يسمى الفرط (Overflow).
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.
الآن جرب بنفسك
- اكتب كودك أدناه واضغط على تشغيل لرؤية المخرجات.
- اضغط على تحقق من الإجابة لاختبارها.
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 لإظهار كيف تدور القيمة (Overflow).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.