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

bitfield and enum, is this legal?

Hi group!

Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;

//Erik
Nov 14 '05 #1
8 1619
er*******@japro.se (Erik Cato) writes:
Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;


It's not portable. Bit-fields can only portably have type signed
int or unsigned int.
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
Nov 14 '05 #2
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
er*******@japro.se (Erik Cato) writes:
Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;


It's not portable. Bit-fields can only portably have type signed
int or unsigned int.


Or _Bool in C99.

Note that 'plain' int is also allowed, but it is implementation
defined as to whether this behaves as signed or unsigned int in the
context of bitfields.

--
Peter
Nov 14 '05 #3
ai***@acay.com.au (Peter Nilsson) writes:
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
It's not portable. Bit-fields can only portably have type signed
int or unsigned int.


Or _Bool in C99.


I thought about adding that, but then I decided that C99 was also
not portable :-)
Nov 14 '05 #4
On Mon, 23 Feb 2004 09:46:36 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote in comp.lang.c:
er*******@japro.se (Erik Cato) writes:
Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;


It's not portable. Bit-fields can only portably have type signed
int or unsigned int.


No, it is undefined behavior under any version of the standard prior
to C99. Semantics section of 6.5.2.1 of C90/95:

<quote>
A bit-field shall have a type that is a qualified or unqualified
version of one of int, unsigned int, or signed int. Whether the
high-order bit position of a (possibly qualified) “plain” int
bit-field is treated as a sign bit is implementation-defined. A
bit-field is interpreted as an integral type consisting of the
specified number of bits.
<unquote>

The result of violating a "shall" outside of a constraint clause is
undefined behavior.

--
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 14 '05 #5
On 23 Feb 2004 04:55:53 -0800, er*******@japro.se (Erik Cato) wrote in
comp.lang.c:
Hi group!

Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;

//Erik


It is undefined behavior prior to the C99 standard update and allowed
as an implementation-defined extension in C99.

The behavior of single bit bit-fields not defines specifically as
unsigned is very much implementation defined. Any field where the bit
is set to 1 might be promoted to the integer value -1, rather than +1,
in expressions.

--
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 14 '05 #6
Jack Klein <ja*******@spamcop.net> writes:
On Mon, 23 Feb 2004 09:46:36 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote in comp.lang.c:
er*******@japro.se (Erik Cato) writes:

It's not portable. Bit-fields can only portably have type signed
int or unsigned int.


No, it is undefined behavior under any version of the standard prior
to C99. Semantics section of 6.5.2.1 of C90/95:


Relying on undefined behavior is nonportable.
--
"The fact that there is a holy war doesn't mean that one of the sides
doesn't suck - usually both do..."
--Alexander Viro
Nov 14 '05 #7
> > typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1, .... }t_bitfield8;


It is undefined behavior prior to the C99 standard update and allowed
as an implementation-defined extension in C99.


What is the difference between these two cases.
If the standard calls something "undefined" then the implementation
is allowed to define it anyway. And if it is "allowed as an ID extension"
but the extension is not provided, then it is UB.
Nov 14 '05 #8
On 25 Feb 2004 13:26:34 -0800, ol*****@inspire.net.nz (Old Wolf)
wrote:
> typedef enum
> {
> FALSE = 0,
> TRUE = 1,
> }t_bool;
>
> typedef struct
> {
> t_bool b0 : 1,... > }t_bitfield8;
>


It is undefined behavior prior to the C99 standard update and allowed
as an implementation-defined extension in C99.


What is the difference between these two cases.
If the standard calls something "undefined" then the implementation
is allowed to define it anyway. And if it is "allowed as an ID extension"
but the extension is not provided, then it is UB.


The difference is:

If it is undefined but your compiler accepts it today, there is no
guarantee what will happen tomorrow, if your compiler is updated, or
if you change systems.

If it is implementation defined, then every compliant compiler
must document what it will do (or in this case if the extension is
allowed).
<<Remove the del for email>>
Nov 14 '05 #9

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

Similar topics

10
by: Siemel Naran | last post by:
Hi. I notice that my compiler accepts enum { A, // comment B, // comment }; Notice the trailing comma after B.
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
3
by: Andy Venikov | last post by:
Sometimes you want to use a bitfield to hold an enum value. In such cases you would only use as many bits as are needed to encode the full set of enum values. And it is a pain to recompute and...
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
10
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
2
by: Lee Gillie | last post by:
I see this was posted a long time ago, but it has since expired. Given.... Public Enum Fruit Apple Banana Cherry Peach Strawberry
9
by: Fred Zwarts | last post by:
What is the recommended way to loop over all enum values of a certain enum type? Consider the following definition: typdef enum {A=2, B, C=5, D} E; then for (E x = A; x <= D; ++x) { ... }
2
by: Alex Vinokur | last post by:
--- File foo.cpp --- class Foo { private: enum Eoo { aaa, bbb, ccc }; struct Soo { Eoo m_code; }; };
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: 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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.