c++ - Unexpected results from bitwise operation why (1 | 1 & 2) gives 1 instead of 2? -
cannot simple calculation straight using visual studio 2010
std::cout<<(1 | 1 & 2); //gives value 1, expecting 2 since 1 | 1 = 2 , & 2 gives 2 std::cout<<( (1 | 1) & 2); //gives value 0, why different above?
first case:
1 & 2 // since &
has higher precedence |
01 &10 ---- 00
and or result 1,
1 or 0 ----- 1
in second case
1 | 1
01 or 01 ---- 01
and , result 2,
01 & 10 ----- 0
Comments
Post a Comment