hi ,

please refer to the code below:

Code:

//-----code follows----------------------------
#include<stdio.h>
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef unsigned long UInt32;
typedef unsigned long long UInt64;
int main(){
UInt16 lowP = 8874, lowC =61008;
UInt16 midP = 32566, midC =32848;
UInt8 hiP = 0, hiC =0;
//following two lines produce wrong output, note that we dont type cast midP and midC here
UInt64 valP = lowP + (midP << 16) + (((unsigned long long)(hiP))<<32); //line 1
UInt64 valC = lowC + (midC << 16) + (((unsigned long long)(hiC))<<32); //line 2
//uncommenting below two lines instead of above two lines gives right result
// UInt64 valP = lowP + (((unsigned long)midP) << 16) + (((unsigned long long)(hiP))<<32);
//UInt64 valC = lowC + (((unsigned long)midC) << 16) + (((unsigned long long)(hiC))<<32);
UInt64 diff = valC - valP;
printf("valP:%x\n",valP >> 32);
printf("valC:%x\n",valC >> 32);
return 0;
}
//----------------code end-----------------------------

now when I run this program I get following output:

valP:0

valC:ffffffff

However instead if I comment two lines mentioned as line1 and line2 and uncomment two lines

following in the code above, I get the following output:

valP:0

valC:0

Can somebody explain this behaviour? why this behaviour incase we dont typecast to unsigned long. This does not happen with any values but some values.