Sams Teach Yourself C++ in 21 Days

(singke) #1
Bits, Bytes, and Nybbles ................................................................................

After the decision is made to represent truth and falsehood with 1s and 0s,binary digits
(or bits) become very important. Because early computers could send eight bits at a time,
it was natural to start writing code using 8-bit numbers—called bytes.

812 Appendix A

With eight binary digits, you can represent up to 256 different values. Why? Examine the
columns: If all 8 bits are set (1), the value is 255. (128+64+32+16+8+4+2+1) If none is
set (all the bits are clear or zero), the value is 0. 0–255 is 256 possible states.

What’s a KB?..................................................................................................

It turns out that 2^10 (1,024) is roughly equal to 10^3 (1,000). This coincidence was too
good to miss, so computer scientists started referring to 2^10 bytes as 1K or 1 kilobyte,
based on the scientific prefix of kilo for thousand.
Similarly, 1024*1024 (1,048,576) is close enough to one million to receive the designa-
tion 1MB or 1 megabyte, and 1,024 megabytes is called 1 gigabyte (giga implies thou-
sand-million or billion). Finally, 1,024 gigabytes is called a terabyte.

Binary Numbers..............................................................................................

Computers use patterns of 1s and 0s to encode everything they do. Machine instructions
are encoded as a series of 1s and 0s and interpreted by the fundamental circuitry.
Arbitrary sets of 1s and 0s can be translated back into numbers by computer scientists,
but it would be a mistake to think that these numbers have intrinsic meaning.
For example, the Intel 8086 chipset interprets the bit pattern 1001 0101 as an instruction.
You certainly can translate this into decimal (149), but that number per se has no meaning.
Sometimes, the numbers are instructions, sometimes they are values, and sometimes they
are codes. One important standardized code set is ASCII. In ASCII, every letter and
punctuation is given a seven-digit binary representation. For example, the lowercase let-
ter “a” is represented by 0110 0001. This is not a number, although you can translate it
to the number 97 in base 10 (64+32+1). It is in this sense that people say that the letter
“a” is represented by 97 in ASCII; but the truth is that the binary representation of 97,
01100001, is the encoding of the letter “a,” and the decimal value 97 is a human conve-
nience.

NOTE Half a byte (4 bits) is called a nybble!

29 0672327112_app_a.qxd 11/19/04 12:30 PM Page 812

Free download pdf