In the Week 2 notes, I demonstrate how a program running on a computer might represent integer (whole number) values greater than or equal to 0 in a byte (eight bits). A question that may occur to you is why, if the number of integers available in an 8-bit representation is 28 = 256, do I say that the top number you can represent is 255? The answer has to do with the stipulation that the program being imagined is representing integers greater than or equal to 0. That means that the first number our bit pattern is required to represent isn't one, it's zero. Thus, the bit pattern
00000000
is used to represent the value zero,
00000001
is used to represent the value one,
00000010
is used to represent the value two,
00000011
is used to represent the value three,
00000100
is used to represent the value four,
00000101
is used to represent the value five, and so on. In this system, as also mentioned in the notes, each position in the bit pattern is arbitrarily assigned a weight that is a power of two, starting with a weight of 20 for the rightmost column and ending with a weight of 27 in the leftmost column. Expressed as decimal values, these powers of two are
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Thus, to choose a bit pattern more or less at random...
10011101
... we determine its value, in decimal, by adding up the corresponding column weights for every bit with a value of 1 (ignoring those bits with a value of 0):
1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
So that's 128 + 16 + 8 + 4 + 1 = 157
Now, as should be readily evident, the highest value that can be represented in 8 bits using this system is achieved by turning all the bits on, i.e., giving them all the value 1:
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Adding up the weights now gives us 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 (and NOT 256!). So, this particular system of assigning integers to bit patterns gives us 256 possible number values with a range 0 to 255.
Here's an extra tidbit that you can chew on: In this system, all bit patterns ending with a 1 in the rightmost column represent odd integers, and all bit patterns ending with a 0 in the rightmost column represent even integers.