0

I found this when I was trying to rightshift a number greater than pow(2, 32). And I noticed that the document has noted that JS will do a 32bit convert before bitwise calculations. And so I wonder why JS do such a convertion? What's its intention behind the logic?

Lucas Lin
  • 1
  • 1
  • The document I referred to [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND) – Lucas Lin Jun 29 '21 at 07:38
  • The link provided by @phuzi is about how it works, but i wonder why it works so, why not support bitwise calculations greater than 2^32. Thanks all the same ! – Lucas Lin Jun 29 '21 at 07:48
  • 1
    Quote: _"Bitwise operators treat their operands as a sequence of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. If you didn't treat the numbers like this the bitwise ops wouldn't make much sense, which is why it is done"_. – E_net4 Jun 29 '21 at 07:49
  • 1
    All numbers in JavaScript are floating point numbers. Although you can have whole numbers and Math,MAX_SAFE_INTEGER is greater than 2^32 it is also less than 2^64 so it seems sensible to limit bitwise operarations to 32 bits. – phuzi Jun 29 '21 at 07:54
  • "*Why the conversion?*" - because doing bitwise logic on raw floating point values is pretty useless. "*Why only 32bit?*" - because floating point number cannot precisely represent all values the next-larger domain, 64bit integers, anyway, and because it would be a waste on 32bit cpu architectures. – Bergi Jun 29 '21 at 09:10
  • 1
    Btw, you can do bitwise operations on arbitrarily large integers with `BigInt`. – Bergi Jun 29 '21 at 09:11
  • @phuzi Thanks for you hint about the "floating point numbers." it explained all. – Lucas Lin Jun 29 '21 at 09:49
  • @Bergi Thanks you for the detailed explaination. Very clear to understand. – Lucas Lin Jun 29 '21 at 09:51

0 Answers0