Fall, 2021
Let’s define an unsigned 8-bit integer called a
. The maximum integer value that can be represented using 8 bits is 2^8 - 1 = 255.
We’ll also define a variable called i
, using MATLAB’s default numeric type, which is a double-precision floating-point value.
a = uint8(0); % unsigned 8-bit integer
i = 0; % default numeric type, double-precision floating point
Now let’s increment the value of a
and of i
by adding 1, and then print out the decimal value of i
, the decimal value of a
, and the hexadecimal and binary representations as well. We will increment starting at 0 and after 4 we will jump to 200 and then to 250 and see what happens when we go past 255:
fprintf('\n');
fprintf(' i a hex bin\n');
fprintf('---- ---- ----- ---------\n');
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 1; i = i +1;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 1; i = i +1;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 1; i = i +1;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 1; i = i +1;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 200; i = i + 200;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 50; i = i + 50;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 1; i = i +1;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 1; i = i +1;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
a = a + 1; i = i +1;
fprintf('%4d %4d %5x %9s\n', i, a, a, dec2bin(a,8));
Here is the output:
i a hex bin
---- ---- ----- ---------
0 0 0 00000000
1 1 1 00000001
2 2 2 00000010
3 3 3 00000011
4 4 4 00000100
204 204 cc 11001100
254 254 fe 11111110
255 255 ff 11111111
256 255 ff 11111111
257 255 ff 11111111
The value stored in the variable a
never goes past 255.
If this were a clock timer counting time, whatever algorithm using it would think that time is standing still. If an algorithm were using time to compute the velocity of some quantity (e.g. airspeed of an aircraft by dividing change-in-position by change-in-time) then we would experience a divide-by-zero error, and we would have big problems as velocity would appear to suddenly increase to infinity!
This is admittedly a bit of a contrived example, but, nevertheless, it’s important to remember: EVERY variable stored in a computer using a finite number of bits has a maximum representable value.