integer to binary...

Anton Vredegoor anton.vredegoor at gmail.com
Thu Jun 1 15:54:45 EDT 2006


nicolasg at gmail.com wrote:

> does anyone know a module or something to convert numbers like integer
> to binary format ?
> 
> for example I want to convert number 7 to 0111 so I can make some
> bitwise operations...

  >>> def bits(i,n):
	 return tuple((0,1)[i>>j & 1] for j in xrange(n-1,-1,-1))

  >>> bits(7,4)
  (0, 1, 1, 1)

Anton



More information about the Python-list mailing list