# Bitwise OR?

Clemens Hepper ethrandil at gmx.net
Mon Mar 27 01:10:42 CEST 2006

```Adam DePrince wrote:
>> BTW: Is there something like a sizeof() method for int numbers?
>
> import struct
> help( strict.calcsize )
Mh, that doesn't do what i want. I'd like to have something like:

def size(number):
return sizeof(number)

> Why one bit at a time?

Good question...

Here my new approach based on your idea:

_digits = { 0:"0000", 1:"1000", 2:"0100", 3:"1100",
4:"0010", 5:"1010", 6:"0110", 7:"1110",
8:"0001", 9:"1001", 0xa:"0101", 0xb:"1101",
0xc:"0011", 0xd:"1011", 0xe:"0111", 0xf:"1111"}

def bitstring2(number):
"""lsb------>msb"""
rlist = list()
if number >= 0:
while number:
rlist.append( _digits[number & 0xF] )
number >>= 4
else:
while number != -1:
rlist.append( _digits[number & 0xF] )
number >>= 4

return ''.join(rlist)

This was faster for positive numbers. For negative numbers yours was
faster, but my version handles those numbers different.

Your version fails for Large numbers since hex( long ) returns
something like "0xFFFL" instead of "0xfff".