views:

164

answers:

5

Found this code in our code base the other day. Not sure what it is used for. Any guesses?

function checkIntegerRange(x) {
  return ((x >= 0) && (x < 2020202020)) || (x == 2147483647) || (x == 4294967295);
}
+2  A: 

What it does is validate that x is in the range 0..2020202020 or x == 2^31-1 (2147483647, the maximum positive value in a 32-bit signed integer) or x == 2^32-1 (4294967295; which would be -1 in a two's complement 32-bit signed integer value, or the highest value that can be stored in a 32-bit unsigned integer value).

My suspicion is that it's trying to figure out whether x will fit in a 32-bit integer, but I can't for the life of me figure out why it has the odd range at the beginning and why it makes the big positive exception and the -1 (or other big positive, depending) exception.

T.J. Crowder
+6  A: 

2147483647 is the highest value that can be stored in a typical signed 32-bit integer type. 4294967295 is the analogous value for a 32-bit unsigned integer type. Possibly some other part of your code is using these as special marker values.

I have no idea what 2020202020 might signify, though it has the look of an arbitrarily chosen upper bound on something.

Syntactic
+1  A: 

it returns a boolean (true, false) if the number sent to it is between 0 (inclusive) and 2020202020 (non inclusive) or if the number equals 2147483647 or if it equals 4294967295.

As for the purpose... that's up to you to find out ;)

Shaded
A: 

seems to be a kind of filtering/flagging:

2147483647:  Hex 7FFFFFFF or bin 1111111111111111111111111111111
4294967295  Hex: FFFFFFFF or bin 11111111111111111111111111111111

BTW: 2*2147483647 = 4294967295-1

I would say it should check between a certain range or against some funny flags

maersu
+3  A: 

2020202020 is the conversion of " " (5 spaces) to a hex string. The author (probably one prone to writing obfuscated code :) may have wanted to ensure that a string of minimum of 5 characters converted to hex was not an considered an integer.

Here is a sample converter http://www.string-functions.com/string-hex.aspx

agilefall
Uh, no, that would be 0x2020202020. 2020202020 is a decimal number
BlueRaja - Danny Pflughoeft

related questions