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

Frederic Rentsch anthra.norell at vtxmail.ch
Fri Sep 29 05:08:18 EDT 2006


bearophileHUGS at lycos.com wrote:
> Frederic Rentsch:
>   
>> Good idea, but shorter with ->
>>  >>> SE.SE ('se_definition_files/int_to_binary.se') ('%X' % 987654321)
>> '00111010110111100110100010110001'
>>     
>
> Note that your version keeps the leading zeros.
> Have you tested the relative speeds too?
> (I'll probably have to learn to use SE.)
>
> Bye,
> bearophile
>
>   
If you say speed, I presume you mean speed of execution. No I have not 
tested that. I know it can't be fast on a test bench. After all, SE is 
written in Python. I did a first version fifteen years ago in C, am 
still using it today on occasion and it runs much, much faster than this 
Python SE. This SE here could be done in C if it passes the test of 
acceptance.
     Professionals need to measure execution speed as a part of 
documenting their products. I am not a professional and so I am free to 
define my own scale of grades: A (fast enough) and F (not fast enough). 
I have yet to encounter a situation where SE gets an F. But that says 
less about SE than about my better knowledge which prevents me from 
using SE to, say, change colors in a 50 Mb bitmap. Obviously, "fast 
enough" and "not fast enough" pertains not to code per se, but to code 
in a specific situation. So, as the saying goes: the proof of the 
pudding ...
     Another kind of speed is production speed. I do believe that SE 
rather excels on that side. I also believe that the two kinds of speed 
are economically related by the return-on-investment principle.
     The third kind of speed is learning speed. SE is so simple that it 
has no technical learning curve to speak of. It's versatility comes from 
a wealth of application techniques that invite exploration, invention 
even. Take leading zeroes:

Leading zeroes can be stripped in a second pass if they are made 
recognizable in the first pass by some leading mark that is not a zero 
or a one. ([^01]; I use "@" in the following example). To purists this 
may seem hackish. So it is! And what's wrong with that if it leads to 
simpler solutions?

 >>> Hex_To_Binary = SE.SE ('0=0000 1=0001 2=0010 3=0011 4=0100 5=0101 
6=0110 7=0111 8=1000 9=1001 A=1010 a=1010 B=1011 b=1011 C=1100 c=1100 
D=1101 d=1101 E=1110 e=1110 F=1111 f=1111 | ~[^01]0*~=')
 >>> Hex_To_Binary.set (keep_chain = 1)
 >>> Hex_To_Binary ('@%x' % 1234567890)
'1001001100101100000001011010010'
 >>> Hex_To_Binary.show ()

... snip ...

Data Chain
----------------------------------------------------------------------------------
  @499602d2
0 
--------------------------------------------------------------------------------
  @01001001100101100000001011010010
1 
--------------------------------------------------------------------------------
  1001001100101100000001011010010
----------------------------------------------------------------------------------


Frederic

(The previously posted example "Int_To_Binary = SE.SE (SE.SE ( ..." was 
a typo, or course. One (SE.SE  does it. Sorry about that.)




More information about the Python-list mailing list