Unsigned Integers of 8 bits. A uint8 data type contains all whole numbers from 0 to 255. As with all unsigned numbers, the values must be non-negative. Uint8's are mostly used in graphics (colors are always non-negative).
uints are a type of integer. The u-int-8 stands for:
Which means: All values are positive (no negative "sign" allowed).
Which means: All values are whole numbers, 0,1,2,3...
Only 8 bits of information:
Which means: the max value is 255 and the min value is 0.
Consider the variables a and b to be of type integer (NOT double).
Thus, what is:
The answer is that in integer math, we "Truncate" the remaining float, thus 3/2 is 1. NOT 1.5 but ONE. Everything after the integer part of the number is discarded.
What if we take an integer and divide it by a floating point number?
The answer is that the entire expression is evaluated as if the 5 was a floating point number as well.
uint8 variables use the integer math rules!
Overflow is defined as having "too much information" to store in the given size of bucket.
As uint8's can only support numbers from 0 to 255, we have a problem. This problem is called overflow. Consider the following unit8 addition:
100 + 100
The answer is 200.... thats easy. Now consider:
200 + 200
The answer is .... 255... hmmm.... The reason for this is because we have reached the MAX value possible for our 8 bits and cannot represent anything larger.
Say we have three unit8 variables (a,b,c) and we want to average them. What happens if we do:
a = 100; b = 100; c = 100; average = (a + b + c) / 3;
The average value will be 85 (255/3) not 100. This can cause some major problems when programmers are unaware.
How would we go about calculating the actual average? Three strategies present themselves:
Modify the mathematical expression, such that it never creates overflow:
answer = (a/3) + (b/3) + (c/3)
Again, this causes problems, because we are dealing with integer division, and thus a/3 == 33 and b/3 == 33 and c/3 == 33, leaving the answer at 99 (better, but still not what we want)
Convert a,b, and c into doubles, do the average, then convert them back. We can accomplish this by "Casting"
To cast a variable from one type to another, we use the name of the type we want as a function!
average = (double(a) + double(b) + double(c)) / 3;
Notice, the following code is STILL wrong:
average = double(a + b + c) / 3;
Use the Matlab builtin function: mean. This function converts the values (to floats) for us!
Note: before a programmer relys on a Matlab function to accurately do the needed work, the programmer should test it!