CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2009
    Posts
    41

    Bit Manipulation

    Hey all,

    I have a value that is stored in a 64 bit integer that I'd like to break down into a bunch of bit fields, ie byteOffset: 6 etc. I've created a struct with a two unions: one for the entire value and one for the broken down values. I was told shifting is not a good method, does anyone know what the best way is?

    Thanks,

    CoDEFRo

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Bit Manipulation

    I was told shifting is not a good method, does anyone know what the best way is?
    Shifting. Somebody told you wrong If you want to find 1 bit, you can use bit-masking.

    ps : why are you using unions ? They where invented (AFAIK) for memory-use reduction. That's not a problem anymore.

  3. #3
    Join Date
    May 2009
    Posts
    41

    Re: Bit Manipulation

    Quote Originally Posted by Skizmo View Post
    Shifting. Somebody told you wrong If you want to find 1 bit, you can use bit-masking.

    ps : why are you using unions ? They where invented (AFAIK) for memory-use reduction. That's not a problem anymore.
    I'm not sure tbh lol, it's the way my manager told me to do it, so that's how I'm doing it. One of the reasons he told me to avoid shifting was if the command changes, ie byteOffset: 6 to byeOffset: 10 than the shift is invalid. I could just be misunderstanding him though.

    So if I wanted to get the bits 17:6 of a 64 bit int, would I shift it right 6 spaces, and then store it in something like uint_64 address: 12? ie:

    Code:
    address = command >> 6;

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Bit Manipulation

    Generally, obtaining a specific bit requires both shifting and masking. Although, it's also possible to use bitfields in a struct.

  5. #5
    Join Date
    May 2009
    Posts
    41

    Re: Bit Manipulation

    Thanks for your input guys. I figured out why he told me to do that, it's actually a cool trick that's described here: http://www.cplusplus.com/forum/articles/12/. If you use a union in the manner I described, you don't have to do any shifts, the values for each bit field are essentially set automatically.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Bit Manipulation

    Be aware, that page is about extracting entire bytes. You need to do a bit more to pull out individual bit values, but it can be done in a similar manner.

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Bit Manipulation

    Also be aware that bitfields are not portable in the respect that packing order/alignment and so on are not standardized.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    May 2009
    Posts
    41

    Re: Bit Manipulation

    Thanks guys. My structure looks like this:

    Code:
     typedef struct addressMap
    {
    	union
    	{
    		struct
    		{
    			uint64_t address;
    			unsigned int byteCount;
    			unsigned int src;
    			unsigned char data;
    
    		}*f1;
    		struct
    		{
    			uint64_t	byteOffset:		6;
    			uint64_t	address: 		              12;
    			uint64_t	bank: 			2;
    			uint64_t	unused:			44;
    			unsigned int byteCount;
    			unsigned int src;
    			unsigned char data;
    		}*f2;
    	};
    };
    I believe as long as the memory locations align in the two structs, it should work?

    Also wanted to ask, for the struct variables f1 and f2, would be reasoning to make these not a pointer, or to make them a pointer?

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Bit Manipulation

    It's a long time since I wrote a union. I think I had an overdose of them from a multi-platform source long time ago...

    Anyway I think I would have written it like this
    Code:
    typedef struct
    {
      union
      {
        uint64_t all;
        struct
        {
          uint64_t byteOffset : 6;
          uint64_t address    : 12;
          uint64_t bank       : 2;
          uint64_t            : 44;
        } parts;
      } address;
      unsigned int  byteCount;
      unsigned int  src;
      unsigned char data;
    } addressMap;
    to not have to maintain more fields than neccessary. You don't have to have a name for unused fields and in fact in this case you don't have to have it at all. The remaining 44 bits will be present anyway since the underlying type is 64 bits.

    As said before bitfields are not fully specified by the standard. In MSVC the byteOffset bits will be the least significant bits in 'all'. In another compiler they might be the most significant bits.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

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