help on masking

hapa_at_1's Avatar
Light Poster
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.