Equivalent of Javascript Bitwise Operators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Foord

    #1

    Equivalent of Javascript Bitwise Operators

    Please pardon my ignorance on this one - but I'm not certain how the
    sign bt is treated in python bitwise operators. I've trying to convert
    a javascript DES encryption routine into python.

    Javascritp has >>> and >>. >>> is a zero fill bit shift whereas >> is
    a sign propagating bit shift. My understanding is that the python >>
    is equivalent to the javascript >> - but python has no equivalent to[color=blue][color=green][color=darkred]
    >>>.[/color][/color][/color]

    Would a >>> 3 in javascript be equivalent to abs(a) >> 3 in python
    (make sure the sign bit is set to zero first) ?

    In actual fact I'm now using DES3 from Pycrypto - but I'm still
    interested in the answer to the question. In case anyone is interested
    I've done some work with some Javascript encryption and python
    equivalents. Thsi allows you to build client side encryption into
    webpages - e.g. for secure logins. It all works - very nice.

    Regards,

    Fuzzy
    http://www.voidspace.org.uk /atlantibots/pythonutils.htm l
  • Bengt Richter

    #2
    Re: Equivalent of Javascript Bitwise Operators

    On 27 Oct 2004 04:04:36 -0700, fuzzyman@gmail. com (Michael Foord) wrote:
    [color=blue]
    >Please pardon my ignorance on this one - but I'm not certain how the
    >sign bt is treated in python bitwise operators. I've trying to convert
    >a javascript DES encryption routine into python.
    >
    >Javascritp has >>> and >>. >>> is a zero fill bit shift whereas >> is
    >a sign propagating bit shift. My understanding is that the python >>
    >is equivalent to the javascript >> - but python has no equivalent to[color=green][color=darkred]
    >>>>.[/color][/color]
    >
    >Would a >>> 3 in javascript be equivalent to abs(a) >> 3 in python
    >(make sure the sign bit is set to zero first) ?
    >[/color]
    No. Python has the sign bit virtually extended infinitely to the left, so
    you'll never see the zeroes ;-)

    IOW, python (no longer) make the assumption that bit 31 (or 63 on 64 bit platforms??!!)
    is a sign bit in its integers. Unification of int and long is behind that.

    If you want to shift in zeroes from above 32 bits of a possibly negative number,
    mask it down to 32 bits, don't use abs. I.e, use int((a&0xffffff ffL) >> 3)

    (A 32-bit positive number with zeroes in the sign bits has to be a long unless
    you have a 32-bit machine. The unification is trying to get away from that platform
    dependency from version 2.4 on (I have 2.3). Then hex literals will all be positive
    numbers. But I guess there will still be an int/long representation duality, so if
    you want to make the above int again, after the & with a long constant, you have to
    do the int(result). I'm not sure what type(a&0xffffff ff) >> 3) will be in 2.4, but
    I suspect long. I guess I should install the new beta.
    [color=blue]
    >In actual fact I'm now using DES3 from Pycrypto - but I'm still
    >interested in the answer to the question. In case anyone is interested
    >I've done some work with some Javascript encryption and python
    >equivalents. Thsi allows you to build client side encryption into
    >webpages - e.g. for secure logins. It all works - very nice.
    >[/color]

    Regards,
    Bengt Richter

    Comment

    • Bengt Richter

      #3
      Re: Equivalent of Javascript Bitwise Operators

      On Thu, 28 Oct 2004 00:00:32 GMT, bokr@oz.net (Bengt Richter) wrote:
      "32" while thinking "64"
      [...][color=blue]
      >
      >(A 32-bit positive number with zeroes in the sign bits has to be a long unless
      >you have a 32-bit machine. The unification is trying to get away from that platform[/color]
      ^^
      I meant 64. I should have said >32 I suppose ;-)

      Sorry if any confusion.

      Regards,
      Bengt Richter

      Comment

      Working...