|
-
June 1st, 2012, 12:05 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, 01:10 AM
#2
Re: Reading bits using bitwise operators from 32 bit integer
Microsoft already made the most of the work for you. Have a look at Window Macros
What you need are HIBYTE, HIWORD. LOBYTE. LOWORD
Victor Nijegorodov
-
June 1st, 2012, 01:14 AM
#3
Re: Reading bits using bitwise operators from 32 bit integer
Okay, but I need this in C under Linux platform.
-
June 1st, 2012, 01:36 AM
#4
Re: Reading bits using bitwise operators from 32 bit integer
 Originally Posted by madhu542
Okay, but I need this in C under Linux platform.
Then why do you ask it in the Windows forum? 
C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum.
There is another forum for linux:
C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.
As for your question - use bitwise operator & with 0x000000FF to extract the lowest byte, then shift the original "32 bit integer" 8 bits to the right and so on...
Victor Nijegorodov
-
June 1st, 2012, 07:35 AM
#5
Re: Reading bits using bitwise operators from 32 bit integer
 Originally Posted by madhu542
Okay, but I need this in C under Linux platform.
Code:
// bitwise.cpp
#include <stdio.h>
typedef union U32
{
unsigned u32;
struct {
unsigned short w2;
unsigned short w1;
} w32;
struct {
unsigned char b4;
unsigned char b3;
unsigned char b2;
unsigned char b1;
} b32;
} U32;
typedef struct {
unsigned b4:8;
unsigned b3:8;
unsigned b2:8;
unsigned b1:8;
} bb32;
int main()
{
U32 uni32;
uni32.u32 = 0x01020304;
bb32& bb = *(bb32*)&uni32.u32;
printf ("%08x\n", uni32.u32);
printf ("%04x:%04x\n", uni32.w32.w1, uni32.w32.w2);
printf ("%02x:%02x:%02x:%02x\n", uni32.b32.b1, uni32.b32.b2, uni32.b32.b3, uni32.b32.b4);
printf ("%02x:%02x:%02x:%02x\n", bb.b1, bb.b2, bb.b3, bb.b4);
return 0;
}
Code:
igor@igor-desktop:~/Temp$ g++ bitwise.cpp -o bitwise.out
igor@igor-desktop:~/Temp$ ./bitwise.out
01020304
0102:0304
01:02:03:04
01:02:03:04
Best regards,
Igor
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
|