Solved

dos batch file bitwise and

Posted on 2013-01-23
3
934 Views
Last Modified: 2013-01-23
Hi Experts,

I'm trying to do a simple bitwise and in dos.  The docs say that & is the bitwise character, but when I write:

      if 1 & 1==1 echo hello

I get the following error:

"& was unexpected at this time."

Any ideas how to check a mask on a variable in dos?  (if $myvar$ & 1 == 1)

Thanks!
Mike
0
Comment
Question by:thready
  • 2
3 Comments
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38810487
You will need to use the & in a SET /A statement, and since it's a special character to the command interpreter you need to quote it, or escape it.

So, you first set a variable to the result of the "AND", and then test the result, for example:

set /A "Test=15 & 1"
if %Test% == 1 echo hello


this will also work:

set /A Test=15 ^& 1
if %Test% == 1 echo hello


but this will not:

set /A Test=15 & 1
if %Test% == 1 echo hello


~bp
0
 
LVL 1

Author Closing Comment

by:thready
ID: 38810496
Thanks!
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 38811288
Welcome.

~bp
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question