BIT field confusing

This is a discussion on BIT field confusing within the C Programming forums, part of the General Programming Boards category; Hi people, Code: int main() { int far * equipment_memory = (int far *)0x00400010; union check_equip { unsigned int view_equip; ...

  1. #1
    C-Dumbie
    Guest

    BIT field confusing

    Hi people,

    Code:
    int main()
    {
        int far * equipment_memory = (int far *)0x00400010;
    
        union check_equip
        {
            unsigned int view_equip;
            struct bits
            {  
                unsigned num_drive: 1;
                unsigned co_processor: 1;
                                                     : 2;
                unsigned game_port : 2;  
                                                  : 2;    /*what does it mean here*/
                unsigned video_mode  : 2;                                     
    
    
            } bit_view; 
       }equipment; 
    
      /* some processing here....*/
       
      return 0;
    
    }
    what does it mean for those definition of the bit fields, co_processor and game_port?
    Would anyone please explain it clearly. Thanx!

    C-Dumbie

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    This is a bit field. the :2 notation means assign 2 bits to this member so your struct above would be 6 bits without packing.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    C-Dumbie
    Guest

    Re: BIT field confusing

    Originally posted by C-Dumbie
    Hi people,

    Code:
    int main()
    {
        int far * equipment_memory = (int far *)0x00400010;
    
        union check_equip
        {
            unsigned int view_equip;
            struct bits
            {  
                unsigned num_drive: 1;
                unsigned co_processor: 1;
                                     : 2;
                unsigned game_port : 2;  
                                   : 2;    /*what does it mean here*/
                unsigned video_mode  : 2;                                     
    
    
            } bit_view; 
       }equipment; 
    
      /* some processing here....*/
       
      return 0;
    
    }
    what does it mean for those definition of the bit fields, co_processor and game_port?
    Would anyone please explain it clearly. Thanx!

    C-Dumbie
    Can you explain more about 6 bits packing?
    what does it mean unpack and pack? how do you get 6 bits?
    I thought 10 bits used for this struct? is that right?
    C-Dumbie

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    :2 without a name just means leave a gap of two bits

    It's like this, except you don't have to keep thinking up names for things which don't interest you
    Code:
    struct bits { 
        unsigned num_drive   : 1;
        unsigned co_processor: 1;
        unsigned unused_1    : 2;
        unsigned game_port   : 2;
        unsigned unused_2    : 2;
        unsigned video_mode  : 2;
    } bit_view;
    Furthermore, you don't even have to name things you don't want to use, to for ce bit packing:

    Code:
    struct bits { 
        unsigned num_drive   : 1;
        unsigned co_processor: 1;
        unsigned :0;
        unsigned game_port   : 2;
        unsigned :0;
        unsigned video_mode  : 2;
    } bit_view;
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >I thought 10 bits used for this struct? is that right?

    Depends on your machine, there is alignment done. If you are working on a 16 bit system. Then the structure will be aligned to 16 bits. So your struct will have some extra 6 bits, which you don't use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 64 bit testing
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2007, 02:20 AM
  2. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 08:28 PM
  3. How can I parse data from a file
    By figo2476 in forum C Programming
    Replies: 5
    Last Post: 08-19-2005, 08:07 AM
  4. binary numbers
    By watshamacalit in forum C Programming
    Replies: 4
    Last Post: 01-14-2003, 10:06 PM
  5. 16 bit or 32 bit
    By Juganoo in forum C Programming
    Replies: 9
    Last Post: 12-19-2002, 06:24 AM

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