Solved

Simulating the == operator using the bitwise operators.

Posted on 2006-10-19
10
482 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
  • 4
  • 3
  • 3
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
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
Comment Utility
For a solution to your initial problem, you can do:

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

Author Comment

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

Author Comment

by:alphonsobasemore
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:alphonsobasemore
Comment Utility
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
Comment Utility
:)
0
 
LVL 10

Expert Comment

by:GuruGary
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now