Page 2 of 2
Re: Coding style: uint8_t for 256 entries
Posted: Sat Nov 28, 2015 5:46 am
by onlyonemac
The bottom line is I made my small table exactly 256 entries to fit neatly in the uint8_t that I was using to iterate it (choosing arbitrary sizes for tables is difficult) so it seems like I should stick to the uint8_t that determined that size. Furthermore, as I was choosing the size of the table around the uint8_t, 255 is in fact the correct size.
Re: Coding style: uint8_t for 256 entries
Posted: Sun Nov 29, 2015 9:55 pm
by kzinti
C99 standard (§6.2.5/9) wrote:
A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
You are right for unsigned integers. I didn't know / forgot about this. But is is undefined for signed integers:
C99 standard (§3.4.3/1) wrote:
An example of undefined behavior is the behavior on integer overflow
As to why that is, check this link:
http://stackoverflow.com/questions/1819 ... verflow-is
Re: Coding style: uint8_t for 256 entries
Posted: Mon Nov 30, 2015 1:22 am
by onlyonemac
kiznit wrote: C99 standard (§6.2.5/9) wrote:
A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
You are right for unsigned integers. I didn't know / forgot about this. But is is undefined for signed integers:
C99 standard (§3.4.3/1) wrote:
An example of undefined behavior is the behavior on integer overflow
As to why that is, check this link:
http://stackoverflow.com/questions/1819 ... verflow-is
Interesting comments, I hadn't thought of that before. Nevertheless I can find many "obscure" ways of representing unsigned integers, but there truly is only one system that's in common use and while there are multiple ways of representing signed integers that all allow the same range of numbers to be represented and that give similar performance, that is not true for unsigned integers as there is really only one system that *should* be used (ordinary binary).