Thursday, February 17, 2011

Also, bytes

A single bit can represent two distinct "things," as we talked about in the previous post, but what about collections of bits? How many things can 2 bits represent? 8 bits? 1000 bits? 0 bits? The formula to figure this out is given by the exponential formula 2^x, where x is the number of bits. The caret symbol denotes that what follows should be considered as superscript, so 2^x means "two raised to the x power." A single bit gives you 2^1=2 things you can represent. 2^10=2x2x2x2x2x2x2x2x2x2=1024 things you can represent. 2^0=1 thing you can represent (just that one thing, no options, no either/or). A byte is a collection of 8 bits, giving us 2^8=2x2x2x2x2x2x2x2=256 possible things you can represent with one byte.

Unless I mention otherwise, I'm going to talk about bits and bytes from here on out in terms of binary numbers. One bit can represent the numbers 0b0 and 0b1. Whenever I talk about binary numbers I'll start it off with "0b" first (that's a zero followed by the letter 'b'). If I omit the "0b," then I'm talking about regular decimal numbers that we're all so used to. The 'b' in "0b" stands for "binary," so that's an easy way to remember that. I am always going to use binary numbers to represent bit strings. A bit string is just a series of bits of any length. A byte is a bit string of length 8.

Here are two example bit strings for two different bytes: 0b1010 1010 and 0b1111 1111 (I added a space every 4 digits just for readability's sake, and remember, the "0b" at the beginning just means it's in binary). We can consider these as binary numbers, with each bit representing a digit in the binary number system, and when we look at all the bits together we can determine the value of the number. Or, like I mentioned last time, we can give arbitrary meaning to each of those bits and treat them as separate entities (not part of a larger binary number) that each tell us something about the computer, like which physical switches are turned on or which lights are blinking. The human designing the system gets to decide how all of the bytes in the system will be used.

If a byte is a collection of 8 bits, and represents a binary number, then its low value would be 0b0000 0000, and its high value would be 0b1111 1111. 0b0000 0000 is the same as the decimal number 0, and 0b1111 1111 is the same as the decimal number 255. So a single byte can represent numbers in the range of 0-255, or in other words it can represent 256 distinct numbers (0 counts as a number).

Back in the day when we only had 8-bit computers, mathematical operations could only be done directly on numbers of single-byte size, so only on values ranging from 0-255. In order to represent larger numbers and do mathematical operations on larger numbers, programmers had to cleverly build those larger operations out of multiple uses of smaller operations. As 16-, 32-, and 64-bit computers were developed then we no longer had to use those tricks to perform math on large numbers because the computer now has hardware to do those operations directly. A modern 64-bit computer can represent integers in the range of 0-18,446,744,073,709,551,615, so you only have to use those tricks from former years if you want to use numbers larger than that.

Next time we'll look at how to do basic arithmetic on binary numbers, how it's different from arithmetic on decimal numbers, and especially how it's exactly the same.

No comments:

Post a Comment