#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    19
    Rep Power
    0

    Reading bits using bitwise operators


    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
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Well since you know it's 8 bits, then x >> 8 will come into it somewhere.

    The only other step is masking off the unwanted bits with say x & 0xnnnn
    Can you figure out a hex value representing 11111111 (8 bits) ?

    Draw it on paper if you're still stuck.

    Yes, I'm being deliberately vague to try and make you think about it. I'm resisting just posting the one line of code it takes to spoon-feed you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Another alternative, if you want to access the bytes that make up the int, is to create a union of the int and a char array. Note that endien-ness matters when you access the parts that way, but it might be slightly more intuitive than bit shifting.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by mitakeet
    Another alternative, if you want to access the bytes that make up the int, is to create a union of the int and a char array. Note that endien-ness matters when you access the parts that way, but it might be slightly more intuitive than bit shifting.
    Endianness is a pretty big issue, though, isn't it? I think this would be a really great solution if endianness weren't an issue, but since it is, I would actually advise against it, since it's more difficult to implement it portably.

IMN logo majestic logo threadwatch logo seochat tools logo