integer to binary...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno Desthuilliers

    #16
    Re: integer to binary...

    nicolasg@gmail. com a écrit :[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]

    You don't need to convert anything. The bitwise ops are &, |, <<, >>

    0 | 2 | 4
    -> 6
    6 & 2
    -> 2
    2 << 4
    -> 32
    8 >> 1
    -> 4


    Comment

    • Bruno Desthuilliers

      #17
      Re: integer to binary...

      Grant Edwards a écrit :[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]

      Not really.
      [color=blue][color=green][color=darkred]
      >>> (7).__class__[/color][/color][/color]
      <type 'int'>[color=blue][color=green][color=darkred]
      >>> dir((7))[/color][/color][/color]
      ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
      '__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
      '__floordiv__', '__getattribute __', '__getnewargs__ ', '__hash__',
      '__hex__', '__init__', '__int__', '__invert__', '__long__',
      '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
      '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
      '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__' , '__repr__',
      '__rfloordiv__' , '__rlshift__', '__rmod__', '__rmul__', '__ror__',
      '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
      '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__'][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Comment

      • Bruno Desthuilliers

        #18
        Re: integer to binary...

        nicolasg@gmail. com a écrit :[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]
        >
        > I know I can do that but I need to operate in every bit separeted.[/color]

        Could you explain the difference ?

        Comment

        • Grant Edwards

          #19
          Re: integer to binary...

          On 2006-06-02, Bruno Desthuilliers <bdesth.quelque chose@free.quel quepart.fr> wrote:[color=blue]
          > Grant Edwards a écrit :[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]
          >
          > Not really.[/color]

          Yes, really. Otherwise the bitwise boolean operations you
          demonstrated wouldn't work as shown.
          [color=blue][color=green][color=darkred]
          >>>> (7).__class__[/color][/color]
          ><type 'int'>[color=green][color=darkred]
          >>>> dir((7))[/color][/color]
          > ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
          > '__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
          > '__floordiv__', '__getattribute __', '__getnewargs__ ', '__hash__',
          > '__hex__', '__init__', '__int__', '__invert__', '__long__',
          > '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
          > '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
          > '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__' , '__repr__',
          > '__rfloordiv__' , '__rlshift__', '__rmod__', '__rmul__', '__ror__',
          > '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
          > '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__'][color=green][color=darkred]
          >>>>[/color][/color][/color]

          The fact that they impliment the xor operator is pretty much
          proof that integers are stored in binary format -- xor is only
          defined for binary numbers.

          --
          Grant Edwards grante Yow! ... Blame it on the
          at BOSSA NOVA!!!
          visi.com

          Comment

          • Tim Chase

            #20
            Re: integer to binary...

            > The fact that they impliment the xor operator is pretty much[color=blue]
            > proof that integers are stored in binary format -- xor is only
            > defined for binary numbers.[/color]

            Um...let's not use bad logic/proofs for evidencing this...
            [color=blue][color=green][color=darkred]
            >>> hasattr(set(), "__xor__")[/color][/color][/color]
            True

            :)

            -tkc



            Comment

            • Grant Edwards

              #21
              Re: integer to binary...

              On 2006-06-03, Tim Chase <python.list@ti m.thechases.com > wrote:[color=blue][color=green]
              >> The fact that they impliment the xor operator is pretty much
              >> proof that integers are stored in binary format -- xor is only
              >> defined for binary numbers.[/color]
              >
              > Um...let's not use bad logic/proofs for evidencing this...
              >[color=green][color=darkred]
              > >>> hasattr(set(), "__xor__")[/color][/color]
              > True[/color]

              Sets aren't numbers. Perhaps I should have phrased it better:
              xor is only defined for numbers if they are represented in
              binary. If numbers were represented in something other than
              binary, then an xor operation on those numbers wouldn't make
              sense.

              --
              Grant Edwards grante Yow! .. I want to perform
              at cranial activities with
              visi.com Tuesday Weld!!

              Comment

              • Bruno Desthuilliers

                #22
                Re: integer to binary...

                Grant Edwards a écrit :[color=blue]
                > On 2006-06-02, Bruno Desthuilliers <bdesth.quelque chose@free.quel quepart.fr> wrote:
                >[color=green]
                >>Grant Edwards a écrit :
                >>[color=darkred]
                >>>On 2006-06-01, nicolasg@gmail. com <nicolasg@gmail .com> wrote:
                >>>
                >>>>does anyone know a module or something to convert numbers like integer
                >>>>to binary format ?
                >>>
                >>>They _are_ in binary format.[/color]
                >>
                >>Not really.[/color]
                >
                > Yes, really.[/color]

                No, not really.
                [color=blue]
                > Otherwise the bitwise boolean operations you
                > demonstrated wouldn't work as shown.[/color]

                Ho yes ?
                [color=blue]
                >[color=green][color=darkred]
                >>>>>(7).__clas s__[/color]
                >>
                >><type 'int'>
                >>[color=darkred]
                >>>>>dir((7))[/color]
                >>
                >>['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
                >>'__delattr__' , '__div__', '__divmod__', '__doc__', '__float__',
                >>'__floordiv__ ', '__getattribute __', '__getnewargs__ ', '__hash__',
                >>'__hex__', '__init__', '__int__', '__invert__', '__long__',
                >>'__lshift__ ', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
                >>'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
                >>'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__' , '__repr__',
                >>'__rfloordiv_ _', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
                >>'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
                >>'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']
                >>[/color]
                >
                > The fact that they impliment the xor operator is pretty much
                > proof that integers are[/color]

                .... objects, instance of the int class. Not really what I'd call "binary
                format" !-)

                Now if you go that way, it's of course true that everything on a
                computer ends up in a binary format.... It's true.
                [color=blue]
                > stored in binary format -- xor is only
                > defined for binary numbers.
                >[/color]
                class Prisonner(objec t):
                def __xor__(self, other):
                return "I'm not a (binary) number, I'm a free man"

                The fact that an object implements the xor operator is pretty much proof
                that the guy that wrote the class decided to implement the xor operator !-)

                Grant, I of course agree that, *for practical means*, one can consider
                that Python's integer are "already in binary format" - for a definition
                of "binary format" being "you can do bitwise ops on them". But the truth
                is that Python integers are objects (in the OO meaning) holding integer
                values - not integer values themselves.

                Comment

                Working...