Bitwise OR?

Clemens Hepper ethrandil at gmx.net
Fri Mar 24 06:55:18 EST 2006


Hello,

I've written a little (optimized) method to get a bit-string:

def bitstringneg(number, digits=32):
  """optimized for negative numbers"""
  result = ""
  for a in xrange(digits):
    if number & 1:
      result += '1'
    else:
      result += '0'
    number >>= 1
  return result

def bitstringpos(number):
  """optimized for positive numbers"""
  result = ""
  while number:
    if number & 1:
      result += '1'
    else:
      result += '0'
    number >>= 1
  return result

def bitstring(number, digits=32):
  """lsb------>msb"""
  result = ""
  if number < 0:
    return bitstringneg(number, digits)
  else:
    return bitstringpos(number)

BTW: Is there something like a sizeof() method for int numbers?

mfg
- eth



More information about the Python-list mailing list