CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    15

    Unhappy declaration with width

    Can any one explain me this declaration,
    unsigned int is_key : 1;
    And where can this be used?
    What operations can't be performed and what opeations can be done using this variable?operations like +,-,&,&&..
    Thanks in advance,
    masg
    Last edited by masg; February 6th, 2006 at 12:48 AM. Reason: more info

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: declaration with width

    unsigned int is_key : 1;
    That is a bitfied.

    And where can this be used?
    You can use them when a whole byte might be a waste. You might find them very usefull when dealing with various communication protocols.

    What operations can't be performed and what opeations can be done using this variable?operations like +,-,&,&&
    You cannot take the address of the bitfield. You can use any of the +,-,&,&&.
    Har Har

  3. #3
    Join Date
    May 2004
    Posts
    75

    Re: declaration with width

    This declares a "C" Bitfield e.g. in a struct. AFAIK only integral types (signed, unsigned, int) are allowed. You basically say that the is_key variable should use 1 bit. Example:
    Code:
    struct SFoo {
    unsigned short hand : 1;
    unsigned short finger : 3;
    unsigned short age : 7;
    };
    This structure uses 2 bytes. hand ranges from [0;1], finger from [0;7] and age from [0;127].

    You can use at least +/- operators as usual and the values will be kept in range. [edit]I'm not sure about other operators.[/edit]

    It is generally not recommended to use bitfields except if it really helps and you can make assumptions about the data types, but it can truely be a porting nightmare. Moreover, although less memory is used, you can expect that access to these data members is somewhat slower.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured