5

Now signed_int max value is 2,147,483,647 i.e. 2^31 and 1 bit is sign bit, so

when I run long a = 2,147,483,647 + 1;

It gives a = -2,147,483,648 as answer.. This hold good. But, 24*60*60*1000*1000 = 86400000000 (actually)... In java, 24*60*60*1000*1000 it equals to 500654080.. I understand that it is because of overflow in integer, but what processing made this value come, What logic was used to get that number by Java. I also refered here.

Community
  • 1
  • 1
Gru
  • 2,686
  • 21
  • 29
  • 2
    This should be of interest to you - http://stackoverflow.com/q/12758338/1679863 – Rohit Jain Jul 02 '13 at 09:17
  • think of it that way: `24*60*60*1000*1000 = (((24*60)*60)*1000)*1000` and apply the same rule you did in your addition example. – jlordo Jul 02 '13 at 09:18
  • @jlordo I want to know how java processes – Gru Jul 02 '13 at 09:21
  • @Gru. What process are you interested in? I answered a similar question last year, that I've linked in my previous comment. You can go through it. If still you have any doubt, then you can ask for clarification. – Rohit Jain Jul 02 '13 at 09:23
  • @RohitJain Similar? Seems exactly same. Guess that question originated from Bloch's puzzlers text. – devnull Jul 02 '13 at 09:27
  • @devnull. Yeah those are in fact the same question. – Rohit Jain Jul 02 '13 at 09:29

6 Answers6

4

Multiplication is executed from left to right like this

    int x = 24 * 60;    
    x = x * 60;     
    x = x * 1000; 
    x = x * 1000;

first 3 operations produce 86400000 which still fits into Integer.MAX_VALUE. But the last operation produces 86400000000 which is 0x141dd76000 in hex. Bytes above 4 are truncated and we get 0x1dd76000. If we print it

System.out.println(0x1dd76000);

the result will be

500654080
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
3

This is quite subtle: when writing long a = 2147483647 + 1, the right hand side is computed first using ints since you have supplied int literals. But that will clock round to a negative (due to overflow) before being converted to a long. So the promotion from int to long is too late for you.

To circumvent this behaviour, you need to promote at least one of the arguments to a long literal by suffixing an L.

This applies to all arithmetic operations using literals (i.e. also your multiplication): you need to promote one of them to a long type.

The fact that your multiplication answer is 500654080 can be seen by looking at

long n = 24L*60*60*1000*1000;
long m = n % 4294967296L; /* % is extracting the int part so m is 500654080 
                             n.b. 4294967296L is 2^32 (using OP notation, not XOR). */

What's happening here is that you are going 'round and round the clock' with the int type. Yes, you are losing the carry bits but that doesn't matter with multiplication.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I want to know how 24*60*60*1000*1000 it equals to 500654080, what processing made java come up with this number – Gru Jul 02 '13 at 09:24
  • @Gru, I've put the reconciliation in my answer – Bathsheba Jul 02 '13 at 09:33
  • your answer was good but @Evgeniy Dorofeev provided step by step analysis, +1 for your answer...Thanks... – Gru Jul 02 '13 at 09:54
1

As the range of int is -2,147,483,648 to 2,147,483,647. So, when you keep on adding numbers and its exceed the maximum limit it start gain from the left most number i.e. -2,147,483,648, as it works as a cycle. That you had already mentioned in your question.

Similarly when you are computing 24*60*60*1000*1000 which should result 86400000000 as per Maths.

But actually what happens is somehow as follows:

86400000000 can be written as 2147483647+2147483647+2147483647+2147483647+..36 times+500654080

So, after adding 2147483647 for 40 times results 0 and then 500654080 is left which ultimately results in 500654080.

I hope its clear to you.

Nitesh Mishra
  • 311
  • 1
  • 10
0

Add L in your multiplicatoin. If you add L than it multiply you in Long range otherwise in Integer range which overflow. Try to multiply like this.

24L*60*60*1000*1000

This give you a right answer.

Masudul
  • 21,823
  • 5
  • 43
  • 58
  • I already know this check for the link in question, my question how java processes and gives a result such number. – Gru Jul 02 '13 at 09:20
0

An Integer is 32 bit long. Lets take for example a number that is 4 bit long for the sake of simplicity.

It's max positive value would be:

0111 = 7 (first bit is for sign; 0 means positive, 1 means negative)
0000 = 0

It's min negative value would be:

1111 = -8 (first bit is for sign)
1000 = -1

Now, if we call this type fbit, fbit_max is equal to 7.

fbit_max + 1 = -8
because bitwise 0111 + 1 = 1111

Therefore, the span of fbit_min to fbit_max is 16. From -8 to 7.

If you would multiply something like 7*10 and store it in fbit, the result would be:

fbit number = 7 * 10 (actually 70)
fbit number = 7 (to get to from zero to max) + 16 (min to max) + 16 (min to max) + 16 (min to max) + 15 (the rest)
fbit number = 6
darijan
  • 9,725
  • 25
  • 38
0
24*60*60*1000*1000 = 86400000000

Using MOD as follows: 86400000000 % 2147483648 = 500654080

Multithreader
  • 878
  • 6
  • 14