Solved

Simulating the == operator using the bitwise operators.

Posted on 2006-10-19
10
507 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 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
7-Zip Command Line Issues 12 90
robocopy vs xcopy vs copy 8 108
Help adding usb port to computer running on ms dos 20 80
Copy dir and files with robocopy 2 55
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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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