Can string formatting be used to convert an integer to its binary form ?

Gabriel Genellina gagsl-py at yahoo.com.ar
Thu Sep 28 22:50:47 EDT 2006


At Thursday 28/9/2006 22:07, Lawrence D'Oliveiro wrote:

>How about this: (where n is the integer you want to convert):
>
>     "".join([["0", "1"][(1 << i & n) != 0] for i in 
> range(int(math.ceil(math.log(n, 2))) - 1, -1, -1)])

Uhm... so to generate a binary string you have to import the math 
module, convert the integer to float, compute a non-standard 
logarithm, and then...
What if n<=0?
Too much, don't you think? :)


Gabriel Genellina
Softlab SRL 


	
	
		
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas




More information about the Python-list mailing list