[Tutor] First realscript + Game of Life

Luke Paireepinart rabidpoobear at gmail.com
Sat Nov 11 17:18:59 CET 2006


Kent Johnson wrote:
> Luke Paireepinart wrote:
>   
>> <me attempting to explain how Hieu's Binary Conversion function was 
>> working>
>
> I think you are a bit confused here.
>   
Quite possible!
> It's important to make a distinction between the way a number is 
> actually stored in the computer and the string that is created when a 
> number is printed.
>
> Most computers store integers as binary numbers. So the number 21 for 
> example will be stored as the bit pattern 00010100. In order to print 
> this number, Python converts it to a string containing the two 
> characters '2' and '1' (which are themselves stored as the bit patterns 
> 00110010 and 00110001) and outputs those characters to the console. So 
> now we have two representations, the binary number stored in the 
> computer memory and the string representation of the base 10 
> representation of the number.
>   
I was trying to make it clear that Python wasn't directly accessing the 
binary from memory with this function Carlos had.
The function just mathematically converted a base-10 number into a 
base-2 number.

The function, if you recall (from Hieu Hoang):
 >Hi Carlos,

>I hope this module would help you with the binary conversion part:

> def tobits(inte,size = 3):
>     """Copy an integer's bits from memory"""
>     s=''
>     for i in range(size):
>         s += str((inte & (1<<i)) >>i)
>     #bits are extracted right-to-left
>     s = s[::-1] #reverse the result string
>     print s
>     return list(s)

And what Carlos said in response to it,
 > Hey Rooy, so its possible to copy binary numbers from memory? I had 
the impression that this could be done, but obviously it is too much for 
me. This way is going to be faster than the hack that I tried before, 
right? Thanks for the  module : )
I'm not sure why Carlos calls them Rooy.
Anyway, I just wanted to make sure that Carlos knew that python wasn't 
directly copying the binary representation from memory into a list,
Python was getting the base-10 integer representation out of the binary, 
and then the function converted this back again into base2 using
binary shifts.  I.E. the sentence 'copy binary numbers from memory' made 
me think Carlos thought the binary was directly copied into a list.
I also clarified that it's not a 'module' it's a function.

Am I still confused, Kent?


More information about the Tutor mailing list