Bits, Bytes and Binary Numbers
AP Computer Science Principles · Unit 2 — Data
Everything is bits, and bits are an abstraction
A bit (binary digit) is the smallest unit of data a computer stores: a single 0 or 1. Eight bits form a byte. Computing devices represent every kind of data — numbers, text, colours, sound, instructions — as sequences of bits. This is an abstraction: the same pattern 01000001 can be the number 65, the letter A in ASCII, or the red component of a pixel. The bits do not carry their own meaning; the program that reads them decides how to interpret them.
With $n$ bits there are exactly $2^n$ different patterns. One more bit doubles the number of patterns; it does not add a fixed amount. So 8 bits give 256 patterns and 10 bits give 1,024, enough to assign unique IDs to 1,000 students but not to 1,100. To find the fewest bits needed for $k$ different values, find the smallest $n$ with $2^n \ge k$.
In binary (base 2), each place is worth twice the place to its right. From the right, the place values are $2^0=1$, $2^1=2$, $2^2=4$, $2^3=8$, and so on. In decimal (base 10) each place is worth ten times the place to its right. The rightmost bit tells you whether a number is odd (1) or even (0); the leftmost bit of an $n$-bit number tells you whether the value is at least $2^{n-1}$.
Converting and comparing
Binary to decimal: write the place values above the bits and add those under a 1. For 11001000: $128+64+8=200$.
Decimal to binary: repeatedly take the largest place value that still fits. For 181: 128 fits (remainder 53), 64 does not, 32 fits (21), 16 fits (5), 8 does not, 4 fits (1), 2 does not, 1 fits (0). Reading the yes/no answers in order gives 10110101. An equivalent method divides by 2 repeatedly and reads the remainders from bottom to top.
Comparing binary numbers: leading zeros do not change the value, so 01101101 and 1101101 are the same number. Do not compare by length or by the number of 1s; convert, or line the numbers up at the right and compare from the leftmost position where they differ.
Programming languages store many numbers using a fixed number of bits, which limits the range of values. Some languages, such as the one assumed in the exam pseudocode, may represent integers with no fixed upper limit, constrained only by available memory. Whether a given language has a fixed range is a property of that language, and questions will tell you which assumption to use.
Worked example: Đổi cơ số và đếm số bit
(1) Convert 11010110 to decimal. (2) Convert 75 to an 8-bit binary number. (3) A school assigns each student a unique ID made of bits. What is the minimum number of bits needed for 1,100 students, and how many IDs would remain unused?
Solution
(1) Các bit 1 nằm ở vị trí 128, 64, 16, 4, 2: $128+64+16+4+2=214$.
(2) 75: 64 trừ được (còn 11); 32 không; 16 không; 8 được (còn 3); 4 không; 2 được (còn 1); 1 được (còn 0). Thêm bit 128 = 0 ở đầu cho đủ 8 bit: 01001011. Kiểm: $64+8+2+1=75$.
(3) $2^{10}=1024$ nhỏ hơn 1.100 nên 10 bit không đủ; $2^{11}=2048 \ge 1100$. Cần ít nhất 11 bit, và còn $2048-1100=948$ mã chưa dùng. Lưu ý cách nghĩ “1.100 cần 1.100/2 = 550 bit” hoặc “thêm 1 bit thêm 1 học sinh” đều sai vì số mẫu tăng theo cấp số nhân.
Common mistake
Bẫy “thêm bit là cộng thêm”. Đi từ 5 bit lên 8 bit không làm số mức tăng 3 hay tăng $\frac{8}{5}$ lần, mà tăng $2^3=8$ lần. Bẫy thứ hai: số lớn nhất biểu diễn được bằng $n$ bit là $2^n-1$, không phải $2^n$ — 8 bit tối đa 255. Bẫy thứ ba: nghĩ dãy bit dài hơn thì lớn hơn;00011111 (31) nhỏ hơn 100000 (32).