473,395 Members | 1,466 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.

Question about bitfield promotion/cast to integer

Ray
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 an unsigned integer, so I
see a +1 if the bit is set. Microsoft VC++ thinks it's
signed, so I see -1 if the bit is set.

Ex.

typedef enum {
ZERO = 0,
ONE = 1
} test_enum_t;

typedef struct {
test_enum_t bitfield : 1;
} enum_struct_t;

int main( void )
{
enum_struct_t enum_struct;

enum_struct.bitfield = 1; /* bit is set */

if ( (test_num_t) enum_struct.bitfield == ONE ) {
printf( "Bit is ONE\n" ); /* cast as unsigned int? */
}
else {
printf( "Bit is ZERO\n" ); /* cast as signed int? */
}

return 0;
}
Thanks!
Nov 14 '05 #1
4 10396
ry***@hotmail.com (Ray) writes:
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?


Bit-fields may only portably have _Bool, signed int, or unsigned
int type.
--
Go not to Usenet for counsel, for they will say both no and yes.
Nov 14 '05 #2


Ray wrote:
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 an unsigned integer, so I
see a +1 if the bit is set. Microsoft VC++ thinks it's
signed, so I see -1 if the bit is set.

From the standard.
6.7.2.1 Structure and union specifiers
....
9 A bit-field is interpreted as a signed or unsigned integer
type consisting of the specified number of bits. See(104).
If the value 0 or 1 is stored into a nonzero-width bit-field
of type _Bool, the value of the bit-field shall compare
equal to the value stored.

104) As specified in 6.7.2 above, if the actual type specifier
used is int or a typedef-name defined as int, then it is
implementation-defined whether the bit-field is signed or unsigned.

Ex.

typedef enum {
ZERO = 0,
ONE = 1
} test_enum_t;

typedef struct {
test_enum_t bitfield : 1;
} enum_struct_t;

int main( void )
{
enum_struct_t enum_struct;

enum_struct.bitfield = 1; /* bit is set */

if ( (test_num_t) enum_struct.bitfield == ONE ) {
printf( "Bit is ONE\n" ); /* cast as unsigned int? */
}
else {
printf( "Bit is ZERO\n" ); /* cast as signed int? */
}

return 0;
}
Thanks!


--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3
On 05 Jan 2004 18:51:28 -0800, Ben Pfaff <bl*@cs.stanford.edu> wrote
in comp.lang.c:
ry***@hotmail.com (Ray) writes:
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?


Bit-fields may only portably have _Bool, signed int, or unsigned
int type.


Which fails to answer the question the OP asked, so why did you
bother? And bit-fields may indeed be int portably, so long as the
sign bit/msb is always 0. A 4-bit bit-field defined as just "int" may
portably hold the values 0 through 7 an any and every implementation.

As for the enum, it is perfectly legal if the implementation documents
that it accepts it. It is also perfectly portable to any
implementation that provides an option to treat all enumerated types
as ints, and most do.

C99 6.7.2.1 para 4:

"4 A bit-field shall have a type that is a qualified or unqualified
version of _Bool, signed int, unsigned int, or some other
implementation-defined type."

--
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 #4
On 5 Jan 2004 18:11:05 -0800, ry***@hotmail.com (Ray) wrote in
comp.lang.c:
When a single-bit bitfield that was formed from an enum
is promoted/cast into an integer, does ANSI C say anything
If it is cast, it becomes whatever it is cast to. That has nothing to
do with implicit conversions.
about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I
see a +1 if the bit is set. Microsoft VC++ thinks it's
signed, so I see -1 if the bit is set.

A single bit bit-field must promote to a signed int. In fact, any
bit-field with no more than 15 bits must promote to signed int, even
if it is defined as an unsigned int bit-field.

========
6.3.1.1 Para 2 & 3:

2. The following may be used in an expression wherever an int or
unsigned int may be used:

— An object or expression with an integer type whose integer
conversion rank is less than the rank of int and unsigned int.

— A bit-field of type _Bool, int, signed int, or unsigned int.

If an int can represent all values of the original type, the value is
converted to an int; otherwise, it is converted to an unsigned int.
These are called the integer promotions. All other types are
unchanged by the integer promotions.

3. The integer promotions preserve value including sign. As discussed
earlier, whether a ‘‘plain’’ char is treated as signed is
implementation-defined.

========

Since the only possible values of a 1 bit unsigned int bit-field are 0
and -1, so a signed int can represent all possible values. The only
possible values of a 1 bit unsigned int bit-field are 0 and +1, and a
signed int can represent all possible values. So a single bit
bit-field defined as int must promote to a signed int in an
expression, regardless of whether the implementation considers the
bit-field to be signed or unsigned int.
Ex.

typedef enum {
ZERO = 0,
ONE = 1
} test_enum_t;

typedef struct {
test_enum_t bitfield : 1;
} enum_struct_t;

int main( void )
{
enum_struct_t enum_struct;

enum_struct.bitfield = 1; /* bit is set */

if ( (test_num_t) enum_struct.bitfield == ONE ) { ^^^^^^^^^^^^
This is a cast, and it is meaningless. The value of the bit-field
will be converted to a test_num_t, which either is an int or will be
automatically promoted to an int by the rules I quoted above.
printf( "Bit is ONE\n" ); /* cast as unsigned int? */
There is no cast here. There is no such thing as an implicit cast in
C. A cast is an explicit conversion, which might or might not be an
acceptable automatic conversion without a cast.
}
else {
printf( "Bit is ZERO\n" ); /* cast as signed int? */
}

return 0;
}


You are confused, you are comparing the constants ONE and ZERO against
the value of your bit-field converted to a (signed) int.

What is confusing you is the fact that when a bit-field is defined as
"int", or a type such as an enumerated type, without specifying signed
int or unsigned int, it is up to the implementation to decide whether
to treat it as signed or unsigned. Exactly the same as whether
"plain" char is signed or unsigned.

Your compiler treats your unspecified bit-fields as unsigned, which
should be documented in its documentation. That unsigned bit-field
promotes to the signed int value of 1 in an expression.

If your compiler treated that bit-field as unsigned, your second
printf() statement would execute, but it would lie, because your
bit-field would convert to the int value -1.

Try:

printf("Bit is %d\n", enum_struct.bitfield);

Then try changing the type of the bit-field to, in turn, int, signed
int, unsigned int, and see what is output.

--
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

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

Similar topics

5
by: Marc Ueberall | last post by:
Hi there! I've got the following problem. I have to read an integer value out of a 2 byte data block. The structure is like following: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 x y ...
20
by: Russell Shaw | last post by:
Hi, I'm using gcc-3.4.3 on a linux pc. The ints and long ints are 32 bits and long long ints are 64 bits. When i have: int num=9600; long long int reg=(long long)800000000000/(5000000*num);...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
21
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me....
4
by: spibou | last post by:
On 6.3.1.1 of N1124 we read: If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. A few lines above that...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
7
by: arne | last post by:
Hi all, cleaning up some elderly code, I stumbled across the following: /**************************************************/ struct { uint bf:8; char a1; char a2;
40
by: somenath | last post by:
Hi All, I have one question regarding the behavior of printf function. In page number of 154 in K&R2 in Table 7-1 it is stated that Characters Argument Type :...
6
by: shaun roe | last post by:
For a bit of seasonal festive fun, I thought I'd try making a bitfield function, i.e. a function returning, for example, the value of bits 1 to 5 of a word as an integer, when the exact bits are...
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:
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
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.