0

ActionScript and javascript supports '>>>' operator. I am converting an actionscript of base64 encoding/decoding using python. I know base64 is available and this is for learning purpose. python doesn't have "..." operator. base64 action script

in decode function you have two lines

  1. o1 = bits >>> 16 & 0xff;
  2. o2 = bits >>> 8 & 0xff;

How do I convert these lines into python?

4
  • 2
    What does the operator do? Arithmetic right-shift? (Things like >>> are hard to Google, so please name them.) Commented Apr 18, 2011 at 12:08
  • btw. if you need AES in Python, just use PyCrypto package. pycrypto.org Commented Apr 18, 2011 at 12:14
  • It's an arithmetic shift right (ASR), as opposed to a logical shift right (LSR). I still wish one of these languages would implement the rotate instructions (ROL and ROR), but those depend on an exact number of bits, and some processors let you rotate through the carry bit, so it gets messy. Oh, BTW, bits >>> 16 & 0xff and bits >> 16 & 0xff are the same thing as long as bits is at least 24 bits long, due to the masking performed by the & operator. Same for the other equation. In English, these equations just extract single bytes from a larger integer field. Commented Apr 18, 2011 at 12:20
  • 2
    I don't think this is a good way to learn Python. At best you'll learn the syntax. But you will miss the opportunity to learn the right concepts in you new language. Worst case you will learn things that are wrong in Python. Also your script is horribly low level, something like this should never be implemented in Python. You should learn to use the right libraries and modules instead. Commented Apr 18, 2011 at 12:25

2 Answers 2

3

IINM, >>> is a right shift that preserves the value of the leftmost bit. This has the effect of keeping the sign of the number intact (i.e. negative numbers get a 1 inserted as their MSB instead of a 0).

In Python, integers can be arbitrarily big. There is no meaningful concept of a most significant bit since the number of bits is not bounded. Right shifts therefore simply shift all bits to the right and preserve the sign. So, in Python you can just use >>.

Edit:

As @Sven Marnach has pointed out, the >>> operation does the exact opposite of what I thought it did in Javascript. Thus >>> does not preserve sign, while >> appears to do so. Another interesting fact is that apparently -1>>1 is -1 and not 0 in Javascript. Needless to say, my knowledge of Javascript is really limited and I am not sure what the exact equivalent of these operators are in Python.

One options might be to explicitly limit how many bits are being used (perhaps by using ctypes.c_int) and then set the left most bit manually as desired in a user defined right shift function.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not true. >>> does not preserve sign: Try javascript:alert(-3>>>1);
0
import ctypes
#convert signed to unsigned
bits = ctypes.c_uint32(bits).value

o1 = bits >> 16 & 0xff
o2 = bits >> 8 & 0xff

1 Comment

The operator >> does something different than >>> in JavaScript, and the difference matters in this context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.