Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simulating the == operator using the bitwise operators.

Posted on 2006-10-19
10
Medium Priority
?
537 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 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

688 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