0

if Bitwise OR was used ieee-754 to calc the result in Javascript,i can't understand the result.

for example:

2|1 =>3

In ieee-754, 2 is stored as 0 10000000000 0000...0000,and 1 is stored as 0 01111111111 0000...0000

if exec bitwise or,i think the result is 0 11111111111 0000...0000,but why does it output 3?

As same as above,

example:

0.1|0 =>0

0 is stored as 0 00000000000 0000...0000,and 0.1 is stored as 0 01111111011 1001100110011001100110011001100110011001100110011010

if exec bitwise or,i think the result is 0 01111111011 1001100110011001100110011001100110011001100110011010,but why does it output 0,and lose decimal?

example:

2|-1 =>-1

2 is stored as 0 10000000000 0000...0000,and -1 is stored as 1 01111111111 0000...0000

if exec bitwise or,i think the result is 1 11111111111 0000...0000,but why does it output -1?

HanQ
  • 95
  • 1
  • 8
  • 2
    in javascript the number is converted to a 32 bit integer number, then the bitwise operation is made and the result is converted back to the standard format of 64 bit float.. – Nina Scholz Apr 14 '19 at 08:03
  • Have some spec or document indicate that ? @NinaScholz – HanQ Apr 14 '19 at 08:07
  • here you go: [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) – Nina Scholz Apr 14 '19 at 08:09
  • [How do I convert a float number to a whole number in JavaScript?](//stackoverflow.com/a/12837315) Also [Read/Write bytes of float in JS](//stackoverflow.com/a/35748581) for actually type-punning so you could bitwise OR the bit-patterns of floating-point numbers. Not exactly a duplicate, though. – Peter Cordes Apr 14 '19 at 08:09
  • Possible duplicate of [~ bitwise operator in JavaScript](https://stackoverflow.com/questions/32527807/bitwise-operator-in-javascript) (The answers there state that bitwise operators convert from `Number` (double) to 32 bit integer) – Peter Cordes Apr 14 '19 at 08:15
  • but why 0.1|0 is output 0? As the document,number will convert to 32bit,but after bitwise or,the result should be `0 01111011 10011001100110011001101` – HanQ Apr 14 '19 at 08:23
  • 1
    you miss the integer part. – Nina Scholz Apr 14 '19 at 08:25
  • 1.1|0 is output 1,also lose the decimal,i wonder that why the decimal was lost@NinaScholz.if javascript lose decimal in 32-bit,not use ieee-754 binary32 – HanQ Apr 14 '19 at 08:30
  • @HanQ again, bitwise operations first convert the number to a 32 bit ***integer*** so `0.1` would be converted into `0b0` while `1.1` would be converted to `0b1` before the operation is even calculated. – VLAZ Apr 14 '19 at 08:32
  • what is mean `0b0`?what's the `b`?@VLAZ – HanQ Apr 14 '19 at 08:41
  • `0b` - prefix meaning "binary number" `0b0` - zero in binary, `0b10` is the binary number for the decimal `2`. – VLAZ Apr 14 '19 at 08:50

1 Answers1

1

JavaScript is an implementation of ECMAScript. The ECMAScript 2018 Language Specification (9th edition, June 2018) specifies the binary bitwise operators in clause 12.12. The evaluation steps in 12.12.3 include (using “@” to stand for one of the bitwise operations):

  1. Let lnum be ? ToInt32(lval).
  2. Let rnum be ? ToInt32(rval).
  3. Return the result of applying the bitwise operator @ to lnum and rnum. The result is a signed 32-bit integer.

Thus, the bitwise operation is not performed on the bytes that represent a Number. The value of the Number is converted to a 32-bit two’s complement representation of an integer, and the operation is performed on those bits. Then resulting bits are then interpreted as a 32-bit two’s complement representation, so the value they represent becomes the value produced by the operation.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312