why does n&!(n-1) for n=6 gives 0 in C++ and 2 in python . it should give 2 though .
! is an invalid operator in python but in c++ it coverts a true statement to false and vice versa .
The real compliment operator is ~ .
By convention in c++ every positive number is true in c++ and 0 is false .
So ! operator converts the positive number which is true into false which translates to 0 . And the AND operation with 0 yields 0.
1 Like
Extract lowest set bit (BLSI) is -n & n
. Wikipedia (Bit Manipulation) has a decent section on this and other bitwise operations.
invalid in python , and !converts true to false in c++ , this is the reason