এই বইটি এখনো বাংলা ভাষায় পাওয়া যায় না। সংস্করণ: English।
Primary & Middle School Computer Science · শ্রেণি 1–9
11Bits and Bytes: Counting in Binary
It is getting dark, and Sami and Noor stand at the two ends of the playground, each with a torch. With one torch, Sami can send only two messages: light on means “come here”, light off means “stay there”. So he takes a second torch. Now there are four ways to hold them: both off, only the left one on, only the right one on, both on. Four messages! With a third torch they would have eight. A computer does exactly the same thing, with billions of tiny switches.
You already know
Sizes in a computer are counted in bytes; one byte holds one letter of plain text. The storage keeps information without electricity (Chapter 8).
11.1 Two states are enough
Inside a computer, every tiny part that holds information can be in one of two states only. A switch is on or off. A tiny area of a hard disk is magnetised one way or the other. A cell of flash memory holds an electric charge or does not. Two states are easy to make, easy to tell apart, and hard to mix up.
Definition 11.1 (Bit)
A bit is the smallest piece of information: one choice between two states. We write the two states 0 and 1.
11.2 Counting with two digits
How can numbers be written with only 0 and 1? Take five cards. The card on the right has 1 dot, the next one 2 dots, then 4, then 8, then 16: each card has twice as many dots as the card on its right. Each card is either face up, and its dots count, or face down, and it counts nothing.
With the five cards, any number from 0 (all face down) to 31 (all face up, ) can be shown, each in exactly one way.
Definition 11.2 (Binary)
Writing a number in binary is writing it with the digits 0 and 1 only, where each place is worth twice the place on its right: 1, 2, 4, 8, 16, and so on, from the right. A number written this way is a binary number.
Method 11.3 (Writing a number in binary)
Start with the largest card that is not more than the number: turn it face up and take its value away from the number. Go on with the next cards to the right: turn a card face up if its value is not more than what is left, and take it away. When nothing is left, read the cards: 1 for face up, 0 for face down.
Example 11.4 (Writing 22 and reading 10011)
For 22: 16 fits, 6 is left; 8 does not fit; 4 fits, 2 is left; 2 fits, 0 is left; 1 does not fit. So 22 is 10110 in binary. To read 10011, add the values of the places that hold a 1: .
Counting in binary is like counting with ordinary numbers, but each place is full after 1 instead of after 9: 0, 1, 10, 11, 100, 101, 110, 111, 1000…
11.3 A byte is eight bits
The byte of the chapter on memory and storage is made of bits: a byte is eight bits. With eight cards, worth 128, 64, 32, 16, 8, 4, 2 and 1, any number from 0 to 255 can be shown: . So one byte can hold any whole number from 0 to 255.
11.4 Each extra bit doubles the patterns
How many different patterns can a few bits make? One bit has two patterns: 0 and 1. Two bits have four: 00, 01, 10, 11. For three bits, take each of these four and add a 0 or a 1 at the end: that makes twice as many, eight patterns.
Proposition 11.5 (Doubling)
Each extra bit doubles the number of different patterns: 2 patterns with one bit, 4 with two, 8 with three, 16 with four, and so on. With eight bits, one byte, there are 256 patterns: the numbers 0 to 255.
Proof. Each pattern of bits gives two patterns of bits, one ending in 0 and one ending in 1. Every pattern of bits comes from exactly one pattern of bits: the one left when its last bit is taken away. So there are twice as many patterns of bits. ∎
Doubling grows very fast. Keep doubling from 2, eight times: 2, 4, 8, 16, 32, 64, 128, 256. Twenty more doublings would take us past 268 million.
11.5 Exercises
Exercise 11.1 ★
Write 5, 9 and 20 in binary with five cards.
Solution
Solution of Exercise 11.1.
: 00101. : 01001. : 10100.
Exercise 11.2 ★
Which number is written 11010 in binary?
Solution
Solution of Exercise 11.2.
.
Exercise 11.3 ★
What is the largest number that can be shown with four cards? With five? With eight?
Solution
Solution of Exercise 11.3.
Four cards: . Five cards: . Eight cards: .
Exercise 11.4 ★
How many different patterns can 4 bits make? Find it by doubling.
Exercise 11.5 ★
Count from 0 to 7 in binary, using three digits each time.
Solution
Solution of Exercise 11.5.
000, 001, 010, 011, 100, 101, 110, 111.
Exercise 11.6 ★
How many bits are needed to write 40 in binary? Try with the cards.
Solution
Solution of Exercise 11.6.
Five cards reach only 31, which is less than 40, so a sixth card, worth 32, is needed: , written 101000. Six bits.
Exercise 11.7 ★
These eight lamps show a byte (yellow is on). Which number is it?
Exercise 11.8 ★
Give three things, in a computer or elsewhere, that have exactly two states.
Solution
Solution of Exercise 11.8.
For example: a light switch (on or off); a tiny area of a hard disk (magnetised one way or the other); a cell of flash memory (charged or not); a door (open or shut); a coin on a table (heads or tails).
Exercise 11.9 ★★
The cards show 0111. Add 1: which cards turn over? What is the new number in binary, and in ordinary numbers?
Solution
Solution of Exercise 11.9.
is . Adding 1 gives 8: the cards 4, 2 and 1 turn face down and the card 8 turns face up. The new number is 1000 in binary, 8 in ordinary numbers. (It is like : every full place goes back to 0 and carries 1 to the left.)
Exercise 11.10 ★★
The teacher wants to give each of the 50 pupils of the school’s two classes a different code of 6 bits. Are there enough codes?
Solution
Solution of Exercise 11.10.
6 bits make 2, 4, 8, 16, 32, 64 patterns: 64 codes, more than 50. Yes, there are enough (and 14 to spare).
Exercise 11.11 ★★
Why must the value of each card be twice the value of the card on its right? What would go wrong with cards worth 1, 2, 3, 4 and 5?
Solution
Solution of Exercise 11.11.
Each card must be worth one more than all the cards on its right together: , and the next card is worth 8. Then every number can be shown, and in one way only. With cards worth 1, 2, 3, 4 and 5, some numbers can be shown in several ways (3 is the card 3, or ), so a pattern of cards would not tell a number in a single way; and with all five cards face up, the largest number would be 15 instead of 31.