Thread: Accessing bit fields using a union problem.

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Accessing bit fields using a union problem.

    Hello - I changed my code from my last post:
    http://cboard.cprogramming.com/showthread.php?t=109945
    so that the struct of bit fields is unionized with a UINT32 word:

    Code:
    #include <stdio.h>
    
    
    typedef union 
    {
        struct {
    
        /// Reserved - read as 0
        unsigned reserved : 30;
    
        /** This bit contains the interrupt status. Reading this register
         * clears this bit.
         */
        unsigned stats : 1;
    
        /** This field masks the interrupt.
         *	0 - Masked
         *	1 - Enabled
         */
        unsigned maskn : 1;
    
        } bits;
    
        unsigned long word;
    
    } volatile RegStruct;
    
    RegStruct *hwReg
    When I print out hwReg.word I get the entire correct 32 bits - BUT when I print out one of the bits hwReg.bits.maskn, I get 0x00000000 - I think it still may be accessing the bits as bytes even though the struct is unionized with UINT32. what is going on?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I assume you're printing with %x? %x says "the thing I am handing you is an unsigned int". If the thing you are printing is not an unsigned int, well, then, that's too bad -- it's going to be printed as an unsigned int.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Why yes we are printing with a %x and all the bytes that are displayed are positive status bits, so they are unsigned. Can you please help still?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Show us the code that does the printing.

    --
    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.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Code:
    void putHex(UINT32 x)
    {
        printf("0x&#37;x ", x);
    }
    but could it have to do with something about union and struct in my code instead?

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Have you by any chance intialized RegStruct.word as well as RegStruct.bits.maskn or any other bit variable? That is not advisable when using a union as they share the same memory of the larger of the two defined variables.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, can you please post all (or at least a decent part) of the code, as we need some context to answer this question, I think.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. A Bit Of A Problem Here...
    By LordVirusXXP in forum C++ Programming
    Replies: 10
    Last Post: 12-20-2002, 04:11 AM
  3. Accessing a specific bit
    By Zewu in forum C++ Programming
    Replies: 7
    Last Post: 07-02-2002, 07:55 AM
  4. struct - bit fields
    By chubaka in forum C Programming
    Replies: 4
    Last Post: 02-01-2002, 07:37 PM
  5. Bit fields
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 01-22-2002, 02:01 PM