# nooby question

• September 18th, 2011, 06:38 PM
boogerface
nooby question
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).

• September 18th, 2011, 06:42 PM
Bertorox
Re: nooby question
Are you trying to make x an array?
• September 18th, 2011, 06:47 PM
boogerface
Re: nooby question
wow, I didn't even explain it properly xD

I mean't in an if statement. if (x == 1|2) {}

Thanks again :P
• September 18th, 2011, 08:38 PM
Norm
Re: nooby question
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
• September 18th, 2011, 09:12 PM
boogerface
Re: nooby question
| 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) {}
• September 18th, 2011, 09:22 PM
Bertorox
Re: nooby question
Oh! ok. As far as i know, the correct if statement would be :

if ((x == 1)|(x==2)) {}
• September 18th, 2011, 09:33 PM
boogerface
Re: nooby question
I'm guessing there's no way to make it shorter then? :(
• September 18th, 2011, 09:54 PM
Bertorox
Re: nooby question
Unfortunately, i don\'t think there is =(
• September 19th, 2011, 09:12 AM
copeg
Re: nooby question
Quote:

Originally Posted by Bertorox
Oh! ok. As far as i know, the correct if statement would be :

if ((x == 1)|(x==2)) {}

'|' 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.