Or operand with int in if statement


My problem is that program is not reading codes as i intended "he" would.

I have

if (hero.getPos() == (6 | 11 | 16)) { move = new Object[] {"Up", "Right", "Left"}; } else { move = new Object[] {"Up", "Down", "Right", "Left"}; }

When hero position is 6, the program still goes to else.

Why is that? Is it because of operands? If yes, how should i change it?



if (hero.getPos() == 6 || hero.getPos() == 11 || hero.getPos() == 16)) {

This will do what you want.

What you did is comparing hero.getPos() with the result of (6|11|16) which will do <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html" rel="nofollow">bitwise or</a> between those numbers.


The other answers are correct, just thinking differently you may use Sets.

static final Set<Integer> positions = new HashSet<Integer>(); static{ positions.add(6); positions.add(11); positions.add(16); } if (positions.contains(hero.getPos())){ move = new Object[] {"Up", "Right", "Left"}; } else { move = new Object[] {"Up", "Down", "Right", "Left"}; }


You cannot do it like that. It ors the 3 number bitwise.

You have to do like this :

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) { move = new Object[] {"Up", "Right", "Left"}; } else { move = new Object[] {"Up", "Down", "Right", "Left"}; }

You see the difference ? | is a bitwise or while || is a logical or. Note also that you have to rewrite the comparison each time.


(6 | 11 | 16) would be evaluated first to 31 (binary operation), which is 6 != 31. Not what you want.

Better is to check every single position (you have only 3, so inline is good, for more consider using a loop):

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) { move = new Object[] {"Up", "Right", "Left"}; } else { move = new Object[] {"Up", "Down", "Right", "Left"}; }


No, you're going to need to check ci.getNumber() == ... for each value, or add them to a collection and check myCollection.contains(ci.getNumber()). However, you may want to re-think the structure of your code if you are checking a method against several known values.


using the answer from:

<a href="https://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an-array-contains-a-certain-value" rel="nofollow">How can I test if an array contains a certain value?</a>

you could create an array of numbers and check if your ci.getNumber() is in it.


No. You could create a Set<Integer> once and then use that, or just:

int number = ci.getNumber(); if (number == 6252001 || number == 5855797 || number == 6251999)

I'd also consider changing those numbers into constants so that you get more meaningful code.


There is no such operator. But if you are comparing number, you can use switch do simulate that. Here is how:

int aNumber = ci.getNumber(); swithc(aNumber) { case 6252001: case 5855797: case 6251999: { ... break; } default: { ... // Do else. } }

Hope this helps.


boolean theyAretheSame = num1 == num2 ? (num1 == num3 ? true:false):false;

I must admit I haven't checked this but the logic looks correct.


You could put all the numbers in a collection, and then use the contains() method. Other than that, I don't believe there is any special syntax for comparing like you want to do.


Java won't let you do that. You can do a hash lookup (which is overkill for this) or a case statement, or a big honking ugly multiple compare:

if ((n==1 ) || (n==2) || ...


no.. you have to compare them individually.


  • NSPredicate : CoreData fetchRequest filtered by Array of dictionaries
  • get the days between the dates [duplicate]
  • PHP multiple OR in if condition
  • General contract for object comparision : equals() and hashCode()
  • Variable might not have been initialized
  • Pythonic Way to have multiple Or's when conditioning in a dataframe
  • MATLAB quick find of a vector in matrix
  • Complex Object won't return when using 'include' syntax in WCF with Entity Version 6
  • Java, will (low + high) >>> 1 overflow?
  • get passwords from chrome
  • How to use animated gif in Firemonkey?
  • Efficient algorithm to find additions and removals from 2 collections
  • Java color detection
  • Z3: Convert between FP and BitVector?
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • OOP Javascript - Is “get property” method necessary?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Scrapy recursive link crawler
  • Is there a javascript serializer for JSON.Net?
  • Counter field in MS Access, how to generate?
  • Validaiting emails with Net.Mail MailAddress
  • Where to put my custom functions in Wordpress?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Convert array of 8 bytes to signed long in C++
  • Buffer size for converting unsigned long to string
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal