4

As you might have noticed, -2147483648 = -231 is the negative limit of the signed 32-bit integer range.

When I try to use this value literally...:

set /A -2147483648

..., the following error occurs:

Invalid number.  Numbers are limited to 32-bits of precision.

However, I can state the positive limit 2147483647 = 231 - 1 without any trouble:

set /A 2147483647

So why does set /A behave like that, although the number is perfectly within the applicable range for signed two's complement 32-bit integers?

And is there a way to state the constant -2147483648 directly in a set /A expression?

I know, I could use a variable that is preset to the value, like set NEGLIM=-2147483648, and then use it in an unexpanded way, like set /A NEGLIM, but is there not an immediate way of providing that value?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    Because the program treats the number as a positive 32-bit number and then negates it? Just a guess. – Mick Sep 21 '16 at 21:07
  • 2
    I have investigated this issue, as well as many other issues related to number parsing within CMD.EXE. I published my results at DosTips at [Rules for how CMD.EXE parses numbers](http://www.dostips.com/forum/viewtopic.php?t=3758). I think you will find it interesting. – dbenham Sep 21 '16 at 22:19
  • Wow, I'm impressed, nice work! Thanks a lot for the link! I'm really shocked about the difference in behaviour of `set /A` on Windows XP and later versions... :-O – aschipfl Sep 21 '16 at 23:20

2 Answers2

3

I propose the following workaround:

set /A -2147483647-1

result:

-2147483648

funny note: set /A -2147483647-2 yields 2147483647 so toying with the limit is dangerous :)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 1
    If you look at the binary two's complement representation, the result of `set /A -2147483647-2` becomes obvious... ;-) – aschipfl Sep 21 '16 at 21:46
  • the same thing has to be used in C: [Why do we define INT_MIN as -INT_MAX - 1?](https://stackoverflow.com/q/26003893/995714) – phuclv Sep 12 '18 at 03:15
2

I just found an interesting fact: when using hexadecimal representation, the negative limit can be given:

set /A -0x80000000

Or (also resulting in -2147483648 due to the two's complement representation):

set /A +0x80000000

However, octal notation fails just like the decimal one:

rem These fail:
set /A -020000000000
set /A +020000000000
aschipfl
  • 33,626
  • 12
  • 54
  • 99