Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > bitfield and enum, is this legal?

Reply
Thread Tools

bitfield and enum, is this legal?

 
 
Erik Cato
Guest
Posts: n/a
 
      02-23-2004
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
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      02-23-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      02-23-2004
Ben Pfaff <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-24-2004
(E-Mail Removed) (Peter Nilsson) writes:

> Ben Pfaff <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>> 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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      02-24-2004
On Mon, 23 Feb 2004 09:46:36 -0800, Ben Pfaff <(E-Mail Removed)>
wrote in comp.lang.c:

> (E-Mail Removed) (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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      02-24-2004
On 23 Feb 2004 04:55:53 -0800, (E-Mail Removed) (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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-24-2004
Jack Klein <(E-Mail Removed)> writes:

> On Mon, 23 Feb 2004 09:46:36 -0800, Ben Pfaff <(E-Mail Removed)>
> wrote in comp.lang.c:
>
>> (E-Mail Removed) (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
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      02-25-2004
> > 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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-27-2004
On 25 Feb 2004 13:26:34 -0800, (E-Mail Removed) (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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitfield in struct and padding Stanley Rice C Programming 6 11-10-2011 07:32 PM
bitfield and initialization list mc C++ 5 12-16-2008 10:19 PM
transforming bitfield to readable data hortitude.eyeball@gmail.com XML 0 05-25-2006 07:59 PM
bitfield & union strange ?! Claudio C++ 2 08-02-2004 09:23 AM
bitfield optimizations zb32 C++ 1 07-13-2004 05:13 AM



Advertisments