educative.io

Educative

Bitwise NOT Operator

Lesson Bitwise Operator

I think the comment in the examples is wrong:

-11 -> Binary value = 1111 0101

this isn’t the value of -11 right?

Thank you very much for your feedback. We have made the required changes.

Thank you for your quick response.

This hasn’t been updated in the lesson yet, correct? I’ve been sitting here for such a long time trying to figure out why ~(10) is equal to -11…

If 10 = 1010, then wouldn’t ~10 = 0101 = 5?
Or 10 = 0000 1010, then ~10 = 1111 0101 right? which I have no idea how that equates to -11.

I wish this page did a better job of explaining this operation, rather than just saying “if you apply the same rules from OR and AND, you should be able to figure out NOT” and just dismissing it. I guess I will have to learn how bitwise NOT operations in Python work at a later time.

Agree, skipping the first thing which is not straight forward to understand is very frustrating.

1111 0101 is -11 because -2^7 + 2^6 + 2^5 + 2^4 + 2^2 + 2^0 = -128+64+32+16+4+1 = -11
bitwise not would negate all leading zeros for a positive number
in the case of 20 (binary 010100) ~20 would be 101011 = -2^5 + 2^3 + 2^1 + 2^0 = -32 + 8 + 2 + 1 = -21