Thread: Bitfields

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    37

    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. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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;
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Click_here View Post
    I'd make sure that the enum is giving the values that you are expecting
    In this case C makes sure of that.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Subsonics View Post
    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.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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:
    */
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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"
    Fact - Beethoven wrote his first symphony in C

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also cross-posted
    Bitfields - Dev Shed
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    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. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BitFields -- is this possible?
    By trish in forum C Programming
    Replies: 6
    Last Post: 01-22-2012, 02:22 AM
  2. bitfields
    By Tiago in forum C Programming
    Replies: 2
    Last Post: 05-16-2010, 09:21 AM
  3. Structure Bitfields
    By samGwilliam in forum C Programming
    Replies: 6
    Last Post: 09-04-2009, 09:45 PM
  4. portable bitfields
    By Yarin in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2007, 01:39 PM
  5. bitfields
    By Hunter2 in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 05:21 PM