Coding style: uint8_t for 256 entries

Programming, for all ages and all languages.
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Coding style: uint8_t for 256 entries

Post 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.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Coding style: uint8_t for 256 entries

Post 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
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Coding style: uint8_t for 256 entries

Post 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).
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Post Reply