binary number format ? format character %b or similar.

Discussion in 'Python' started by Ken Starks, Jun 22, 2008.

  1. Ken Starks

    Ken Starks Guest

    I'm was wanting to format a positive integer in binary,
    and not finding it--to my surprise--I rolled my own version.

    Is this already in python, or have I missed it somewhere?

    I have Googled around a bit, and found a few threads on
    the subject, but they all seem to fizzle out.

    (e.g. : INPUT 35, OUTPUT "100011" )
     
    Ken Starks, Jun 22, 2008
    #1
    1. Advertising

  2. Ken Starks

    weheh Guest

    I don't know if you found this example:
    http://www.daniweb.com/code/snippet285.html

    --
    > I'm was wanting to format a positive integer in binary,
    > and not finding it--to my surprise--I rolled my own version.
    >
    > Is this already in python, or have I missed it somewhere?
    >
    > I have Googled around a bit, and found a few threads on
    > the subject, but they all seem to fizzle out.
    >
    > (e.g. : INPUT 35, OUTPUT "100011" )
     
    weheh, Jun 22, 2008
    #2
    1. Advertising

  3. Ken Starks

    Ken Starks Guest

    weheh wrote:
    > I don't know if you found this example:
    > http://www.daniweb.com/code/snippet285.html
    >


    Thanks for that. The offerings are very similar to the
    algorithms I wrote myself.

    It wasn't the solution I was after,really; that's
    easy. It was whether anything had found its way into
    the standard library.
     
    Ken Starks, Jun 22, 2008
    #3
  4. Ken Starks

    Mensanator Guest

    On Jun 22, 4:07�pm, Ken Starks <> wrote:
    > weheh wrote:
    > > I don't know if you found this example:
    > >http://www.daniweb.com/code/snippet285.html

    >
    > Thanks for that. The offerings are very similar to the
    > algorithms I wrote myself.
    >
    > It wasn't the solution I was after,really; that's
    > easy. It was whether anything had found its way into
    > the standard library.


    Isn't that coming in Python 3.0?

    You could also use gmpy, which has a lot of
    other bit-functionality in addition to displaying
    them.

    >>> for i in xrange(8):

    print 'i:',gmpy.digits(i,2).zfill(8),
    print '# of 1-bits:',gmpy.popcount(i),
    print 'find LS 1-bit:',gmpy.scan1(i),
    print 'bit difference:',gmpy.hamdist(i,2**8-1)


    i: 00000000 # of 1-bits: 0 find LS 1-bit: -1 bit difference: 8
    i: 00000001 # of 1-bits: 1 find LS 1-bit: 0 bit difference: 7
    i: 00000010 # of 1-bits: 1 find LS 1-bit: 1 bit difference: 7
    i: 00000011 # of 1-bits: 2 find LS 1-bit: 0 bit difference: 6
    i: 00000100 # of 1-bits: 1 find LS 1-bit: 2 bit difference: 7
    i: 00000101 # of 1-bits: 2 find LS 1-bit: 0 bit difference: 6
    i: 00000110 # of 1-bits: 2 find LS 1-bit: 1 bit difference: 6
    i: 00000111 # of 1-bits: 3 find LS 1-bit: 0 bit difference: 5
     
    Mensanator, Jun 23, 2008
    #4
  5. Ken Starks

    Ken Starks Guest

    Mensanator wrote:
    > On Jun 22, 4:07�pm, Ken Starks <> wrote:
    >> weheh wrote:
    >>> I don't know if you found this example:
    >>> http://www.daniweb.com/code/snippet285.html

    >> Thanks for that. The offerings are very similar to the
    >> algorithms I wrote myself.
    >>
    >> It wasn't the solution I was after,really; that's
    >> easy. It was whether anything had found its way into
    >> the standard library.

    >
    > Isn't that coming in Python 3.0?


    Thanks for the tip! following which, I found
    information at:

    http://docs.python.org/dev/3.0/library/string.html#formatstrings

    >
    > You could also use gmpy, which has a lot of
    > other bit-functionality in addition to displaying
    > them.
    >


    Yes, that'll be useful. Thanks again.
     
    Ken Starks, Jun 23, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,931
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Ben Bacarisse
    Replies:
    8
    Views:
    364
    Ben Bacarisse
    Dec 22, 2011
  3. 88888 Dihedral
    Replies:
    35
    Views:
    1,152
    88888 Dihedral
    Jan 4, 2012
  4. Bob Sanders
    Replies:
    5
    Views:
    233
    Ryan Lewis
    May 30, 2008
  5. Fei Liu
    Replies:
    21
    Views:
    2,416
    John Bokma
    Dec 16, 2006
Loading...

Share This Page