Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

What is 32-bit integer in JavaScript?

I was doing some coding challenges and encountered something I am not too familiar with. I am more curious to learn what it is and why it is there.

The prompt is pretty straightforward:

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 am stumped with some of the failing tests:

Input:
1563847412
Output:
2147483651
Expected: 0

To my understanding, 32 bit integer is 2^32. What is its significance in JS and what happen if I started going over? (2^32 + 1)

My second question, if I may ask two, is I "anticipated" if value of reverseX exceeds 2^32, but it is still failing the test.

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

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

question from:https://stackoverflow.com/questions/47600096/what-is-32-bit-integer-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The upper bound of a signed integer is not 232 - 1, but 231 - 1, since the first bit is the sign bit.

If you make that comparison, you'll see your test gives the right result.

Be aware that JavaScript uses IEEE-754 floating point representation for numbers, even when they are integers. But the precision of floating point is more than enough to perform exact calculations on 32-bit integers. As you realised, you'll need to make 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 take an argument. Here is how I would write it -- see comments in 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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...