0

Javascript

1<<31
-2147483648
1<<32
1

Python

1<<31
2147483648
1<<32
4294967296

Is this related to max int? But 4294967296 not Bigger then the max int in js.

  • Possible duplicate of [What is JavaScript's highest integer value that a Number can go to without losing precision?](https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin) – shole Jul 11 '17 at 03:07
  • But 4294967296 not bigger then that. – user7924038 Jul 11 '17 at 03:08
  • 1
    Quote from the post: "Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 2^31-1, or 2147483647." Did you even read it? – shole Jul 11 '17 at 03:14
  • thanks,I got it,but do you know why 1<<32 using 32bit but 2**32 using 64bit? – user7924038 Jul 11 '17 at 03:24
  • It is part of the design which I don't know why, but this is like asking why JS is not strong typing or why unary operator + will turn string into a number. If you really need to know why, you can open new thread to ask, it is totally different question – shole Jul 11 '17 at 03:30
  • The `**` operator doesn't work only on integers, e.g., `2.25**0.5` is `1.5`. – nnnnnn Jul 11 '17 at 03:32
  • @nnnnnn I'd add that ** isn't a bitwise operator either – Loïc Faure-Lacroix Jul 11 '17 at 03:54

1 Answers1

1

An integer in JavaScript is actually an IEEE 754 64bit float number. But an integer in Python may be a simple integer or a bignum.


All bit operations in JavaScript was defined on 32bit signed / unsigned integers. When you do these operations, the two operand was first converted to 32 bit integers, and the result will always be a 32 bit integer.

If you want multiple a number with 232, you should do 1 * 2 ** 32 (or 1 * Math.pow(2, 32) in ES5) instead of this one.


Python has builtin bignum support, which support all bit operations such as shift left. As a result, you may shift a number with any (reasonable) bits and it may be greater than 232.

tsh
  • 4,263
  • 5
  • 28
  • 47