OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 12:04 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Coding style: uint8_t for 256 entries
PostPosted: Sat Nov 28, 2015 5:46 am 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Coding style: uint8_t for 256 entries
PostPosted: Sun Nov 29, 2015 9:55 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
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

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: Coding style: uint8_t for 256 entries
PostPosted: Mon Nov 30, 2015 1:22 am 
Offline
Member
Member

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 61 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group