Here is a little known feature of our beloved CMD.EXE. It is extremely easy to evaluate an arithmetic expression! All it takes is a five characters. Just type set /a followed by your expression.
Here is an example:
E:\>set /a 1+12
Or, if you want, you can convert hex numbers to decimal:
E:\>set /a 0x8000ffff-2147418113
Or even more, you can use and assign variables:
E:\>set /a i=1+23E:\>set /a j=i+14
You can use this everywhere, for example in loops:
E:\>for /L %i in (1,1,10) do @set /a 1^<^<%i & echo.2481632641282565121024
So what operators can you use? That's easy to figure out. Here is a snippet from the "set /?" help text...
The /A switch specifies that the string to the right of the equal signis a numerical expression that is evaluated. The expression evaluatoris pretty simple and supports the following operations, in decreasingorder of precedence: () - grouping ! ~ - - unary operators * / % - arithmetic operators + - - arithmetic operators << >> - logical shift & - bitwise and ^ - bitwise exclusive or | - bitwise or = *= /= %= += -= - assignment &= ^= |= <<= >>= , - expression separator If you use any of the logical or modulus operators, you will need toenclose the expression string in quotes. Any non-numeric strings in theexpression are treated as environment variable names whose values areconverted to numbers before using them. If an environment variable nameis specified but is not defined in the current environment, then a valueof zero is used. This allows you to do arithmetic with environmentvariable values without having to type all those % signs to get theirvalues. If SET /A is executed from the command line outside of acommand script, then it displays the final value of the expression. Theassignment operator requires an environment variable name to the left ofthe assignment operator. Numeric values are decimal numbers, unlessprefixed by 0x for hexadecimal numbers, and 0 for octal numbers.So 0x12 is the same as 18 is the same as 022. Please note that the octalnotation can be confusing: 08 and 09 are not valid numbers because 8 and9 are not valid octal digits.
The /A switch specifies that the string to the right of the equal signis a numerical expression that is evaluated. The expression evaluatoris pretty simple and supports the following operations, in decreasingorder of precedence:
() - grouping ! ~ - - unary operators * / % - arithmetic operators + - - arithmetic operators << >> - logical shift & - bitwise and ^ - bitwise exclusive or | - bitwise or = *= /= %= += -= - assignment &= ^= |= <<= >>= , - expression separator
If you use any of the logical or modulus operators, you will need toenclose the expression string in quotes. Any non-numeric strings in theexpression are treated as environment variable names whose values areconverted to numbers before using them. If an environment variable nameis specified but is not defined in the current environment, then a valueof zero is used. This allows you to do arithmetic with environmentvariable values without having to type all those % signs to get theirvalues. If SET /A is executed from the command line outside of acommand script, then it displays the final value of the expression. Theassignment operator requires an environment variable name to the left ofthe assignment operator. Numeric values are decimal numbers, unlessprefixed by 0x for hexadecimal numbers, and 0 for octal numbers.So 0x12 is the same as 18 is the same as 022. Please note that the octalnotation can be confusing: 08 and 09 are not valid numbers because 8 and9 are not valid octal digits.