why sometimes bitwise operations do not work properly?(even though logically they are correct.)

Give some examples

If you’re using x << y to perform the operation x/=(2^y) then there are chances that the MSB of the number becomes 1 and the number suddenly becomes negative. Beware of doing this. That’s all I can think of …

1 Like

Can you give an example please…

x<<y will compute x*=(2^y). So the MSB becoming one is essentially integer overflow.

1 Like

I guess you mean something like this [(1 << 35) / (1 << 5) == (1 << 30)].

But operation (1 << 35) is treated as “int”(32bit) in this context. (So, bring over-flow.)

Writing (1LL << 35) is right way, if you intend to “long long”(64bit) operation.

1 Like