Solved

dos batch file bitwise and

Posted on 2013-01-23
3
905 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 53

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 53

Expert Comment

by:Bill Prew
ID: 38811288
Welcome.

~bp
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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