最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

difference between JavaScript bit-wise operator code and Python bit-wise operator code - Stack Overflow

programmeradmin2浏览0评论

I have converted JavaScript code which uses bit-wise operators in that code to Python code, but there is one problem when i do this in JavaScript and Python

412287 << 10

then I get this 422181888 same results in both languages. but when i do this in both

424970184 << 10

then i get different results in both of the languages 1377771520 in JavaScript and 435169468416 in Python

can anybody help me with this? any help would be appreciated.

I have converted JavaScript code which uses bit-wise operators in that code to Python code, but there is one problem when i do this in JavaScript and Python

412287 << 10

then I get this 422181888 same results in both languages. but when i do this in both

424970184 << 10

then i get different results in both of the languages 1377771520 in JavaScript and 435169468416 in Python

can anybody help me with this? any help would be appreciated.

Share Improve this question asked Jan 12, 2017 at 10:03 VASIM SETAVASIM SETA 1651 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

If you want the JavaScript equivalent value then what you can do is :

import ctypes

print(ctypes.c_int(424970184 << 10 ^ 0).value)

Output:

1377771520

As stated in this SO answer, in javascript the bitwise operators and shift operators operate on 32-bit ints, and your second example overflows the 32 bit capacity, so the python equivalent would be:

(424970184 << 10) & 0x7FFFFFFF

(you get a "modulo"/"masked" value with the signed 32 bit integer mask, not the actual value)

In Python there's no limit in capacity for integers, so you get the actual value.

发布评论

评论列表(0)

  1. 暂无评论