comparing shorts and ints
Peter Verkaik
Posts: 3,956
Hi,
I wrote a little testprogram to check how shorts and ints behave when compared.
Here are the results:
compare shorts and ints
short a = (short)0x8000 > short b = 1
short a = -32768 > short b = 1
int x = (short)0x8000 > int y = 1
int x = -32768 > int y = 1
short a = (short)0xFFFF < short b = 1
short a = -1 < short b = 1
int x = (short)0xFFFF < int y = 1
int x = -1 < int y = 1
short a = (short)0x8001 < short b = 1
short a = -32767 < short b = 1
int x = (short)0x8001 < int y = 1
int x = -32767 < int y = 1
short a = (short)0x8000 < 0
short a = -32768 < 0
int x = (short)0x8000 < 0
int x = -32768 < 0
short a = (short)0xFFFF < 0
short a = -1 < 0
int x = (short)0xFFFF < 0
int x = -1 < 0
short a = (short)0x8001 < 0
short a = -32767 < 0
int x = (short)0x8001 < 0
int x = -32767 < 0
program finished
It appears when two variables of int or short are compared that are not 0,
the value 0x8000 (eg. -32768) is treated as positive !!!
When comparing against 0, 0x8000 is treated as negative (as it should).
This can have impact on your program if you are not aware of this.
To check for negative value you best compare against 0.
regards peter
I wrote a little testprogram to check how shorts and ints behave when compared.
Here are the results:
compare shorts and ints
short a = (short)0x8000 > short b = 1
short a = -32768 > short b = 1
int x = (short)0x8000 > int y = 1
int x = -32768 > int y = 1
short a = (short)0xFFFF < short b = 1
short a = -1 < short b = 1
int x = (short)0xFFFF < int y = 1
int x = -1 < int y = 1
short a = (short)0x8001 < short b = 1
short a = -32767 < short b = 1
int x = (short)0x8001 < int y = 1
int x = -32767 < int y = 1
short a = (short)0x8000 < 0
short a = -32768 < 0
int x = (short)0x8000 < 0
int x = -32768 < 0
short a = (short)0xFFFF < 0
short a = -1 < 0
int x = (short)0xFFFF < 0
int x = -1 < 0
short a = (short)0x8001 < 0
short a = -32767 < 0
int x = (short)0x8001 < 0
int x = -32767 < 0
program finished
It appears when two variables of int or short are compared that are not 0,
the value 0x8000 (eg. -32768) is treated as positive !!!
When comparing against 0, 0x8000 is treated as negative (as it should).
This can have impact on your program if you are not aware of this.
To check for negative value you best compare against 0.
regards peter
Comments
The results:
compare shorts and ints
short a = (short)0x8000 > short b = 1
short a = -32768 > short b = 1
int x = (short)0x8000 > int y = 1
int x = -32768 > int y = 1
short a = (short)0xFFFF < short b = 1
short a = -1 < short b = 1
int x = (short)0xFFFF < int y = 1
int x = -1 < int y = 1
short a = (short)0x8001 < short b = 1
short a = -32767 < short b = 1
int x = (short)0x8001 < int y = 1
int x = -32767 < int y = 1
short a = (short)0x8000 < 0
short a = -32768 < 0
int x = (short)0x8000 < 0
int x = -32768 < 0
short a = (short)0xFFFF < 0
short a = -1 < 0
int x = (short)0xFFFF < 0
int x = -1 < 0
short a = (short)0x8001 < 0
short a = -32767 < 0
int x = (short)0x8001 < 0
int x = -32767 < 0
short a = (short)0x8001 > short b = 2
short a = -32767 > short b = 2
int x = (short)0x8001 > int y = 2
int x = -32767 > int y = 2
short a = (short)0x8FFF > short b = 4096
short a = -28673 > short b = 4096
int x = (short)0x8FFF > int y = 4096
int x = -28673 > int y = 4096
program finished
It appears whenever a-b or x-y creates an overflow, then a<b and x<y return false
where they should return true. I guess the internal·sequence for a<b is
result = a-b;
return result<0
and this yields false if a-b overflows.
This really is something to look out for as not only the value 0x8000 is involved.
regards peter
compare shorts and ints
test (x<y): int x = -28673 > int y = 4096
test (x>y): int x = -28673 > int y = 4096
test (x<y): int x = 28673 < int y = -4096
test (x>y): int x = 28673 < int y = -4096
test (x<y): int x = -28673 > int y = -28674
test (x>y): int x = -28673 > int y = -28674
test (x<y): int x = 28673 < int y = 28674
test (x>y): int x = 28673 < int y = 28674
program finished
Ok, the result of comparing x<y orr x>y is incorrect,
only, and only if x and y have opposit signs and x-y causes overflow.
When comparing values of equal sign, the results are correct.
regards peter