Python Bitwise Invert Oddity

By Xah Lee. Date:
# -*- coding: utf-8 -*-
# python

# binary invert
print( "{:b}".format(~ 0b1) )   # prints -10 WTF?

here, we have a binary number 1. Its bitwise inversion should be 0. But python gives “-10”, wtf? (same in python 2.7.3 and 3.2.3)

it seems, python now represent numbers by certain infinite sequence of bits, and negative numbers starts with 1. But i don't quite get it.

i haven't worked with bitwise operations before. Is this the behavior in other langs?

If you have a question, put $5 at patreon and message me.

Python 3

Lang Detail

  1. Print Version String
  2. Quoting String
  3. String Operations
  4. Format String
  5. Operators
  6. Object, ID, Type


  1. Traverse Directory
  2. Sort List, Matrix, Object
  3. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  4. Python 3 Closure
  5. Python 2 and 3 Difference