|
-
June 1st, 2012, 01:56 AM
#1
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.
-
June 1st, 2012, 02:33 AM
#2
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 );
-
June 1st, 2012, 03:24 AM
#3
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
-
June 1st, 2012, 03:37 AM
#4
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
-
June 1st, 2012, 03:20 PM
#5
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.
-
June 1st, 2012, 11:08 PM
#6
Re: Reading bits using bitwise operators from 32 bit integer
 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"
-
June 2nd, 2012, 06:19 AM
#7
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|