views:

123

answers:

4

Possible Duplicate:
What does 'unsigned temp:3' means

This is C code sample of a reference page.

      signed int _exponent:8;

What's the meaning of the colon before '8' and '8' itself?

+5  A: 

It's a bitfield. It means that the system will only use 8 bits for your integer.

EboMike
I believe this is only valid in a struct, though.
Cameron
It wouldn't make sense outside a struct.
EboMike
Yes, it's part of a struct
Eonil
+1  A: 

When that statement is inside a structure, means bit fields.

karlphillip
+3  A: 

It's a bitfield, an obscure and misguided feature of structures. That should be enough for you to lookup the information you need to know to deal with bitfields in other people's code. As for your own code, never use bitfields.

Edit: As requesed by Zack, bitfields have significant disadvantages versus performing your own bit arithmetic, and no advantages. Here are some of them:

  • You can only copy, compare, serialize, or deserialize one bitfield element at a time. Doing your own bit arithmetic, you can operate on whole words at a time.
  • You can never have a pointer to bitfield elements. With your own bit arithmetic, you can have a pointer to the larger word and a bit index within the word as a "pointer".
  • Directly reading/writing C structures to disk or network is half-way portable without bitfields, as long as you use fixed-size types and know the endianness. Throw in bitfields, though, and the order of elements within the larger type, as well as the total space used and alignment, become highly implementation-dependent, even within a given cpu architecture.
  • The C specification has very strange rules than allow the signedness of bitfield elements to be different from what you'd expect it to, and very few people are aware of these.

For single-bit flags, using your own bit arithmetic instead of bitfields is a complete no-brainer. For larger values you need to pack, if it's too painful to write out all the bit arithmetic all over the place, write some simple macros.

R..
That's an awfully strong statement, there. Care to elaborate?
Zack
+1 for "never use bitfields."
Travis Gockel
Disagree :). I don't agree on the "obscure and misguided feature of structures" and "never use bitfields" part. Bit fields are very necessary for economic use of space, and interface to hardware.
ArunSaha
@Zack: Bitfields are not portable for a variety of reasons, amongst other things. Although I've not experimented with a modern compiler (say in the last decade), using bitfields was apt to lead to code bloat.
Jonathan Leffler
"Obscure and misguided"? That is strong! Bitfields have their place, the same as gotos and multiple return points. For example, they can be invaluable in embedded systems so as to avoid manual bit fiddling. I don't berate my builder father-in-law for using power tools because I assume he knows how to handle them. That's the same for _all_ tools, including programming languages.
paxdiablo
@Travis: That is *implementation-defined*. All implementation-defined features must be documented. See §6.7.2.1/10.
dreamlax
@dreamlax: That also means that it is not portable. I whole-heartedly agree with paxdiablo on this one: 99.9% of the time, you shouldn't use a bitfield.
Travis Gockel
@Travis: In situations where two bit-field members can both reside within a single addressable unit, they will be. The implementation defined behaviour applies only when two adjacent bit-field members cannot fit within the same addressable unit (that is, having `int n:5; int m:5;` on a system with `CHAR_BIT==8`).
dreamlax
@paxdiablo: I don't consider bitfields a powertool that should be avoided because they're dangerous. I consider them crippled by underspecification that makes their use less effective than the simple alternative of doing bit arithmetic.
R..
@R.. That is true. Experienced C developers already know how to twiddle bits and recognise such constructs in code. I don't use bit-fields myself, but I'm sure they're useful to some demographic for them to be defined by a standard.
dreamlax
+1  A: 

It is a bitfield specification.

It means _exponent takes only 8 bits out of the signed int which typically takes more than 8 bits. Typically, bit-fields are used with unsigned types.

IIRC, compiler would warn if a something that does not fit into 8-bits is written into _exponent.

ArunSaha