1

I'm 'hacking' my router, and I need to rewrite one JS function that takes date in hexdec format and convert it into Y m d

JS code looks like:

return [(((n >> 16) & 0xFF) + 1900), ((n >>> 8) & 0xFF), (n & 0xFF)];

where n is variable in format 0x123456 (e.g. 6 hexdec digits after 0x sign) found that python has operators like >> but does't have >>> operator. Any idea how to do that ? thanks

2 Answers 2

5

Use >>. You are removing the sign anyway with the & 0xFF. Note, that you cannot leave out the & part, i.e., (n >> 8) & 0xff, or you'll get the wrong result, as you have already noted.

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

Comments

3

First thing you should know is that the bitwise operator for JS is operating on 32bit data. While for python, it's assuming the data could have infinite number of bits.

So '>>' in JS which is called signed-propagating right shift, should equal to

def right_shift(data, bits):
    sign = (data >> 31) & 1 
    if sign:
       fills = ((sign << bits) - 1) << (32 - bits)
    else:
       fills = 0
    return ((data & 0xffffffff) >> bits) | fills

'>>>' in JS is called zero-fill right shift, in python since it always fill with zero, so you could just use '>>'. A more robust way to make sure it has same result with js

def zero_fill_right_shit(data, bits):
    return (data & 0xffffffff) >> bits

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.