integer to binary...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nicolasg@gmail.com

    #1

    integer to binary...

    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...

    Thanks

  • Schüle Daniel

    #2
    Re: integer to binary...

    nicolasg@gmail. com schrieb:[color=blue]
    > does anyone know a module or something to convert numbers like integer
    > to binary format ?[/color]

    unfortunately there is no builtin function for this
    [color=blue][color=green][color=darkred]
    >>> int("111",2)[/color][/color][/color]
    7[color=blue][color=green][color=darkred]
    >>> str(7)[/color][/color][/color]
    '7'[color=blue][color=green][color=darkred]
    >>> str(7,2)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: str() takes at most 1 argument (2 given)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    int, str are not symmetrical
    I hope this will change in future

    <rebel on>

    you can use Ruby's 7.to_s(2) for this
    irb(main):001:0 > 7.to_s(2)
    => "111"
    irb(main):002:0 > 7.to_s(3)
    => "21"
    irb(main):003:0 >

    </rebel on>
    [color=blue]
    > for example I want to convert number 7 to 0111 so I can make some
    > bitwise operations...[/color]

    you can use bitwise operations on int's anyway

    7 & 3 == 3
    (1 << 20) | (1 << 10) == 2**20+2**10

    and so on

    Comment

    • Grant Edwards

      #3
      Re: integer to binary...

      On 2006-06-01, nicolasg@gmail. com <nicolasg@gmail .com> wrote:
      [color=blue]
      > does anyone know a module or something to convert numbers like integer
      > to binary format ?[/color]

      They _are_ in binary format.
      [color=blue]
      > for example I want to convert number 7 to 0111 so I can make some
      > bitwise operations...[/color]

      Just do it:
      [color=blue][color=green][color=darkred]
      >>> 7 & 3[/color][/color][/color]
      3[color=blue][color=green][color=darkred]
      >>> 7 | 8[/color][/color][/color]
      15


      --
      Grant Edwards grante Yow! QUIET!! I'm being
      at CREATIVE!! Is it GREAT
      visi.com yet? It's s'posed to SMOKEY
      THE BEAR...

      Comment

      • Alexis Roda

        #4
        Re: integer to binary...

        En/na nicolasg@gmail. com ha escrit:[color=blue]
        > does anyone know a module or something to convert numbers like integer
        > to binary format ?[/color]




        [color=blue]
        > for example I want to convert number 7 to 0111 so I can make some
        > bitwise operations...[/color]

        python already provides some bitwise operators:





        HTH

        Comment

        • nicolasg@gmail.com

          #5
          Re: integer to binary...


          Grant Edwards wrote:[color=blue]
          > On 2006-06-01, nicolasg@gmail. com <nicolasg@gmail .com> wrote:
          >[color=green]
          > > does anyone know a module or something to convert numbers like integer
          > > to binary format ?[/color]
          >
          > They _are_ in binary format.
          >[color=green]
          > > for example I want to convert number 7 to 0111 so I can make some
          > > bitwise operations...[/color]
          >
          > Just do it:
          >[color=green][color=darkred]
          > >>> 7 & 3[/color][/color]
          > 3[color=green][color=darkred]
          > >>> 7 | 8[/color][/color]
          > 15
          >
          >
          > --[/color]
          I know I can do that but I need to operate in every bit separeted.[color=blue]
          > Grant Edwards grante Yow! QUIET!! I'm being
          > at CREATIVE!! Is it GREAT
          > visi.com yet? It's s'posed to SMOKEY
          > THE BEAR...[/color]

          Comment

          • Anton Vredegoor

            #6
            Re: integer to binary...

            nicolasg@gmail. com wrote:
            [color=blue]
            > 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...[/color]
            [color=blue][color=green][color=darkred]
            >>> def bits(i,n):[/color][/color][/color]
            return tuple((0,1)[i>>j & 1] for j in xrange(n-1,-1,-1))
            [color=blue][color=green][color=darkred]
            >>> bits(7,4)[/color][/color][/color]
            (0, 1, 1, 1)

            Anton

            Comment

            • nicolasg@gmail.com

              #7
              Re: integer to binary...


              nicolasg@gmail. com wrote:[color=blue]
              > Grant Edwards wrote:[color=green]
              > > On 2006-06-01, nicolasg@gmail. com <nicolasg@gmail .com> wrote:
              > >[color=darkred]
              > > > does anyone know a module or something to convert numbers like integer
              > > > to binary format ?[/color]
              > >
              > > They _are_ in binary format.
              > >[color=darkred]
              > > > for example I want to convert number 7 to 0111 so I can make some
              > > > bitwise operations...[/color]
              > >
              > > Just do it:
              > >[color=darkred]
              > > >>> 7 & 3[/color]
              > > 3[color=darkred]
              > > >>> 7 | 8[/color]
              > > 15
              > >
              > >[/color][/color]
              this is exactly what I need ->
              http://www.daniweb.com/code/snippet285.html

              thanks.[color=blue][color=green]
              > > --[/color]
              > I know I can do that but I need to operate in every bit separeted.[color=green]
              > > Grant Edwards grante Yow! QUIET!! I'm being
              > > at CREATIVE!! Is it GREAT
              > > visi.com yet? It's s'posed to SMOKEY
              > > THE BEAR...[/color][/color]

              Comment

              • Grant Edwards

                #8
                Re: integer to binary...

                On 2006-06-01, nicolasg@gmail. com <nicolasg@gmail .com> wrote:
                [color=blue][color=green][color=darkred]
                >>> does anyone know a module or something to convert numbers like
                >>> integer to binary format ?[/color]
                >>
                >> They _are_ in binary format.
                >>[color=darkred]
                >> > for example I want to convert number 7 to 0111 so I can make some
                >> > bitwise operations...[/color]
                >>
                >> Just do it:
                >>[color=darkred]
                >> >>> 7 & 3[/color]
                >> 3[color=darkred]
                >> >>> 7 | 8[/color]
                >> 15[/color]
                >
                > I know I can do that but I need to operate in every bit separeted.[/color]

                Sorry, I've no clue what that means.

                --
                Grant Edwards grante Yow! Now KEN is having
                at a MENTAL CRISIS beacuse
                visi.com his "R.V." PAYMENTS are
                OVER-DUE!!

                Comment

                • Tim Chase

                  #9
                  Re: integer to binary...

                  >>> for example I want to convert number 7 to 0111 so I can make some[color=blue][color=green][color=darkred]
                  >>> bitwise operations...[/color]
                  >> Just do it:
                  >>[color=darkred]
                  >>>>> 7 & 3[/color]
                  >> 3[color=darkred]
                  >>>>> 7 | 8[/color]
                  >> 15[/color]
                  > I know I can do that but I need to operate in every bit separeted.[/color]


                  I suppose there might be other operations for which having them
                  as strings could be handy. E.g. counting bits:

                  bitCount = len([c for c in "0100101010 1" if c=="1"])

                  or parity checking with those counted bits...sure, it can be done
                  with the raw stuff, but the operations often tend to be more obscure.

                  Other reasons for wanting an arbitrary integer in binary might be
                  for plain-old-display, especially if it represents bitmap data.

                  If you just want to operate on each bit, you can iterate over the
                  number of bits and shift a single bit to its position:
                  [color=blue][color=green][color=darkred]
                  >>> target = 10
                  >>> shift = 0
                  >>> while 1 << shift <= target:[/color][/color][/color]
                  .... print "Bit %i is %i" % (shift,
                  .... (target & (1 << shift)) >> shift)
                  .... shift += 1
                  ....
                  Bit 0 is 0
                  Bit 1 is 1
                  Bit 2 is 0
                  Bit 3 is 1


                  It's ugly, but it works...

                  -tkc



                  Comment

                  • Grant Edwards

                    #10
                    Re: integer to binary...

                    On 2006-06-01, nicolasg@gmail. com <nicolasg@gmail .com> wrote:
                    [color=blue][color=green][color=darkred]
                    >>>> does anyone know a module or something to convert numbers like integer
                    >>>> to binary format ?
                    >>>
                    >>> They _are_ in binary format.
                    >>>
                    >>>> for example I want to convert number 7 to 0111 so I can make
                    >>>> some bitwise operations...
                    >>>
                    >>> Just do it:
                    >>>
                    >>> >>> 7 & 3
                    >>> 3
                    >>> >>> 7 | 8
                    >>> 15[/color]
                    >>[/color]
                    > this is exactly what I need -> http://www.daniweb.com/code/snippet285.html[/color]

                    That's nice, but I don't register at web sites like that.
                    [color=blue][color=green]
                    >> I know I can do that but I need to operate in every bit
                    >> separeted.[/color][/color]

                    I still don't get what you want a binary string for.

                    I can see wanting a sequence (e.g. array) of boolean values,
                    but how are you going to do bitwise operations on a binary
                    string?

                    --
                    Grant Edwards grante Yow! .. I think I'd
                    at better go back to my DESK
                    visi.com and toy with a few common
                    MISAPPREHENSION S...

                    Comment

                    • Grant Edwards

                      #11
                      Re: integer to binary...

                      On 2006-06-01, Tim Chase <python.list@ti m.thechases.com > wrote:[color=blue][color=green][color=darkred]
                      >>>> for example I want to convert number 7 to 0111 so I can make some
                      >>>> bitwise operations...
                      >>> Just do it:
                      >>>
                      >>>>>> 7 & 3
                      >>> 3
                      >>>>>> 7 | 8
                      >>> 15[/color]
                      >> I know I can do that but I need to operate in every bit separeted.[/color]
                      >
                      >
                      > I suppose there might be other operations for which having them
                      > as strings could be handy. E.g. counting bits:
                      >
                      > bitCount = len([c for c in "0100101010 1" if c=="1"])
                      >
                      > or parity checking with those counted bits...sure, it can be done
                      > with the raw stuff, but the operations often tend to be more obscure.[/color]

                      I would think an array or list of bits would be a lot more
                      useful for doing "bitwise operations":

                      bitCount = sum([0,1,0,0,1,0,1,0 ,1,0,1])
                      parity = reduce(operator .xor,[0,1,0,0,1,0,1,0 ,1,0,1])
                      [color=blue]
                      > Other reasons for wanting an arbitrary integer in binary might be
                      > for plain-old-display, especially if it represents bitmap data.[/color]

                      Yes. I thought C should have had a %b format since the
                      beginning, but nobody listens. But that's not
                      what the OP said he wanted it for.

                      --
                      Grant Edwards grante Yow! Now I'm concentrating
                      at on a specific tank battle
                      visi.com toward the end of World
                      War II!

                      Comment

                      • mensanator@aol.com

                        #12
                        Re: integer to binary...


                        nicolasg@gmail. com wrote:[color=blue]
                        > 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...
                        >
                        > Thanks[/color]

                        Use the gmpy module.
                        [color=blue][color=green][color=darkred]
                        >>> import gmpy
                        >>> a = 14
                        >>> b = 7
                        >>> c = 8[/color][/color][/color]
                        [color=blue][color=green][color=darkred]
                        >>> help(gmpy.digit s)[/color][/color][/color]
                        Help on built-in function digits:

                        digits(...)
                        digits(x[,base]): returns Python string representing x in the
                        given base (2 to 36, default 10 if omitted or 0); leading '-'
                        present if x<0, but no leading '+' if x>=0. x must be an mpz,
                        or else gets coerced into one.
                        [color=blue][color=green][color=darkred]
                        >>> print gmpy.digits(a,2 )[/color][/color][/color]
                        1110[color=blue][color=green][color=darkred]
                        >>> print gmpy.digits(b,2 )[/color][/color][/color]
                        111[color=blue][color=green][color=darkred]
                        >>> print gmpy.digits(c,2 )[/color][/color][/color]
                        1000

                        [color=blue][color=green][color=darkred]
                        >>> help(gmpy.setbi t)[/color][/color][/color]
                        Help on built-in function setbit:

                        setbit(...)
                        setbit(x,n,v=1) : returns a copy of the value of x, with bit n set
                        to value v; n must be an ordinary Python int, >=0; v, 0 or !=0;
                        x must be an mpz, or else gets coerced to one.
                        [color=blue][color=green][color=darkred]
                        >>> d = gmpy.setbit(c,1 ,1)
                        >>> print gmpy.digits(d,2 )[/color][/color][/color]
                        1010


                        [color=blue][color=green][color=darkred]
                        >>> help(gmpy.scan1 )[/color][/color][/color]
                        Help on built-in function scan1:

                        scan1(...)
                        scan1(x, n=0): returns the bit-index of the first 1-bit of x (that
                        is at least n); n must be an ordinary Python int, >=0. If no more
                        1-bits are in x at or above bit-index n (which can only happen for
                        x>=0, notionally extended with infinite 0-bits), None is returned.
                        x must be an mpz, or else gets coerced to one.
                        [color=blue][color=green][color=darkred]
                        >>> help(gmpy.scan0 )[/color][/color][/color]
                        Help on built-in function scan0:

                        scan0(...)
                        scan0(x, n=0): returns the bit-index of the first 0-bit of x (that
                        is at least n); n must be an ordinary Python int, >=0. If no more
                        0-bits are in x at or above bit-index n (which can only happen for
                        x<0, notionally extended with infinite 1-bits), None is returned.
                        x must be an mpz, or else gets coerced to one.
                        [color=blue][color=green][color=darkred]
                        >>> print gmpy.scan1(a)[/color][/color][/color]
                        1[color=blue][color=green][color=darkred]
                        >>> print gmpy.scan1(b)[/color][/color][/color]
                        0[color=blue][color=green][color=darkred]
                        >>> print gmpy.scan1(c)[/color][/color][/color]
                        3[color=blue][color=green][color=darkred]
                        >>> print gmpy.scan1(d)[/color][/color][/color]
                        1[color=blue][color=green][color=darkred]
                        >>> print gmpy.scan0(a)[/color][/color][/color]
                        0[color=blue][color=green][color=darkred]
                        >>> print gmpy.scan0(b)[/color][/color][/color]
                        3[color=blue][color=green][color=darkred]
                        >>> print gmpy.scan0(c)[/color][/color][/color]
                        0[color=blue][color=green][color=darkred]
                        >>> print gmpy.scan0(d)[/color][/color][/color]
                        0
                        [color=blue][color=green][color=darkred]
                        >>> help(gmpy.popco unt)[/color][/color][/color]
                        Help on built-in function popcount:

                        popcount(...)
                        popcount(x): returns the number of 1-bits set in x; note that
                        this is 'infinite' if x<0, and in that case, -1 is returned.
                        x must be an mpz, or else gets coerced to one.
                        [color=blue][color=green][color=darkred]
                        >>> print gmpy.popcount(a )[/color][/color][/color]
                        3[color=blue][color=green][color=darkred]
                        >>> print gmpy.popcount(b )[/color][/color][/color]
                        3[color=blue][color=green][color=darkred]
                        >>> print gmpy.popcount(c )[/color][/color][/color]
                        1[color=blue][color=green][color=darkred]
                        >>> print gmpy.popcount(d )[/color][/color][/color]
                        2

                        [color=blue][color=green][color=darkred]
                        >>> help(gmpy.hamdi st)[/color][/color][/color]
                        Help on built-in function hamdist:

                        hamdist(...)
                        hamdist(x,y): returns the Hamming distance (number of bit-positions
                        where the bits differ) between x and y. x and y must be mpz, or
                        else
                        get coerced to mpz.
                        [color=blue][color=green][color=darkred]
                        >>> print gmpy.hamdist(a, b)[/color][/color][/color]
                        2[color=blue][color=green][color=darkred]
                        >>> print gmpy.hamdist(a, c)[/color][/color][/color]
                        2[color=blue][color=green][color=darkred]
                        >>> print gmpy.hamdist(a, d)[/color][/color][/color]
                        1[color=blue][color=green][color=darkred]
                        >>> print gmpy.hamdist(b, c)[/color][/color][/color]
                        4[color=blue][color=green][color=darkred]
                        >>> print gmpy.hamdist(b, d)[/color][/color][/color]
                        3[color=blue][color=green][color=darkred]
                        >>> print gmpy.hamdist(c, d)[/color][/color][/color]
                        1

                        Comment

                        • Claudio Grondi

                          #13
                          Re: integer to binary...

                          mensanator@aol. com wrote:[color=blue]
                          > nicolasg@gmail. com wrote:
                          >[color=green]
                          >>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...
                          >>
                          >>Thanks[/color]
                          >
                          >
                          > Use the gmpy module.
                          >
                          >[color=green][color=darkred]
                          >>>>import gmpy
                          >>>>a = 14
                          >>>>b = 7
                          >>>>c = 8[/color][/color]
                          >
                          >[color=green][color=darkred]
                          >>>>help(gmpy.d igits)[/color][/color]
                          >
                          > Help on built-in function digits:
                          >
                          > digits(...)
                          > digits(x[,base]): returns Python string representing x in the
                          > given base (2 to 36, default 10 if omitted or 0); leading '-'
                          > present if x<0, but no leading '+' if x>=0. x must be an mpz,
                          > or else gets coerced into one.
                          >
                          >[color=green][color=darkred]
                          >>>>print gmpy.digits(a,2 )[/color][/color]
                          >
                          > 1110
                          >[color=green][color=darkred]
                          >>>>print gmpy.digits(b,2 )[/color][/color]
                          >
                          > 111
                          >[color=green][color=darkred]
                          >>>>print gmpy.digits(c,2 )[/color][/color]
                          >
                          > 1000
                          >
                          >
                          >[color=green][color=darkred]
                          >>>>help(gmpy.s etbit)[/color][/color]
                          >
                          > Help on built-in function setbit:
                          >
                          > setbit(...)
                          > setbit(x,n,v=1) : returns a copy of the value of x, with bit n set
                          > to value v; n must be an ordinary Python int, >=0; v, 0 or !=0;
                          > x must be an mpz, or else gets coerced to one.
                          >
                          >[color=green][color=darkred]
                          >>>>d = gmpy.setbit(c,1 ,1)
                          >>>>print gmpy.digits(d,2 )[/color][/color]
                          >
                          > 1010
                          >
                          >
                          >
                          >[color=green][color=darkred]
                          >>>>help(gmpy.s can1)[/color][/color]
                          >
                          > Help on built-in function scan1:
                          >
                          > scan1(...)
                          > scan1(x, n=0): returns the bit-index of the first 1-bit of x (that
                          > is at least n); n must be an ordinary Python int, >=0. If no more
                          > 1-bits are in x at or above bit-index n (which can only happen for
                          > x>=0, notionally extended with infinite 0-bits), None is returned.
                          > x must be an mpz, or else gets coerced to one.
                          >
                          >[color=green][color=darkred]
                          >>>>help(gmpy.s can0)[/color][/color]
                          >
                          > Help on built-in function scan0:
                          >
                          > scan0(...)
                          > scan0(x, n=0): returns the bit-index of the first 0-bit of x (that
                          > is at least n); n must be an ordinary Python int, >=0. If no more
                          > 0-bits are in x at or above bit-index n (which can only happen for
                          > x<0, notionally extended with infinite 1-bits), None is returned.
                          > x must be an mpz, or else gets coerced to one.
                          >
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan1(a)[/color][/color]
                          >
                          > 1
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan1(b)[/color][/color]
                          >
                          > 0
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan1(c)[/color][/color]
                          >
                          > 3
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan1(d)[/color][/color]
                          >
                          > 1
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan0(a)[/color][/color]
                          >
                          > 0
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan0(b)[/color][/color]
                          >
                          > 3
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan0(c)[/color][/color]
                          >
                          > 0
                          >[color=green][color=darkred]
                          >>>>print gmpy.scan0(d)[/color][/color]
                          >
                          > 0
                          >
                          >[color=green][color=darkred]
                          >>>>help(gmpy.p opcount)[/color][/color]
                          >
                          > Help on built-in function popcount:
                          >
                          > popcount(...)
                          > popcount(x): returns the number of 1-bits set in x; note that
                          > this is 'infinite' if x<0, and in that case, -1 is returned.
                          > x must be an mpz, or else gets coerced to one.
                          >
                          >[color=green][color=darkred]
                          >>>>print gmpy.popcount(a )[/color][/color]
                          >
                          > 3
                          >[color=green][color=darkred]
                          >>>>print gmpy.popcount(b )[/color][/color]
                          >
                          > 3
                          >[color=green][color=darkred]
                          >>>>print gmpy.popcount(c )[/color][/color]
                          >
                          > 1
                          >[color=green][color=darkred]
                          >>>>print gmpy.popcount(d )[/color][/color]
                          >
                          > 2
                          >
                          >
                          >[color=green][color=darkred]
                          >>>>help(gmpy.h amdist)[/color][/color]
                          >
                          > Help on built-in function hamdist:
                          >
                          > hamdist(...)
                          > hamdist(x,y): returns the Hamming distance (number of bit-positions
                          > where the bits differ) between x and y. x and y must be mpz, or
                          > else
                          > get coerced to mpz.
                          >
                          >[color=green][color=darkred]
                          >>>>print gmpy.hamdist(a, b)[/color][/color]
                          >
                          > 2
                          >[color=green][color=darkred]
                          >>>>print gmpy.hamdist(a, c)[/color][/color]
                          >
                          > 2
                          >[color=green][color=darkred]
                          >>>>print gmpy.hamdist(a, d)[/color][/color]
                          >
                          > 1
                          >[color=green][color=darkred]
                          >>>>print gmpy.hamdist(b, c)[/color][/color]
                          >
                          > 4
                          >[color=green][color=darkred]
                          >>>>print gmpy.hamdist(b, d)[/color][/color]
                          >
                          > 3
                          >[color=green][color=darkred]
                          >>>>print gmpy.hamdist(c, d)[/color][/color]
                          >
                          > 1
                          >[/color]
                          For those digging deeper into this subject who are looking for speed,
                          reading the past discussion on this newsgroup I was part of myself
                          looking for fastest way of such integer to binary conversion can maybe
                          be of interest:

                          (includes full source code of all compared approaches)

                          Claudio

                          Comment

                          • John Salerno

                            #14
                            Re: integer to binary...

                            mensanator@aol. com wrote:
                            [color=blue]
                            > Use the gmpy module.[/color]

                            Yes, it's good. :)

                            Comment

                            • Sion Arrowsmith

                              #15
                              Re: integer to binary...

                              Tim Chase <python.list@ti m.thechases.com > wrote:[color=blue]
                              >bitCount = len([c for c in "0100101010 1" if c=="1"])[/color]

                              bitCount = "01001010101".c ount("1")

                              --
                              \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
                              ___ | "Frankly I have no feelings towards penguins one way or the other"
                              \X/ | -- Arthur C. Clarke
                              her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

                              Comment

                              Working...