What is a 32-bit integer in Javascript?

I did some coding issues and came across something that I am not very familiar with. I am more curious to know what it is and why it is.

On the command line, it's pretty simple: Given a 32-bit signed integer, reverse digits of an integer.

 Example: Input: -123 Output: -321 Example: Input: 120 Output: 21 Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 

I came up with this.

 var reverse = function(x) { var isNegative = false; if(x < 0){ isNegative = true; x *= -1; }; var reverseX = parseInt(String(x).split('').reverse((a,b) => a - b).join('')); if(reverseX > Math.pow(2,32)){ return 0; } if(isNegative){ return -1 * reverseX } else { return reverseX; } }; 

However, I came across some unsuccessful tests:

 Input: 1563847412 Output: 2147483651 Expected: 0 

As far as I understand, a 32-bit integer is 2 ^ 32. What is its value in JS and what happens if I start moving? ( 2^32 + 1 )

My second question is, if I can ask two, โ€œI expectedโ€ if the reverseX value exceeds 2 ^ 32, but it still does not pass the test.

  if(reverseX > Math.pow(2,32)){ return 0; } 

How can I appropriately return 0 when I exceeded the 32-bit integer?

+5
source share
3 answers

The upper bound of a signed integer is not 2 32 - 1, but 2 31 - 1, since the first bit is a sign bit.

If you make this comparison, you will see that your test gives the correct result.

Remember that JavaScript uses the IEEE-754 floating point representation for numbers, even if they are integers. But floating point precision is more than enough to perform accurate calculations on 32-bit integers. As you understand, you need to do the necessary test to detect 32-bit overflow.

Some notes about your code: it passes an argument to the Array#reverse method, which is a method that does not accept an argument, Here is how I would write it - see comments in the code:

 // Name argument n instead of x, as that latter is commonly used for decimal numbers function reverse(n) { // Array#reverse method takes no argument. // You can use `Math.abs()` instead of changing the sign if negative. // Conversion of string to number can be done with unary plus operator. var reverseN = +String(Math.abs(n)).split('').reverse().join(''); // Use a number constant instead of calculating the power if (reverseN > 0x7FFFFFFF) { return 0; } // As we did not change the sign, you can do without the boolean isNegative. // Don't multiply with -1, just use the unary minus operator. // The ternary operator might interest you as well (you could even use it // to combine the above return into one return statement) return n < 0 ? -reverseN : reverseN; } console.log(reverse(-123)); console.log(reverse(1563847412)); 
+2
source

However, I came across some unsuccessful tests:

 Input: 1563847412 Output: 2147483651 Expected: 0 

the maximum 32-bit integer, which, it seems to me, is (2^31) , which is 2,147,483,647. This means that negative values โ€‹โ€‹can be stored, and (-2^31) is the 32-bit limit (this means "signed"). Thus, any number above this, you can return 0 for your program. If the prompt says "unsigned", the range will be 0 to 2^32 , as you originally intended.

In terms of the failed test, 2147483651 is 4 more than 2,147,483,647 , so you should return 0. Instead, you should say reverseX > Math.pow(2,31) - 1

What is its significance in JS and what will happen if I start the transition? (2 ^ 32 + 1)

Technically, in JS you are not limited to this number, JS uses a significant and double-precision floating-point number . Thus, the maximum value is valid (2^53) - 1

+1
source

Here is my solution to this issue. I would not recommend the split () method. Join () as it greatly increases the complexity of time and space.

 // 0(n) var reverse = function(x) { var reverse = 0 var isNegative = x < 0 ? -1 : 1 x = x * isNegative // capture single digits if (x / 10 < 1) { return x } // reverse while (x >= 1) { var diff = parseInt(x % 10) reverse = (reverse * 10) + diff x = x / 10 } // capture greater than 32bit if (reverse > Math.pow(2,31)-1) { return 0; } // capture negative return reverse * isNegative }; 
0
source

Source: https://habr.com/ru/post/1273803/


All Articles