0

The following expression results different result in Javascript in Python and Javascript:-

Python:-

a, b, c, e, f, h = 271733878, 4023233417, 5858469028, -389564586, 2562383102, 1634886000

a = a + (c & b | ~c & f) + h + e  # 4965557782

Javascript:-

a = 271733878;
b = 4023233417;
c = 5858469028;
e = -389564586
f = 2562383102;
h = 1634886000;

a = a + (c & b | ~c & f) + h + e; // 670590486

console.log(a)

How is the evaluation of the same expression differ in 2 languages?

4
  • 3
    because they are written differently? Commented Mar 4, 2019 at 10:07
  • Even evaluating the mathematical expressions? that sounds weird!! Should be something more. Commented Mar 4, 2019 at 10:11
  • Bitwise calculations always depend on the bit representation of the value, and are thereby very specific to the language/platform used. There isn't really any one "right" answer. Commented Mar 4, 2019 at 10:14
  • Found the solution. Converting the python number to 32-bit works. Commented Mar 4, 2019 at 10:17

1 Answer 1

2

~c is evaluated differently in Python and Javascript.

~c is the negation of the c value. This is done by inverting the bits of the value. As such, the value differs in different languages because they probably use a different amount of bits to store int values, which will result in a different ~value.

Javascript:

~c = -1563501733

Python:

~c = -5858469029
Sign up to request clarification or add additional context in comments.

3 Comments

Any solution to this, for me to make it behave similarly?
Converting the python number to 32-bit works.
Yep, that was what I was about to suggest! Glad you found your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.