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.
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 );
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
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? :confused:
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.
Re: Reading bits using bitwise operators from 32 bit integer
Quote:
Originally Posted by
S_M_A
Create a bitfield that maps the data you get but be aware that the packing order is compiler/target dependent.
Not with extern "C" ;)
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?