Bitwise shift to the left, a problem

I rewrite some code from javascript to pcp, it should work identically.

If you have dealt with bitwise OR, javascript compares the numbers in the 32-bit representation. Now the problem with the bitwise shift to the left.

js 30 << 30 возвращает -2147483648
php 30 << 30 возвращает 32212254720

OK, the first 32 bits, 32212254720 % (2 ** 32), will get 2147483648, almost similar to the result of js.

Trying to recreate the js logic, the number 30 in binary representation

11110
00000000000000000000000000011110

I shift to the left by 30 bits, I get

00000000000000000000000000011110000000000000000000000000000000

I leave 32 bit

10000000000000000000000000000000

Converting to decimal

2147483648

But where does the minus sign come from in js?

PS

Thank you all for your help, I wrote 3 functions | << >>>, and then found a ready-made library https://github.com/simaguo/javascript-bitwise-operators

Author: Jean-Claude, 2019-01-04

3 answers

Bitwise operators in JavaScript work with 32-bit integers in their binary representation.

The number 30 in binary form is 0b00000000000000000000000000011110 (0b - this is just a prefix, indicating that the number given behind it is written in binary form. So, in the thirty-two-bit representation, the leftmost digit is 0 (immediately after 0b and the thirty-second if you count from the right!) - which corresponds to the sign + of the number following the prefix).

Shift this number by 30 positions to the left 30 << 30 we get in decimal -2147483648, and in binary (32-bit) 0b10000000000000000000000000000000, where the highest digit (after the prefix 0b) is one, which corresponds to the minus sign.

 1
Author: Alexander Chernin, 2019-01-04 14:49:37

From the course of discrete mathematics, I remember:

To write numbers in the computer's memory, a different system of binary codes is used... There are 3 types:

1)Straight 2) Reverse 3) Optional (Codes)

Your computer uses an additional code to write numbers. This is not an ordinary binary code, in it, if the first character is 1, then it is a negative number(that is, numbers starting with the sign 1, negative numbers)

By the way you met with a very useful Now you know more, my advice in addition to learning languages, consider how your computer works (this is not in any case not instructions, just advice))) Good luck!

 3
Author: Просто Кодер, 2019-01-04 14:34:32

You perform operations on signed integers. The first bit is responsible for the sign. In javascript, the js 30 << 30 operation returns 10000000000000000000000000000000 - this number in the reverse code (you wrote above) corresponds to -2147483648 in decimal. In pcp, you have a 64-bit word, so you get a binary number with a zero in the first bit 000000000000000000000000000011110000000000000000000000000000000 that corresponds to 32212254720 in decimal.

To double-check, it is convenient to use the Windows programmer calculator.

If you want to perform such an operation on javascript - you need a 64-bit integer. Use node-int64 or goog.math.Long.html from closure-library

 1
Author: Pavlo Grubyi, 2019-01-04 14:53:50