help on masking

Discussion in 'C' started by hapa_at_1, Oct 28, 2006.

  1. hapa_at_1

    hapa_at_1 New Member

    Joined:
    Oct 28, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    how does masking works in c++
    how can i extract the position of my desire from a series of bits given to me

    for instance if

    16 in bit is

    10000
    how can i get the 3rd number
    please adivce
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    You need to do the Bitwise AND (&) / Bitwise OR (|) to do the masking

    Truth table is as follows
    OR
    Code:
    +-----+-----+-----+
    | OP1 | OP2 | O/P |
    +-----+-----+-----+
    |  0  |  0  |  0  |
    |  0  |  1  |  1  |
    |  1  |  0  |  1  |
    |  1  |  1  |  1  |
    +-----+-----+-----+
    
    AND
    Code:
    +-----+-----+-----+
    | OP1 | OP2 | O/P |
    +-----+-----+-----+
    |  0  |  0  |  0  |
    |  0  |  1  |  0  |
    |  1  |  0  |  0  |
    |  1  |  1  |  1  |
    +-----+-----+-----+
    
    Or it with all Zeros and you get the the masked digit or AND it with 1.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice