bitwise operation....help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerico
    New Member
    • Sep 2006
    • 39

    bitwise operation....help

    Hi.Given a number ,say 15, whose binary equivalent is 1111, how can the bit value of any position can be obtained using bitwise operators?Thank s for any help.

    Jerico
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    select the bit you want, say 3 bit (0100 = 0x04) AND the value with that bit and shift right by the number of 0 (2 in this case)

    bit = (number & 0x04) >> 2;

    bit will have either the value 0 or the value 1 depending on the value of the third bit in number.

    Preferably bitwise operations should always be carried out on unsigned integers.

    Comment

    Working...