I am operating on individual bits of two integers, (i am using g++ for compilation on Ubuntu machine).
In some intermediate step, I have the bit representations as
q = 11000000000000000000000000000000
q_1 = 00000000000000000000000000000001
Now I want to check whether unit's places of q and q_1 are both same or not. so, I am checking (*q)&1==q_1
in the if condition, and its working fine.
But whenever I want to check that unit's place of q is 0 and that of q_1 is 1, I thought I should do ((*q)&1==0) && (q_1==1)
, but it is not working out as expected. For debugging, I cout
ed the values of ((*q)&1==0)
and (q_1==1)
individually and they got printed as 1
. However, the value of ((*q)&1==0) && (q_1==1)
got printed as 0. Why?
* EDIT : * In the function, q was passed by reference, so I am using *q to get the value..
In C and C++, the bitwise &
operator actually has lower precedence than the equivalence operator ==
. You'll need to wrap your bitwise operators in parentheses.
So:
((*q)&1==0) && (q_1==1)
should be:
(((*q)&1)==0) && (q_1==1)
See: http://en.cppreference.com/w/cpp/language/operator_precedence
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.