Sign in to follow this  
255

Byte length and portability

Recommended Posts

My goal is to always write programs as portably as possible. Something that has begun to bother me is that I often find myself assuming a byte is (at least) eight bits long. I have heard there are platforms where this isn't true but have never seen anyone mention examples. My question is: is it reasonable to assume that my program will never have to run on such a machine? Something else I find odd is that the inttypes.h and stdint.h headers in C99 define integer types whose sizes are multiples of 8. Does C99, then, assume an 8-bit byte? A slightly related question: how do the various programming language standards (most interestingly the ones of managed/"safe" languages) define what happens when an integer overflows? Is it reasonable to assume that it will just wrap around on all modern and future platforms?

Share this post


Link to post
Share on other sites
Here's a succinct overview: Byte. Older computers used 6, 7 and 9 bit bytes but 8 bit bytes have become the defacto standard. As the article points out, Java defines a byte as 8 bits, so that should give you an indication of what kind of portability you can get with 8 bit bytes. I don't have a copy of the C99 standard on this PC. I'll have to look it up on my other PC and get back to you.

// edit - here's a link to a late draft version of the C99 spec: n869.pdf (1,372 kb).

Quote:

3.4
byte
addressable unit of data storage large enough to hold any member of the basic character set of the execution environment
2 NOTE 1 It is possible to express the address of each individual byte of an object uniquely.
3 NOTE 2 A byte is composed of a contiguous sequence of bits, the number of which is implementationdefined. The least significant bit is called the low-order bit; the most significant bit is called the high-order
bit.


Quote:

5.2.4.2.1 Sizes of integer types <limits.h>

— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
— minimum value for an object of type signed char
SCHAR_MIN -127 // -(27 - 1)
— maximum value for an object of type signed char
SCHAR_MAX +127 // 27 - 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 28 - 1
— minimum value for an object of type char
CHAR_MIN see below
— maximum value for an object of type char
CHAR_MAX see below
...
2 If the value of an object of type char is treated as a signed integer when used in an expression, the value of CHAR_MIN shall be the same as that of SCHAR_MIN and the value of CHAR_MAX shall be the same as that of SCHAR_MAX. Otherwise, the value of CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same as that of UCHAR_MAX.13) The value UCHAR_MAX+1 shall equal 2 raised to the power CHAR_BIT.


HHH

Share this post


Link to post
Share on other sites
On a related question, the behaviour of one's complement machines gets mentioned here occasionally. Now I understand how one's complement works, but are there any modern processors that use it?

Share this post


Link to post
Share on other sites
With regards to LessBread's post, those limits are minimums. A byte can be more than eight bits, the minimum value of a signed character can be less than -127 and is often -128, and so on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this