CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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
    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

    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

  3. #3
    Join Date
    Jun 2012
    Posts
    9

    Re: Reading bits using bitwise operators from 32 bit integer

    Okay, but I need this in C under Linux platform.

  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

    Quote Originally Posted by madhu542 View Post
    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

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Reading bits using bitwise operators from 32 bit integer

    Quote Originally Posted by madhu542 View Post
    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 ("&#37;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
  •  





Click Here to Expand Forum to Full Width

Featured