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

bit fields in a structure

Neo
typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
} bit;
UINT16 word;
} HSL_BANK_SELECT_T;

HSL_BANK_SELECT_T a;
a.bit.bank_no = 1

Is bank_no MSB or LSB?
What is the significance of UINT16 or any other type here?
Can I replace it with UINT8 or UINT32.... arbitrarily?

typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
UINT8 some_flag1 : 1;
UINT8 some_flag2: 1;
} bit;
UINT32 word;
} HSL_BANK_SELECT_T2;

Is this valid to have UINT16 and UINT8 intermixed...???

-Neo
Nov 14 '05 #1
2 3269
Neo wrote:

typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
} bit;
UINT16 word;
} HSL_BANK_SELECT_T;

HSL_BANK_SELECT_T a;
a.bit.bank_no = 1

Is bank_no MSB or LSB?
Either. Or neither. The compiler has wide discretion
in the way it arranges bit-fields, and different compilers
do it differently.
What is the significance of UINT16 or any other type here?
The legitimate types for bit-fields are signed and
unsigned `int' and (in C99) _Bool. A compiler may accept
other types as well, but is not required to do so -- which
means that the next compiler you use might not accept these
extensions.

Since you haven't shown how UINT16 is defined, it's
not possible to say whether it's a legitimate bit-field
type. You may be in trouble already.
Can I replace it with UINT8 or UINT32.... arbitrarily?
See above.
typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
UINT8 some_flag1 : 1;
UINT8 some_flag2: 1;
} bit;
UINT32 word;
} HSL_BANK_SELECT_T2;

Is this valid to have UINT16 and UINT8 intermixed...???


See above.

You have fallen into a trap that has caught many before
you, namely, of thinking that `struct's, with or without
bit-fields, are a means of mapping an externally-imposed
format. Such use may be appropriate for a particular compiler
on a particular machine, but the language definition does not
guarantee that the mapping will be as you hope.

--
Er*********@sun.com
Nov 14 '05 #2
Neo

"Eric Sosman" <Er*********@sun.com> wrote in message
news:40***************@sun.com...
Neo wrote:

typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
} bit;
UINT16 word;
} HSL_BANK_SELECT_T;

HSL_BANK_SELECT_T a;
a.bit.bank_no = 1

Is bank_no MSB or LSB?
Either. Or neither. The compiler has wide discretion
in the way it arranges bit-fields, and different compilers
do it differently.
What is the significance of UINT16 or any other type here?


The legitimate types for bit-fields are signed and
unsigned `int' and (in C99) _Bool. A compiler may accept
other types as well, but is not required to do so -- which
means that the next compiler you use might not accept these
extensions.


What is _Bool ???

Since you haven't shown how UINT16 is defined, it's
not possible to say whether it's a legitimate bit-field
type. You may be in trouble already.
Here is how I define UINT8, 16 and 32 (for unsigned 8, 16 and 32 bit
quantities)...
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
Can I replace it with UINT8 or UINT32.... arbitrarily?


See above.
typedef union
{
struct
{
UINT16 reserved : 14;
UINT16 bank_no : 2;
UINT8 some_flag1 : 1;
UINT8 some_flag2: 1;
} bit;
UINT32 word;
} HSL_BANK_SELECT_T2;

Is this valid to have UINT16 and UINT8 intermixed...???


See above.

You have fallen into a trap that has caught many before
you, namely, of thinking that `struct's, with or without
bit-fields, are a means of mapping an externally-imposed
format. Such use may be appropriate for a particular compiler
on a particular machine, but the language definition does not
guarantee that the mapping will be as you hope.

--
Er*********@sun.com

Nov 14 '05 #3

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

Similar topics

1
by: Phil Kershaw | last post by:
Hi All, Can anyone help me with a problem that has been driving me mad. I have a table with 10 fields in it for staff to enter their initials to register a vote. I need to count the fields that...
1
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word...
3
by: mrhicks | last post by:
Hello all, I have a question regarding efficeny and how to find the best approach when trying to find flag with in a structure of bit fields. I have several structures which look similar to ...
10
by: Abhi | last post by:
In ANSI C, we can have a bit fields inside structure like struct _A { int i:2; }; would define a variable of 2-bit length. My doubt is that what would the size of this structure then? Would...
6
by: Charles Law | last post by:
I have the following structure (for example) that I wish to convert for use in VB.NET. What would be the best way to do it? Ideally, it would convert directly to allow access to the bit fields in...
9
by: Charles Law | last post by:
Sorry for asking this one again, but with the newsgroup now holding things for an ever shorter time, I can't find my original question (and the answers) from a few months ago. I know that...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
4
by: sameerishah | last post by:
Hi, I have a need to read the fields of a structure in a for loop. for eg. the structure looks like: str.a1, str.a2, str.a3 where a1, a2, a3 are of type string How do I do it? Thanks in...
23
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.