32

Possible Duplicate:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?

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?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
eonil
  • 83,476
  • 81
  • 317
  • 516

4 Answers4

36

It's a bitfield. It's only valid in a struct definition, and it means that the system will only use 8 bits for your integer.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
EboMike
  • 76,846
  • 14
  • 164
  • 167
17

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.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 2
    That's an awfully strong statement, there. Care to elaborate? – zwol Oct 21 '10 at 03:00
  • 5
    +1 for "never use bitfields." – Travis Gockel Oct 21 '10 at 03:00
  • 4
    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. – Arun Oct 21 '10 at 03:04
  • @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 Oct 21 '10 at 03:07
  • 1
    "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 Oct 21 '10 at 03:07
  • 1
    @Travis: That is *implementation-defined*. All implementation-defined features must be documented. See §6.7.2.1/10. – dreamlax Oct 21 '10 at 03:07
  • @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 Oct 21 '10 at 03:10
  • @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 Oct 21 '10 at 03:14
  • 3
    @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.. GitHub STOP HELPING ICE Oct 21 '10 at 03:24
  • @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 Oct 21 '10 at 03:34
4

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.

Arun
  • 19,750
  • 10
  • 51
  • 60
2

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

karlphillip
  • 92,053
  • 36
  • 243
  • 426