473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

bit fields ordering

If I have the following structure:

typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE;

On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?
Nov 15 '05 #1
12 1967
In article <sl*********************@HAL.honeypot>,
Jonathan Maddison <jmaddi gmail > wrote:
If I have the following structure: typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE; On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?


No, the order of bits within a char is undefined by the standard.

Besides, uint8_t only exists on boxes that can support it.
If CHAR_BIT is (say) 9, then uint8_t doesn't exist in the
implementation.
--
Feep if you love VT-52's.
Nov 15 '05 #2
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <sl*********************@HAL.honeypot>,
Jonathan Maddison <jmaddi gmail > wrote:
If I have the following structure:

typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE;

On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?


No, the order of bits within a char is undefined by the standard.

Besides, uint8_t only exists on boxes that can support it.
If CHAR_BIT is (say) 9, then uint8_t doesn't exist in the
implementation.


And the only allowed types for bit fields are int, signed int,
unsigned int, and _Bool (or some other implementation-defined type).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3
Jonathan Maddison wrote on 27/08/05 :
typedef union BYTE {
uint8_t bite;
Do you meant byte ?
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE; On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?


You can't. Bitfields are not portable. For portable interfaces, use
unsigned integers and bitwise operators.

This can help

http://mapage.noos.fr/emdel/clib/ed/inc/bits.h

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Nov 15 '05 #4
I thought I posted a reply before, but I seemed to of accidently emailed
Walter instead (sorry Walter :().

On 2005-08-27, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
No, the order of bits within a char is undefined by the standard. Oh, ok - thanks. Besides, uint8_t only exists on boxes that can support it.
If CHAR_BIT is (say) 9, then uint8_t doesn't exist in the
implementation. Oh I see, damn - I'm going to have to find an elegant workaround for other
systems then. But how common exactly are systems that don't support that type
(or other 8-bit types).
And the only allowed types for bit fields are int, signed int,
unsigned int, and _Bool (or some other implementation-defined type). I am using c99 - so uint8_t should be implementation-defined (except in
some systems as Walter pointed out) - I think...
You can't. Bitfields are not portable. For portable interfaces, use
unsigned integers and bitwise operators. :( I was hoping someone wouldn't say that... Oh well, looks like its my only
option now. This can help

Thanks for the link.

Nov 15 '05 #5
Don
Jonathan Maddison <Jo******@HAL.honeypot> wrote in
news:slrndh02ih.1t4.Jo******@HAL.honeypot:

If I have the following structure:

typedef union BYTE {
uint8_t bite;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits;
} BYTE;

On my box, with gcc I get the msb stored in b7, all the way though to
the lsb stored in b0. BUT, how portable is the above code - can I assume
that the fields will always be ordered in such a way?


You cannot assume anything, dumbass.
Nov 15 '05 #6
Jonathan Maddison wrote:
On 2005-08-27, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
Besides, uint8_t only exists on boxes that can support it.
If CHAR_BIT is (say) 9, then uint8_t doesn't exist in the
implementation.


Oh I see, damn - I'm going to have to find an elegant workaround for other
systems then. But how common exactly are systems that don't support that type
(or other 8-bit types).


Not very common. Apparently the C implementation on some old mainframes
had 36-bit words divided into four 9-bit bytes. However, they probably
don't have a compiler available that conforms to the recent standard
that defined the uint8_t style macros.

Some recent embedded systems, particularly Digital Signal Processors
(DSPs), address all memory in 16 or 32 bit chunks, and their C
implementation defines the byte as the native word size. In such an
implementation, sizeof(char), sizeof(short) and sizeof(int) will all be 1.

This causes unexpected results in many common C idioms. For example,
getchar() normally returns a positive int containing the character
value, or else EOF (which is some negative int) if an error or
end-of-file condition occurs. However, if characters require the full
range of the int data type, including what maps to negative numbers,
then there is no space for out-of-band EOF signalling.

If you are writing code for general personal computers, you are very
unlikely to encounter an implementation with a byte size other than 8 bits.

--
Simon.
Nov 15 '05 #7
Don wrote on 27/08/05 :
You cannot assume anything, dumbass.


Why being so rude ? Get a life...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Nov 15 '05 #8
Simon Biber wrote:
Jonathan Maddison wrote:
On 2005-08-27, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
Besides, uint8_t only exists on boxes that can support it.
If CHAR_BIT is (say) 9, then uint8_t doesn't exist in the
implementation.
Oh I see, damn - I'm going to have to find an elegant workaround for
other
systems then. But how common exactly are systems that don't support
that type
(or other 8-bit types).


Not very common. Apparently the C implementation on some old mainframes
had 36-bit words divided into four 9-bit bytes. However, they probably
don't have a compiler available that conforms to the recent standard
that defined the uint8_t style macros.


Probably true.
Some recent embedded systems, particularly Digital Signal Processors
(DSPs), address all memory in 16 or 32 bit chunks, and their C
implementation defines the byte as the native word size. In such an
implementation, sizeof(char), sizeof(short) and sizeof(int) will all be 1.
How do you define recent? I was working in C on such a system in about
1995 IIRC and I think the compiler was up to version 4.something at the
time, so it had been around for a while.
This causes unexpected results in many common C idioms. For example,
getchar() normally returns a positive int containing the character
value, or else EOF (which is some negative int) if an error or
end-of-file condition occurs. However, if characters require the full
range of the int data type, including what maps to negative numbers,
then there is no space for out-of-band EOF signalling.
Such systems are not generally hosted implementations, and it is only
hosted implementations that are required to provide stdio.h and, indeed,
most of the rest of the standard library.
If you are writing code for general personal computers, you are very
unlikely to encounter an implementation with a byte size other than 8 bits.


That is true. Different sizes of int and long are far more likely in
hosted implementations.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #9
Don <do*@127.0.0.1> writes:
[...]
You cannot assume anything, dumbass.


I think we have ourselves a troll. Not only is "Don" is rude; he also
cross-posts to alt.usenet.kooks. I suggest ignoring him, or at least
not following the cross-post.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
"Simon Biber" <ne**@ralmin.cc> wrote

If you are writing code for general personal computers, you are very
unlikely to encounter an implementation with a byte size other than 8
bits.

It's a major engineering blunder not to allow for fast 8-bit addressing in
hardware.
However the snag is that in C there is no "byte" type. There is an argument
for allowing "char" to take unicode.
The current convention is to call unicode characters "wide characters" or
w_char. Unfortunately this then breaks anything that calls stdio (including
fopen), or the string library. So it would be unwise to assume that some
complier writer in the future isn't going to say that "char" is sixteen
bits.
By making as few assumptions as possible, you can to some extent
future-proof your code.
Nov 15 '05 #11
Malcolm wrote on 29/08/05 :
It's a major engineering blunder not to allow for fast 8-bit addressing in
hardware.
<way off-topic>
Why ? Who cares ? If a 16-bit access is as fast than a 8-bit access,
why bother ?
</> However the snag is that in C there is no "byte" type. There is an argument
for allowing "char" to take unicode.


Not char, but wchar_t.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
Nov 15 '05 #12
Emmanuel Delahaye wrote:
Malcolm wrote on 29/08/05 :
However the snag is that in C there is no "byte" type. There is an argument
for allowing "char" to take unicode.


Not char, but wchar_t.


No, he means char, and I agree. But I'm resigned to it not happening in
C.

--
Peter

Nov 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc de Winter | last post by:
Hello all, I'm new to this newsgroup and have tried to search through the archives first, before posting this question. I don't think it has been addressed before. Also, this question is about a...
2
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need...
9
by: Paul Morrow | last post by:
I have seen the technique where a number of rows in a database are displayed in an html table so that each column of each row is editable. They use a single form surrounding the table, where each...
12
by: Ney André de Mello Zunino | last post by:
Hello. I have been having some trouble dealing with bit fields. The following is a simple program that demonstrates it. #include <iomanip> #include <iostream> struct instrucao_i {
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
3
by: Amir Shitrit | last post by:
Hello. How come it's safe to read non-volatile fields that are shared across threads using locks for synchronization (for example, Monitor.Enter or EventWaitHandle), but it's not safe to access...
3
by: sam_cit | last post by:
Hi Everyone, We all know that bit fields are to be used within a structure, like struct sample { int i:4; } However, is there any reason as to why it can only be specified only
2
by: DiAvOl | last post by:
Hello everyone, I read here and there that bit-field usage should be avoided because it's not portable. My question is why it's not portable? For example let's say I have a struct in my code...
4
ChrisWang
by: ChrisWang | last post by:
Dear all, I am reading the book "Core Python Programming". In the chapter talking about modules, it says the modules should follow this ordering: import Python Standard Library modules ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.