Help with set /a command
Moderator: DosItHelp
Help with set /a command
Hi all,
Could someone give me an easytounderstand overview of the set /a command?
Preferably with an example.
Thanks,
Rileyh
Could someone give me an easytounderstand overview of the set /a command?
Preferably with an example.
Thanks,
Rileyh
Re: Help with set /a command
Rileyh wrote:Hi all,
Could someone give me an easytounderstand 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
Code: Select all
set /a var=5+5
echo %var%
will return "10".
Re: Help with set /a command
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 .
() 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
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
Code: Select all
if(var1 != var1) {do stuff}
Basically means if variable 1 is not equal to variable 2 the do something
Re: Help with set /a command
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.
edit
I forgot about the expression separator , does nayone have any examples of usage.
Re: Help with set /a command
The ~ operator provides the one's complement (otherwise known as bitwise negation or bitwise inversion)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 logical negation with a twist: It assumes 0 represents FALSE, and any nonzero value represents TRUE. The result is always expressed as either 0 (FALSE) or 1 (TRUE).
So !(0)=1 and !(nonzero)=0
The comma allows multiple assignments in the same statementBob D wrote:I forgot about the expression separator , does nayone have any examples of usage.
Code: Select all
set /a "x1=1, x2=x1+2"
set x
Code: Select all
x1=1
x2=3
Dave Benham
Re: Help with set /a command
Thanks Dave, it all makes sense now. When I was testing the ! I was getting the correct results but I just didn't believe them. Also the info on ~. I had it worked out as twos complement. Thanks for correcting my error. I'm getting there. I haven't done much in higher languages (if you could call Batch that) for about 30 years. So I'm a bit rusty. More recently I have been doing IBM 370 Assembler but I'm rusty on that too as I retired about 10 years back and I haven't been near one of them since.
Bob
Bob
Re: Help with set /a command
The  unary operator provides the two's compliment (numeric negation)Bob D wrote:Also the info on ~. I had it worked out as twos complement.
Dave Benham
Re: Help with set /a command
An example for "logical shift":
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
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
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
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
You're right Dave. I assume logical and arithmetic shift are only the same for the left shift operator.
Regards
aGerman
Regards
aGerman
Re: Help with set /a command
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
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
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
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

 Posts: 13
 Joined: 18 Sep 2011 23:35
Re: Help with set /a command
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:
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.
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
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
Code: Select all
>>numbers.txt echo %x%
Also there is an operator += to increment a number:
Code: Select all
set /a x+=1
BTW: A FOR /L loop would be a better solution.
Regards
aGerman