tags:

views:

208

answers:

8

Hi everyone, I just saw a code snippet with a piece of syntax that I have never seen before. What does bool start : 1; mean? I found it inside a class definition in a header file.

+4  A: 

it's a bitfield. : 1 means one bit is used. see for example http://msdn.microsoft.com/en-us/library/ewwyfdbe(VS.71).aspx

Chris Card
+3  A: 

It means that start is 1 bit wide, as opposed to the normal bool which is 1 byte long. You can pack multiple smaller variables into a larger variable and the compiler will generate all the and/or code necessary to read/write it for you. You will take a (noticeable) performance hit, but, if used right, you'll use a lot less memory.

Blindy
Performance of what? I assume the compiling process is slowed down (which shouldn;t be a problem imho), but is the runtime performance slowed down as well?
Oxymoron
Yes, at runtime every access you make to this variables (either read or write) have to be translated to bit-level accesses. For example if you read one of these things, you first have to load the big field in a register, AND it with a mask and then shift right, which is at least 3 times slower than a normal read. Same goes for the write.
Blindy
It gets even worse with cross-boundary bit fields, they have to be combined, doubling the amount of work (if they aren't just outright ignored).
Blindy
+3  A: 

It makes the member start into a bit-field, with 1 bit of space reserved.

It's only valid for struct/class members, you can't have a bit-field variable.

unwind
+4  A: 

See the Wikipedia entry about Bit Fields. It tells the compiler how many bits the structure member should occupy.

schnaader
+2  A: 

This is the syntax for bit fields

Essentially, you define a field in a struct to have only a few bits of a full byte or short or int.
Several bit fields may share the same int so this method can be used as a clever way to avoid some bit manipulations in constructing values.

shoosh
+1  A: 

It's a bit-field. But I've never tried making bit-fields on boolean.

Tadeusz Kopec
+6  A: 
struct record {
    char *name;
    int refcount : 4;
    unsigned dirty : 1;
};

Those are bit-fields; the number gives the exact size of the field, in bits. (See any complete book on C for the details.) Bit-fields can be used to save space in structures having several binary flags or other small fields, and they can also be used in an attempt to conform to externally-imposed storage layouts. (Their success at the latter task is mitigated by the fact that bit-fields are assigned left-to-right on some machines and right-to-left on others).

Note that the colon notation for specifying the size of a field in bits is only valid in structures (and in unions); you cannot use this mechanism to specify the size of arbitrary variables.

  • References: K&R1 Sec. 6.7 pp. 136-8
  • K&R2 Sec. 6.9 pp. 149-50
  • ISO Sec. 6.5.2.1
  • H&S Sec. 5.6.5 pp. 136-8
serialx
+2  A: 

This is the syntax for describing bit fields. This is a way of packing more information into a smaller amount of storage. Whereas normally a bool would take at least a byte (probably more) to represent, by using bit fields, you can combine several bools into one byte with a simple syntax.

Be careful though. As one of lesser-known areas of the language, you may run into corner cases when using them. For example, the data structures thus produced are probably not portable between processor types.

Avi