Page 1 of 2
Help with set /a command
Posted: 05 Sep 2011 21:43
by Rileyh
Hi all,
Could someone give me an easy-to-understand overview of the set /a command?
Preferably with an example.
Thanks,
Rileyh
Re: Help with set /a command
Posted: 05 Sep 2011 22:16
by nitt
Rileyh wrote:Hi all,
Could someone give me an easy-to-understand overview of the set /a command?
Preferably with an example.
Thanks,
Rileyh
"set" is used to define variables. "set /a" just views your variable as an equation and tries to solve it. Such as
will return "10".
Re: Help with set /a command
Posted: 07 Sep 2011 19:40
by Bob D
Further to the original question, I am struggling with certain aspects of the set /a command. Below is copied from Win 7 command window help for set.
() z z z z z z z z z - grouping
! ~ - z z z z z z z - unary operators ! what is it
* / % z z z z z z z - arithmetic operators
+ - z z z z z z z z - arithmetic operators
<< >> z z z z z z - logical shift << >> how to use them
& z z z z z zz z z z - bitwise and
^ z z z z z z z z z - bitwise exclusive or
| z z z z z z z z z - bitwise or
= *= /= %= += -= - assignment = I understand but what are the rest and how to use
&= ^= |= <<= >>= - assignment what are these and how to use
, z z z z z z z z z z z - expression separator how to use
I don't understand what some of these are and how to use them. Some help would be great. See my comments to the right in red. It looks a bit messy but you should have seen it before I padded some of the lines with z z z z z z .
Re: Help with set /a command
Posted: 08 Sep 2011 10:28
by Exouxas
Pretty sure '!' means 'not'. In a different scripting language it was like this tho:
Basically means if variable 1 is not equal to variable 2 the do something

Re: Help with set /a command
Posted: 08 Sep 2011 21:28
by Bob D
With a bit of looking round Wikipedia and some experimentation I have worked out the answers to all of my questions about the batch operators except the unary ! I cant seem to use it and get a meaningful result. The big problem is I am not sure what it is. Is it a logical NOT or a ones complement (bitwise inversion). Exouxas ventures out and suggests it as a comparison operator but that does not work (for me). The batch IF already has the following as comparison operators EQU, NEQ, LSS, LEQ, GTR, GEQ so it would seem to be unnecessary to use others. I have been looking at
http://en.wikipedia.org/wiki/Unary_operator and
http://en.wikipedia.org/wiki/Negation and I am now bogged down.
edit
I forgot about the expression separator , does nayone have any examples of usage.
Re: Help with set /a command
Posted: 08 Sep 2011 22:48
by dbenham
Bob D wrote:I have worked out the answers to all of my questions about the batch operators except the unary ! I cant seem to use it and get a meaningful result. The big problem is I am not sure what it is. Is it a logical NOT or a ones complement (bitwise inversion).
The ~ operator provides the one's complement (otherwise known as bitwise negation or bitwise inversion)
The ! operator provides logical negation with a twist: It assumes 0 represents FALSE, and any non-zero value represents TRUE. The result is always expressed as either 0 (FALSE) or 1 (TRUE).
So !(0)=1 and !(non-zero)=0
Bob D wrote:I forgot about the expression separator , does nayone have any examples of usage.
The comma allows multiple assignments in the same statement
output
Dave Benham
Re: Help with set /a command
Posted: 09 Sep 2011 01:28
by Bob D
Re: Help with set /a command
Posted: 09 Sep 2011 08:02
by dbenham
Bob D wrote:Also the info on ~. I had it worked out as twos complement.
The - unary operator provides the two's compliment (numeric negation)
Dave Benham
Re: Help with set /a command
Posted: 09 Sep 2011 08:06
by aGerman
An example for "logical shift":
Code: Select all
@echo off &setlocal
set /a "result=7<<2"
echo %result%
pause
The output is 28.
Why?
The binary expression for 7 is 111.
Shifting 2 digits to the left results to
11100 (bin) = 28 (dec).
Regards
aGerman
Re: Help with set /a command
Posted: 09 Sep 2011 08:38
by dbenham
Beware - the HELP for the SET command is wrong with regard to the shift operators
The help states they are logical shift operators when in fact they are arithmetic shift operators. This has a major impact on the right shift of a negative number.
The right shift (>>) fills in with the value of the sign bit such that a negative number always remains negative.
The left shift (<<) always fills in with 0.
Neither operator detects overflow.
example: SET /A "1<<31>>31" yields -1 (all bits set)
If this were true logical shifts then the answer would be 1 (only 1 bit set)
Dave Benham
Re: Help with set /a command
Posted: 09 Sep 2011 09:26
by aGerman
You're right Dave. I assume logical and arithmetic shift are only the same for the left shift operator.
Regards
aGerman
Re: Help with set /a command
Posted: 09 Sep 2011 09:46
by dbenham
Some systems differentiate between arithmetic left shift and logical left shift by detecting overflow during arithmetic left shifts.
Without overflow detection, arithmetic and logical left shifts are indeed the same.
Our "DOS" SET /A arithmetic shift operators do not detect overflow.
Dave Benham
Re: Help with set /a command
Posted: 09 Sep 2011 13:24
by trebor68
For the operations += -= *= /= ...
If you have a variable and used this variable with the operation executed is this a easy way.
i.E.
num=23
num=num+5
The same result
num=23
num+=5
Code: Select all
@echo off
set num=23
echo original %num%
set /a num+=5
echo new %num%
pause
Re: Help with set /a command
Posted: 19 Sep 2011 18:00
by Captcha142
The set /a command can also be used when creating loops.
eg. To write a text file counting from 1 to 100 named numbers write:
Code: Select all
@echo off
set x=1
:start
if NOT "%x%"=="101" (
echo %x%>>numbers.txt
set /a x=%x%+1
goto start
)
echo Done!
pause
NOTE: the variable can't equal zero, thus must start at 1, and the if parameter must be set 1 greater than desired stopping point.
Re: Help with set /a command
Posted: 19 Sep 2011 18:31
by aGerman
The variable can equal 0 if you would write the redirection in an opposite way:
Also there is an operator += to increment a number:
BTW: A FOR /L loop would be a better solution.
Regards
aGerman