This site requires JavaScript, please enable it in your browser!
Greenfoot back
MakerOfGames
MakerOfGames wrote ...

2012/5/1

What is the symbol for OR?

MakerOfGames MakerOfGames

2012/5/1

#
I need it for my code: if(i=3 OR 2) { do something }
danpost danpost

2012/5/1

#
There are two such operators: | Bitwise inclusive OR ^ Bitwise exclusive OR The one that results in 3 for '3 OR 2' is the inclusive OR ( | ). If you had taken my advice previously, when I said "Check out the java tutorials", you would have known where to find the answer.
rick rick

2012/5/1

#
I think bitwise OR is not what you want, since 2 | 3 equals 2. As a binary number 2 decimal is 10 binary, and 3 decimal is 11. Bitwise Or is: for each position, if one of the bits of the operands at that position is 1, then the corresponding resulting bit equals 1, otherwise 0. Thus the condition results in (i = 2). But note that moreover the if-condition is possibly wrong because (i=2|3) is an assignment, resulting in i becoming 2. As I see it, what you really want is: if i equals 2 or i equals 3: (i==2 || i==3). The operator || is logical OR...
danpost danpost

2012/5/1

#
@rick, you are correct, in that since it was not explicitly stated as 'if (i=3 OR 2)', that there is a possibility he meant it as a logical OR; but, as he had capitalized 'OR', not only in the title of the discussion, but in the pseudo-code itself, it pointed more towards the operational OR, implicitly (though, I did mention the Java tutorials to him, again, where he could find the correct information, if my response was indeed misguided). BTW, carefully calculate, as you stated above, 2 | 3 and see if your result is actually 2 (since both bits are 1s in binary for 3, then the result will have both bits as 1s; hence, 3).
davmac davmac

2012/5/1

#
MakerOfGames, I'm worried the above discussion might be a bit complex, so, to summarise: use 'if (i == 3 || i == 2) { ... }'. This translates to 'if i is 3 or i is 2' which seems like it is what you probably wanted.
rick rick

2012/5/2

#
danpost wrote...
@rick, BTW, carefully calculate, as you stated above, 2 | 3 and see if your result is actually 2 (since both bits are 1s in binary for 3, then the result will have both bits as 1s; hence, 3).
Thanks, you're certainly right :-)
You need to login to post a reply.