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

    Reading bits using bitwise operators from 32 bit integer

    Hi,

    I have a 32 bit integer variable with some value (eg: 4545) in it, now I want to read first 8 bits into uint8_t and second 8 bits into another uint8_t and so on till the last 8 bits.

    I am thinking of using bitwise operators, can anyone give some examples how to do this?


    Thanks for your help!

    -Madhu.

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

    Re: Reading bits using bitwise operators from 32 bit integer

    Here's one way
    Code:
    uint32_t val = 4545;
    uint8_t bit7_0 = static_cast<uint8_t>( val );
    uint8_t bit15_8 = static_cast<uint8_t>( val >> 8 );
    uint8_t bit23_16 = static_cast<uint8_t>( val >> 16 ); 
    uint8_t bit31_24 = static_cast<uint8_t>( val >> 24 );
    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

  3. #3
    Join Date
    Jun 2012
    Posts
    9

    Re: Reading bits using bitwise operators from 32 bit integer

    Thanks for the reply!

    Could you please look into my actual issue (below)...

    The 32 bit integer contains the below information, I need to read and save these values for further calculations...

    * bit 7 to bit 0 : (8 bits) represent the crc_checks value
    * bit 8, 9, 10 : UNUSED
    * bit 17 to bit 11 : (7 bits) represent the mappings value
    * bit 18 : UNUSED
    * bit 22 to bit 19 : (4 bits) represent the result value

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Reading bits using bitwise operators from 32 bit integer

    As I wrote you in your other thread (in Win32 forum) you should use right shift followed by bitwise AND operation (&) with 0x000000FF to extract a low order byte.
    Don't you know how shift and bitwise AND work?
    Victor Nijegorodov

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

    Re: Reading bits using bitwise operators from 32 bit integer

    Create a bitfield that maps the data you get but be aware that the packing order is compiler/target dependent.
    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

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Reading bits using bitwise operators from 32 bit integer

    Quote Originally Posted by S_M_A View Post
    Create a bitfield that maps the data you get but be aware that the packing order is compiler/target dependent.
    Not with extern "C"

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

    Re: Reading bits using bitwise operators from 32 bit integer

    Huh! As far as I know the standard doesn't define the packing order for bitfields. Do you have a reference that supports that claim?
    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