Depending on how many binary digits you want, subtract from the number (2 n ):
>>> bin((1 << 8) - 1)
'0b11111111'
>>> bin((1 << 16) - 1)
'0b1111111111111111'
>>> bin((1 << 32) - 1)
'0b11111111111111111111111111111111'
UPDATE
Using the following expression, you can cover both positive and negative cases:
>>> bin(((1 << 32) - 1) & -5)
'0b11111111111111111111111111111011'
source
share