3 ms·
For example, if you have an uint8_t with the value 200, that denotes either the number 200 or the number 456 or the number 712 and so on. If you have an uint16
by adrian_b 5y ago
For example, if you have an uint8_t with the value 200, that denotes either the number 200 or the number 456 or the number 712 and so on.
If you have an uint16_t with the value 200, that denotes either the number 200 or the number 65736 or the number 131272 and so on.
So it is obvious that the two "200" do not mean the same thing.
If you do a longer sequence of operations with uint8_t or uint16_t, inserting an implicit integer promotion in the middle of the chain of operations will change the end result, depending on the place where it is inserted. (E.g. 200*200 will be either 144 or 40000, depending on whether the promotion is done before or after the multiplication)
Of course most programmers do not actually use the "unsigned" numbers as modular numbers but as a surrogate for the lack of proper "unsigned" interval numbers and they attempt to avoid any operations which will cause modular reduction.
If the programmer knows that no modular reduction has occurred, he/she can write an explicit type conversion from a smaller "unsigned" type to a larger type.
The compiler cannot know which was the intention of the programmer. Because the "unsigned" numbers are defined as modular numbers, not as interval numbers, any implicit conversion can be erroneous. If the conversions are actually correct, the programmer should write them explicitly.