If a variable counting hundredth of a second is stored in a signed long 32-bit integer, how many days, to two decimals, will it take until that integer overflows?
Asked
Active
Viewed 1,680 times
-1
-
I'm confused by: "_a long 32-bit integer_". In most modern compilers, `long` would usually refer to a 64-bit integer. – Martin Jun 11 '21 at 10:21
-
Regardless, a signed 32-bit integer has a maximum value of `2147483647`. It's not difficult to calculate the result to the question knowing that – Martin Jun 11 '21 at 10:23
-
@Martin Please explain the solution. – ThatGuyFromTitan Jun 11 '21 at 10:36
-
I'm really not certain why it's problematic but I've answered below to show how to calculate it – Martin Jun 11 '21 at 10:43
1 Answers
0
Forgoing the nomenclature regarding "long" vs "32-bit integer", assuming you mean a signed 32-bit integer
, this is simple to calculate as follows:
- Signed 32-bit integer has a maximum value of
2,147,483,647
. - When storing 1/100ths of a second, this gives a maximum value of
21,474,836.47
seconds. - There are
86,400
seconds in a day -60 x 60 x 24 = 86,400
. - The value in days is therefore
21,474,836.47 / 86,400 = 248.5513480...
. - This value to 2dp is
248.55
.
Again:
2147483647 ' Signed 32-bit integer max value
/ 100 ' Divide by 100 to get value in seconds
= 21474836.47
/ 86400 ' Divide this by seconds in a day
= 248.55 ' Days (rounded to 2dp)
Alternatively you could simply have divided by hundredths of a seconds in a day (8,640,000
) to get the same result:
2147483647 ' Signed 32-bit integer max value
/ 8640000 ' Divide by hundredths of a second in a day
= 248.55 ' Days (rounded to 2dp)
Of course, if you actually meant a "long" (a signed 64-bit integer
) then the answer becomes the significantly larger 1,067,519,911,673.01
days (2dp) which is the equivalent of nearly 3 billion years!

Martin
- 16,093
- 1
- 29
- 48