I just wanted to know is it possible to do something like x==1|2 or do I have to do x==1 | x ==2?
Sorry for the waste of time. I haven't been able to find it on google (might not be searching the right terms).
Thanks in advance :)
Printable View
I just wanted to know is it possible to do something like x==1|2 or do I have to do x==1 | x ==2?
Sorry for the waste of time. I haven't been able to find it on google (might not be searching the right terms).
Thanks in advance :)
Are you trying to make x an array?
wow, I didn't even explain it properly xD
I mean't in an if statement. if (x == 1|2) {}
Thanks again :P
Are you asking how to test if x is equal to 1/2?
What does the term: 1|2 mean?
The | operator is the bitwise OR. 1 ORed with 2 is 3. Try this:
System.out.println(3 == (1|2)); // this expression is true
| means or. So I want to know if it's possible to check if x == 1|2 (x is equal to 1 or 2) without having to use 2 statements if (x==1 | x==2) {}
Oh! ok. As far as i know, the correct if statement would be :
if ((x == 1)|(x==2)) {}
I'm guessing there's no way to make it shorter then? :(
Unfortunately, i don\'t think there is =(
'|' is a bitwise or operator - are you looking to do math, or to check equality? If the latter, use '||'. You could write this another way:
Code :
if ( 1 <= x && <= 2 ){}
Looking for shorthand ways of doing things could at times be considered bad practice, as it can obfuscate code and make it very unreadable - both by someone else and by yourself some time down the line when you try and figure out what the heck you were trying to do by looking at your own code.