Bit fields problem

This is a discussion on Bit fields problem within the C Programming forums, part of the General Programming Boards category; Hi all, I am experiencing a problem with bit fields. I have simplified my original code to a code snippet ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Bit fields problem

    Hi all,
    I am experiencing a problem with bit fields. I have simplified my original code to a code snippet below:

    In the code below, what is wrong with the static const initialization?
    I am expecting name3 and name4 to have a value of 7 in variable num OR a value of 1 for a,b,c.

    However I see only value of 1 for "a" only in those 2 cases.

    The first two name1 and name2 work fine.


    Code:
    
    typedef union 
    {
        struct 
        {
            unsigned char a         : 1,
                                    b          : 1,
                                    c         : 1,
                                              : 5;
        };
        unsigned char num;
    }STNAME;
        
    
    static const STNAME c1 = 
    {
    	(0x07)
    };
    
    static const STNAME c2 = 
    {
    	(0x01 | 0x02| 0x03)
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	STNAME name1;
    	name1.a = 0x1;
    	name1.b = 0x1;
    	name1.c = 0x1;
    
    	STNAME name2;
    	name2.num=0x7;
    
    	STNAME name3;
    	name3 = c1; // does not assign 0x7 (or 1 to each of a,b,c)
    
    	STNAME name4;
    	name4 = c2; // does not  assign 0x7(or 1 to each of a,b,c)
    
    	return 0;
    }
    Thanks .
    Last edited by johndirect; 10-21-2008 at 12:11 AM. Reason: fixed a variable name

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess would be that if you assign 1 to a, and then print num, you get 128.

    The bit-order for bit-fields is compiler dependant, so you would have to take that into account when ordering the bits in the bitfield struct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > In the code below, what is wrong with the static const initialization?
    You can't initialise a union IIRC.

    Some compilers allow you to initialise the first member, in which case it would be something like
    Code:
    static const STNAME c1 = 
    {
    	{ 1, 1, 1 }
    };
    The rest is as matsp says.
    Everything about bit-fields is implementation dependent. Trying to overlay a bit-field with a union with something else is never going to give you good answers you can rely on.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. bit fields before non-bit fields . . .
    By dwks in forum C Programming
    Replies: 10
    Last Post: 10-13-2005, 02:36 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. bit clearing problem
    By Buckshot in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2005, 07:36 PM
  5. Bitwise Operators....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 21
    Last Post: 04-09-2003, 06:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21