0

I have a 32-bit integer size

if I have an arithmetic expression like

unsigned short current_time, last_time
if((current_time - last_time) > timeout)

I believe current_time and last_time will both be converted to signed int 32 before the subtraction. There are no problems with a 16-bit integer size system, but with this 32-bit integer size system will there be potential for a negative value because of the integer promotion?

gogolf0401
  • 73
  • 1
  • 10

2 Answers2

1

If current_time is greater than or equal to last_time, there won't be potential for a negative value.

To quote section 6.2.1.2 "Signed and unsigned integers" of the C90 spec:

When a value with integral type is converted to another integral type, if the value can be represented by the new type, its value is unchanged.

With unsigned short being shorter than int, all values of type unsigned short can be represented by int, so the converted-to-int values of current_time and last_time will be the same as their un-converted unsigned short value, and the result of the subtraction will be what you expect it to be.

If, however, current_time is less than last_time, there is potential for a negative value; that, however, is not a bug, it's a feature, because, in that case, time really did go backwards.

If current_time and last_time were unsigned ints, and current_time were less than last_time, the result of the subtraction would be an unsigned int, and thus not negative; it would be the difference between the times, modulo the maximum value of an unsigned int.

You probably want to handle time going backwards specially, anyway, if it can happen.

  • 1
    I don't know the unit but my guess is that 16-bit time is likely to overflow fairly often. If so then I also suspect that the best bet is to view the time as circular and use `(unsigned short) ((unsigned int) current_time - last_time) < timeout` instead, thereby correctly comparing time values within a limited frame. Alternatively with a cast to signed short instead if negative periods are possible and compiling for a sane two's complement environment. – doynax Oct 14 '15 at 14:31
0

By modern C rules, unsigned shorts will promote to signed ints, and the difference will be correctly signed according to their original values.

Paul Kienitz
  • 878
  • 6
  • 25
  • would the promotion occur on each variable of the subtraction or just on the result? If the promotion occurs on each variable before the subtraction is it possible to get a negative value after the subtraction? Maybe when the 16 bit value current time overflows back to 0 and the last time is 65000+? – gogolf0401 Oct 14 '15 at 15:35
  • Yes, if a 16 bit value overflows and becomes small, then promotion would preserve that small value, and the subtraction would return a negative result. In that case, promotion is not your problem -- the use of unsigned short in the first place is. If you want to avoid this, you have to cast each value individually to unsigned int. In which case you'll get values that appear to have nothing wrong with them but may be incorrect. – Paul Kienitz Oct 14 '15 at 20:04