Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simulating the == operator using the bitwise operators.

Posted on 2006-10-19
10
Medium Priority
?
541 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
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 2000 total points
ID: 17766948
For a solution to your initial problem, you can do:

if {%j%}=={4} if {%option%}=={1} echo Do something
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

782 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