Help with set /a command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Help with set /a command

#1 Post by Rileyh » 05 Sep 2011 21:43

Hi all,
Could someone give me an easy-to-understand overview of the set /a command?
Preferably with an example.

Thanks,
Rileyh

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Help with set /a command

#2 Post by nitt » 05 Sep 2011 22:16

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

Code: Select all

set /a var=5+5
echo %var%


will return "10".

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: Help with set /a command

#3 Post by Bob D » 07 Sep 2011 19:40

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 .

Exouxas
Posts: 34
Joined: 01 Sep 2011 12:52

Re: Help with set /a command

#4 Post by Exouxas » 08 Sep 2011 10:28

Pretty sure '!' means 'not'. In a different scripting language it was like this tho:

Code: Select all

if(var1 != var1) {do stuff}


Basically means if variable 1 is not equal to variable 2 the do something :P

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: Help with set /a command

#5 Post by Bob D » 08 Sep 2011 21:28

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.

dbenham
Expert
Posts: 2165
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with set /a command

#6 Post by dbenham » 08 Sep 2011 22:48

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

Code: Select all

set /a "x1=1, x2=x1+2"
set x
output

Code: Select all

x1=1
x2=3


Dave Benham

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: Help with set /a command

#7 Post by Bob D » 09 Sep 2011 01:28

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 :lol: :lol: :lol: :lol:

dbenham
Expert
Posts: 2165
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with set /a command

#8 Post by dbenham » 09 Sep 2011 08:02

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

aGerman
Expert
Posts: 3413
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with set /a command

#9 Post by aGerman » 09 Sep 2011 08:06

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

dbenham
Expert
Posts: 2165
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with set /a command

#10 Post by dbenham » 09 Sep 2011 08:38

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

aGerman
Expert
Posts: 3413
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with set /a command

#11 Post by aGerman » 09 Sep 2011 09:26

You're right Dave. I assume logical and arithmetic shift are only the same for the left shift operator.

Regards
aGerman

dbenham
Expert
Posts: 2165
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with set /a command

#12 Post by dbenham » 09 Sep 2011 09:46

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

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Help with set /a command

#13 Post by trebor68 » 09 Sep 2011 13:24

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

Captcha142
Posts: 13
Joined: 18 Sep 2011 23:35

Re: Help with set /a command

#14 Post by Captcha142 » 19 Sep 2011 18:00

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.

aGerman
Expert
Posts: 3413
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with set /a command

#15 Post by aGerman » 19 Sep 2011 18:31

The variable can equal 0 if you would write the redirection in an opposite way:

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

Post Reply