473,395 Members | 1,442 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,395 software developers and data experts.

order of bit fields

is i have this:

struct {
unsigned char bit7: 1;
unsigned char bit6: 1;
unsigned char bit5: 1;
unsigned char bit4: 1;
unsigned char bit3: 1;
unsigned char bit2: 1;
unsigned char bit1: 1;
unsigned char bit0: 1;
};

can i assume that bit0 is the lowest (2^0) and bit7 is the highest (2^7)
bit? is this guaranteed by the standard or is it implementation dependent?
Nov 1 '05 #1
6 2885
"Martin Vorbrodt" <mv*******@gmail.com> wrote in message
news:oZ*****************@twister.nyc.rr.com...
is i have this:

struct {
unsigned char bit7: 1;
unsigned char bit6: 1;
unsigned char bit5: 1;
unsigned char bit4: 1;
unsigned char bit3: 1;
unsigned char bit2: 1;
unsigned char bit1: 1;
unsigned char bit0: 1;
};

can i assume that bit0 is the lowest (2^0) and bit7 is the highest (2^7)
bit? is this guaranteed by the standard or is it implementation dependent?


The order of the bits and the amount of padding (and possibly others) are
implementation dependent.

Ali

Nov 1 '05 #2
On Tue, 01 Nov 2005 19:02:44 GMT, "Martin Vorbrodt"
<mv*******@gmail.com> wrote in comp.lang.c++:
is i have this:

struct {
unsigned char bit7: 1;
unsigned char bit6: 1;
unsigned char bit5: 1;
unsigned char bit4: 1;
unsigned char bit3: 1;
unsigned char bit2: 1;
unsigned char bit1: 1;
unsigned char bit0: 1;
};

can i assume that bit0 is the lowest (2^0) and bit7 is the highest (2^7)
bit? is this guaranteed by the standard or is it implementation dependent?


No, you can't. And you can't assume that the compiler will only use
an 8-bit char (assuming CHAR_BIT is 8 on your platform) to store it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 2 '05 #3
Martin Vorbrodt wrote:
is i have this:

struct {
unsigned char bit7: 1;
unsigned char bit6: 1;
unsigned char bit5: 1;
unsigned char bit4: 1;
unsigned char bit3: 1;
unsigned char bit2: 1;
unsigned char bit1: 1;
unsigned char bit0: 1;
};

can i assume that bit0 is the lowest (2^0) and bit7 is the highest (2^7)
bit? is this guaranteed by the standard or is it implementation dependent?


The order of the bits and the size of an allocated bitfield are not
just implementation-dependent - they are implementation-defined.

Therefore, although the standard mandates no particular bit order or
allocation size of a bitfield, every C++ compiler must nonetheless
document the bit order and the allocation size of a bitfield compiled
with that compiler.

Greg

Nov 2 '05 #4
"Greg" <gr****@pacbell.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Martin Vorbrodt wrote:
is i have this:

struct {
unsigned char bit7: 1;
unsigned char bit6: 1;
unsigned char bit5: 1;
unsigned char bit4: 1;
unsigned char bit3: 1;
unsigned char bit2: 1;
unsigned char bit1: 1;
unsigned char bit0: 1;
};

can i assume that bit0 is the lowest (2^0) and bit7 is the highest (2^7)
bit? is this guaranteed by the standard or is it implementation
dependent?
The order of the bits and the size of an allocated bitfield are not
just implementation-dependent - they are implementation-defined.

Therefore, although the standard mandates no particular bit order or
allocation size of a bitfield, every C++ compiler must nonetheless
document the bit order and the allocation size of a bitfield compiled
with that compiler.

Greg


do you know of two different compilers with different bit fields order? i'm
asking because so far i tested gcc and msvc++ and they seam to be
consistent. bits go from least significant to the most significant, and when
i use unsigned char for bitfields <= 8 bits, it allocates then at byte
boundary. do you know a compiler i could test it with that has a radically
different behaviour?

Nov 2 '05 #5
Ian
Martin Vorbrodt wrote:
"Greg" <gr****@pacbell.net> wrote in message

do you know of two different compilers with different bit fields order? i'm
asking because so far i tested gcc and msvc++ and they seam to be
consistent. bits go from least significant to the most significant, and when
i use unsigned char for bitfields <= 8 bits, it allocates then at byte
boundary. do you know a compiler i could test it with that has a radically
different behaviour?

Well I've used quite a few over the years and never seen one that didn't
do this.

Ian
Nov 3 '05 #6

Martin Vorbrodt wrote:
"Greg" <gr****@pacbell.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Martin Vorbrodt wrote:
is i have this:

struct {
unsigned char bit7: 1;
unsigned char bit6: 1;
unsigned char bit5: 1;
unsigned char bit4: 1;
unsigned char bit3: 1;
unsigned char bit2: 1;
unsigned char bit1: 1;
unsigned char bit0: 1;
};

can i assume that bit0 is the lowest (2^0) and bit7 is the highest (2^7)
bit? is this guaranteed by the standard or is it implementation

dependent?

The order of the bits and the size of an allocated bitfield are not
just implementation-dependent - they are implementation-defined.

Therefore, although the standard mandates no particular bit order or
allocation size of a bitfield, every C++ compiler must nonetheless
document the bit order and the allocation size of a bitfield compiled
with that compiler.

Greg


do you know of two different compilers with different bit fields order? i'm
asking because so far i tested gcc and msvc++ and they seam to be
consistent. bits go from least significant to the most significant, and when
i use unsigned char for bitfields <= 8 bits, it allocates then at byte
boundary. do you know a compiler i could test it with that has a radically
different behaviour?


The bit order tends to correlate with the endianess of the target
processor architecture. Big-endian compilers tend to lay out the bits
in an order that is the reverse of the order used by a little endian
compiler.

Some compilers allow the user to override the default bitfield order.
For example, Metrowerks CodeWarrior (and more recently, gcc) support a
#pragma reverse_bitfields directive that will reverse the order of the
bits in a bitfield from the order that would otherwise have applied.

Greg

Nov 3 '05 #7

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

Similar topics

2
by: Andrea | last post by:
Hi, I'm trying to emulate part of our client-server application as a web site so customers can use it, and I'm stuck when it comes to re-ordering items in a list. Basically we have a list of...
1
by: svilen | last post by:
hi. this was named but it is misleading. i want to have the order of setting items in intrinsic dicts (keyword args, or class attributes, etc). which is, a prdered dict trhat matches the...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
1
by: shank | last post by:
I need advice on how to get started with this project. I want a page where a user can manually enter an order. Our users know our order# system and it's much easier to manually enter items as...
10
by: Mark C. Neustadt | last post by:
Okay, okay... from what I can find, I'm gonna be out of luck. I also understand that it *shouldn't* matter but it does. I'm trying to send some XML to Amazon and they're requiring the nodes to be...
4
by: Joe User | last post by:
Hi all....I have a feeling this is going to be one of those twisted query questions, but here it goes anyways.... I want to generate a report that shows the chronology of events (represented by...
12
by: Jozef | last post by:
Hello, Does anyone here know for sure, when you do a For Each loop on a forms controls collection, does Access cycle through them in Alphabetical order or control ID or ???. Any ideas? ...
6
by: Index | last post by:
Hi, I have a file which I want to sort depending on multiple columns. Actually I want to implement the following query through programming: SELECT SUM(VALUES) GROUP BY x,y ORDER BY x,y; the...
13
by: Kevin | last post by:
Hi, I have used ASP for years using MS Access and have used MSSQL quite a lot as well. I have never came across something like this before. MSSQL table names and types: ProductName nvarchar...
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.