Solved

Simulating the == operator using the bitwise operators.

Posted on 2006-10-19
10
528 Views
Last Modified: 2012-06-21
I would like to know how to simulate the == operator using the bitwise &, |, ~ and ^ operators in the

set /A expression= (some expression)

command.

Specifically, I want to simulate the if stmt condition:     if (j==4)&&(option==1)   { do something } .  But I don't know of a way in DOS to combine two conditions.  I tried:     if %j%==4&%option%==1 , but this doesn't work.  I figured that if did something like this:

set /A answer1=j&4        ::wanted to get 1 or 0 based on j and 4s equality, but doesn't work because I get 4 if they    
                                     ::are equal and I can't get zero unless j is zero.
set /A answer2=option&1::wanted the same thing to happen here, which is easier because if option is one
                                     ::then the answer2 becomes 1, but if the option isn't one it won't be zero unless option is zero

:: then I compare answer2 and answer1 based on the fact they should be 1 or 0.

if %answer1%==%answer2% { do something}
0
Comment
Question by:alphonsobasemore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17766909
Hi alphonsobasemore,

a^a is 0
i.e. if two numbers are same, XORing them would give you zero. Using not would give you desired result

Also note that if you strictly want 1 and 0, then not of not would convert a value > 1 to 1

Cheers!
sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17766941
ignore the bit about not of not ... Realized that not on DOS returns bitwise compliment and not boolean opposite ... XOR part still holds
0
 
LVL 10

Accepted Solution

by:
GuruGary earned 500 total points
ID: 17766948
For a solution to your initial problem, you can do:

if {%j%}=={4} if {%option%}=={1} echo Do something
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:alphonsobasemore
ID: 17767005
Great guys I'll try both solutions and see, which one does what I want.
0
 

Author Comment

by:alphonsobasemore
ID: 17767099
GuruGary seems to work great!  Thats essentially all I wanted to do, but I didn't know exactly how to accomplish that in the batch file using the == operator.

So GuruGary in your own words exactly what is the trick with using the

if {%j%}=={4} if {%option%}=={1} echo Do something

statement that allows you to && (and in C++) the if stmts together?  I guess its because the anding of the conditions is the same as using nested if statements right? But whats the {} for? What if I wanted to || (or in C++) the stmts together what would I change?
0
 

Author Comment

by:alphonsobasemore
ID: 17767863
sunnycoder I've switched to your a^a solution, because it seems I can manipulate the answers to do what I want when I need to OR the answer to j==4 with option==1.

I can't figure out a way to OR GuruGary's solution as needed!

I used this:

set /A ans="(!(j^4))|(!(option^1))"

or simply

set /A ans="(!(j^4))|option"

, which is the same as if ((j==4)||(option==1)) only I use bitwise operators to get a 1 or 0 for the final condition's value.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17768114
:)
0
 
LVL 10

Expert Comment

by:GuruGary
ID: 17771476
Sorry for the delay.  Not sure if you are even using my solution any more ... but to answer your question the statement is basically
if ((j==4) and (option==1)) then do something

or closer to the actual batch code in C format:
if (j==4) {
   if (option==1) {
      echo Do something;
      }
   }

The {} is to prevent errors in case you check against a blank value.  For example, if %option% never gets set, then the batch file line would be
if ==1
... which is not valid syntax and would generate an error.  If you use the {} and %option% is blank the batch code would be
if {}=={1}
which is valid syntax.  You can use most any character(s) to compare as long as they are the same on both sides.
0
 

Author Comment

by:alphonsobasemore
ID: 17773162
Just like I thought about your code!  But if you look at the later posts that I have on this question you'll see that the code you gave isn't quite what I needed.  If you know the alternate format of that code, but instead of using an and format I wanted to use the or format of that code.  Is there a way to state if ((j==4)or(option==1)) then do something in dos?  Or  am I stuck using the code in the and format?
0
 
LVL 10

Expert Comment

by:GuruGary
ID: 17778012
Well, I can think of several ways to accomplish that task, but not with a "logical OR" directly in the IF statement.  See if any of these will work for what you want to do:

Batch 1:
if {%j%}=={4} (
   echo Do something
   ) else if {%option%}=={1} echo Do something

Batch 2:
if {%j%}=={4} call :Something
if {%option%}=={1} if not {%j%}=={4} call :Something
goto :EOF

:Something
echo Do something
goto :EOF

Batch 3:
set test=0
if {%j%}=={4} set /a test+=1
if {%option%}=={1} set /a test+=1
if %test% GEQ 1 echo Do something

Batch 4:
if {%j%}=={4} (
   goto :Continue
   ) else if {%option%}=={1} goto :Continue
goto :Skip
:Continue
echo Do something
:Skip

... or any combinations of the ideas above.
Post back if you have questions or need more help or ideas.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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