Before we move on, I just wanted to take a moment to look at another peculiarity of using binary numbers, the concept of wrap-around or binary overflow.
Wrap-around is very similar to the odometer on a vehicle.Assume the odometer on our example vehicle has 6 digits to represent the mileage.As we clock up the distances, the value on the odometer climbs and eventually reaches the maximum number that those six digits can represent… 9999998, 999999….
At the point that the mileage moves through 999999, the next number that is displayed is 000000, all zeros and the odometer starts counting up again i.e. it wraps around.
Now, take that very same idea and apply it to the binary numbers we have been talking about.
Imagine that we have a single byte containing an unsigned number to which we are continuously adding 1.
Eventually we will get to the maximum number that those bits can collectively represent (111111112).
As we attempt to add the next 1, the resulting binary number would normally be 1000000002 but as we only have 8 bits in which to represent it the 1 that is carried into the 9th column is dropped, leaving the number 000000002.It effectively wraps around to the minimum value that can be stored.
This phenomenon also occurs when we count down.
As we keep subtracting one from our 8 bits of data and reach the smallest possible value (000000002 or 010) the next 1 that is subtracted causes all the bits to flip and we get 111111112 or 25510, the maximum number that can be stored.
Keep an eye out for this when you are reaching the maximum and minimum ranges that can be stored in a given number of bits.
Image Source: http://flic.kr/p/2bn9mu