The Standard ASCII Character Set
Bytes are frequently used to hold individual characters in a text document. In the ASCII character set, each binary value between 0 and 127 is given a specific character. Most computers extend the ASCII character set to use the full range of 256 characters available in a byte. The upper 128 characters handle special things like accented characters from common foreign languages.You can see the 127 standard ASCII codes below. Computers store text documents, both on diskand in memory, using these codes. For example, if you use Notepad in Windows 95/98 to create a text file containing the words, “Four score and seven years ago,” Notepad would use 1 byte of memory per character (including 1 byte for each space character between the words — ASCII character 32). When Notepad stores the sentence in a file on disk, the file will also contain 1 byte per character and per space.Try this experiment: Open up a new file in Notepad and insert the sentence, “Four score and seven years ago” in it. Save the file to disk under the name getty.txt. Then use the explorer and look at the size of the file. You will find that the file has a size of 30 bytes on disk: 1 byte for each character. If you add another word to the end of the sentence and re-save it, the file size will jump to the appropriate number of bytes. Each character consumes a byte.If you were to look at the file as a computer looks at it, you would find that each byte contains not a letter but a number — the number is the ASCII code corresponding to the character (see below). So on disk, the numbers for the file look like this:
F o u r a n d s e v e n 70 111 117 114 32 97 110 100 32 115 101 118 101 110
By looking in the ASCII table, you can see a one-to-one correspondence between each character and the ASCII code used. Note the use of 32 for a space — 32 is the ASCII code for a space. We could expand these decimal numbers out to binary numbers (so 32 = 00100000) if we wanted to be technically correct — that is how the computer really deals with things.The first 32 values (0 through 31) are codes for things like carriage return and line feed. The space character is the 33rd value, followed by punctuation, digits, uppercase characters and lowercase characters. To see all 127 values, check out Unicode.org’s chart.
Byte Prefixes and Binary Math
When you start talking about lots of bytes, you get into prefixes like kilo, mega and giga, as in kilobyte, megabyte and gigabyte (also shortened to K, M and G, as in Kbytes, Mbytes and Gbytes or KB, MB and GB). The following table shows the binary multipliers:
2^10 = 1,024
2^20 = 1,048,576
2^30 = 1,073,741,824
2^40 = 1,099,511,627,776
2^50 = 1,125,899,906,842,624
2^60 = 1,152,921,504,606,846,976
2^70 = 1,180,591,620,717,411,303,424
2^80 = 1,208,925,819,614,629,174,706,176
You can see in this chart that kilo is about a thousand, mega is about a million, giga is about a billion, and so on. So when someone says, “This computer has a 2 gig hard drive,” what he or she means is that the hard drive stores 2 gigabytes, or approximately 2 billion bytes, or exactly 2,147,483,648 bytes. How could you possibly need 2 gigabytes of space? When you consider that one CD holds 650 megabytes, you can see that just three CDs worth of data will fill the whole thing! Terabyte databases are fairly common these days, and there are probably a few petabyte databases floating around the Pentagon by now.
Binary math works just like decimal math, except that the value of each bit can be only 0 or 1. To get a feel for binary math, let’s start with decimal addition and see how it works. Assume that we want to add 452 and 751:
452 + 751 --- 1203
To add these two numbers together, you start at the right: 2 + 1 = 3. No problem. Next, 5 + 5 = 10, so you save the zero and carry the 1 over to the next place. Next, 4 + 7 + 1 (because of the carry) = 12, so you save the 2 and carry the 1. Finally, 0 + 0 + 1 = 1. So the answer is 1203.
Binary addition works exactly the same way:
010 + 111 --- 1001
Starting at the right, 0 + 1 = 1 for the first digit. No carrying there. You’ve got 1 + 1 = 10 for the second digit, so save the 0 and carry the 1. For the third digit, 0 + 1 + 1 = 10, so save the zero and carry the 1. For the last digit, 0 + 0 + 1 = 1. So the answer is 1001. If you translate everything over to decimal you can see it is correct: 2 + 7 = 9.
To see how boolean addition is implemented using gates, see How Boolean Logic Works.
To sum up, here’s what we’ve learned about bits and bytes:
- Bits are binary digits. A bit can hold the value 0 or 1.
- Bytes are made up of 8 bits each.
- Binary math works just like decimal math, but each bit can have a value of only 0 or 1.
There really is nothing more to it — bits and bytes are that simple.