why ~9 returns -10 in Python -


this question has answer here:

i trying following code , here outputs, have ideas why -10 , -11 returned? thanks.

print ~9 print ~10  -10 -11 

btw, using python 2.7.8.

thanks in advance, lin

from: python doc

the unary ~ (invert) operator yields bitwise inversion of plain or long integer argument. bitwise inversion of x defined -(x+1). applies integral numbers.

from: python doc

two's complement binary negative integers:

negative numbers written leading 1 instead of leading zero. if using 8 bits twos-complement numbers, treat patterns "00000000" "01111111" whole numbers 0 127, , reserve "1xxxxxxx" writing negative numbers. negative number, -x, written using bit pattern (x-1) of bits complemented (switched 1 0 or 0 1). -1 complement(1 - 1) = complement(0) = "11111111", , -10 complement(10 - 1) = complement(9) = complement("00001001") = "11110110". means negative numbers go way down -128 ("10000000").

~x returns complement of x - number switching each 1 0 , each 0 1. same -x - 1.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -