Can anyone explain to me why is util2=49, but util1=5?
Program:
#include <stdio.h>
int main()
{
int load1 = 47759534;
int capacity1 = 96000000;
long load2 = 47759534;
int capacity2 = 96000000;
int util1 = (100*load1)/capacity1;
int util2 = (100*load2)/capacity2;
printf("util1 = %d\n", util1);
printf("util2 = %d\n", util2);
return 0;
}
Output:
util1 = 5
util2 = 49
I understand that 'int' is at least 16 bits and 'long' is at least 32 bits but load1 in the above snippet is within the common range of int and long, i.e, 2,147,483,647. Why is util1 rounded off to 5 then?
In C, int is usually 32 bits. Let's assume that is the case here. When you multiply by 100, the result in binary is:
000100011100101010110100001111111000
Since that is more than 32 bits, the most significant bits get discarded. If you take the lowest 32 bit you get 00011100101010110100001111111000 = 480,986,104
If you divide that by 96000000 you'll get 5.