1. ## Bitfields

Hello

I am currently updating a project that uses bitfields heavily
I am planning to use them as well. My question is if the following is correct, appropriate or a good idea

first the bitfield

Code:
```struct{
WORD somefield:1;
WORD otherfield:1;

WORD MYFIELD:2; //this is my field

}somebitfield;```
Yeah, I know the standard recommends using unsigned ints for that but the code already use the user defined WORD.

Ok so I have a 2 bit field, so I guess that means it can take 4 values right?

So I define

Code:
`enum{NOTHING=0, ONE, TWO, THREE} myenum;`
So my question is can I safely do this

Code:
```if(somebitfield.MYFIELD==NOTHING){

}```
or even

Code:
```switch(somebitfield.MYFIELD)
{
case NOTHING:

}```
Any help will be greatly appreciated.

Kansai

2. I'd make sure that the enum is giving the values that you are expecting

Code:
`enum{NOTHING=0, ONE=1, TWO=2, THREE=3} myenum;`

3. Originally Posted by Click_here
I'd make sure that the enum is giving the values that you are expecting
In this case C makes sure of that.

4. Originally Posted by Subsonics
In this case C makes sure of that.
I've used a few non-ANSI compliant compilers, and I've always been a bit worried that it might not give expected values (even with the ANSI compliant compilers).

I'm assuming that the OP's compiler is ANSI compliant and Subsonics is probably right - Disregard my last post.

5. WORD is probably an unsigned int. You should check that out.

I'm sure the comparisons you propose are completely safe.

The code below is just one example, one machine, one compiler, but it shows some of the implicit bit manipulation.
Code:
```#include <stdio.h>

struct {
unsigned a:1;
unsigned b:1;
unsigned c:2;
} bits;

enum {NOTHING=0, ONE, TWO, THREE};

int main(void) {
bits.c = THREE;
if (bits.c == THREE)
printf("yo\n");
return 0;
}
/*
# Partial disassembly:

# bits.c = THREE;
movzbl	_bits, %eax   # movzbl means "move zero-extended byte to long"
orl	\$12, %eax         # 12 == 00000000 00000000 00000000 00001100
movb	%al, _bits

# if (bits.c == THREE)
movzbl	_bits, %eax
andl	\$12, %eax
cmpb	\$12, %al
jne	L2
movl	\$LC0, (%esp)  # LC0 is the address of the string "yo\0"
call	_puts
L2:
*/```

6. Just for those playing at home, I found the part of the C99 draft that covers what I was talking about -
6.7.2.2 Enumeration specifiers
Section 3.
"... Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant"

7. Also cross-posted
Bitfields - Dev Shed

8. Thank you for the replies
and thanks oogabooga

By the way talking about C99 , I read somewhere that the standard recommends only using unsigned ints on bit fields. My code uses user defined WORDS and BYTEs but I wonder, if bitfields are just that : bits why the need of types?

I mean for ex

Code:
```struct{
unsigned int field1:1;
unsigned int field2:2;

}```
field1 is one bit and field2 is two bits. Then why the need to put "int" or any other type??

9. Here's what the standard says:
6.7.2.1
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.
So bit-fields can be signed.