> so this is basically doing bitwise OR operation,but i am in confusion
> how it will calculate the similar for boolean type in (code 1),will u
have u tried to actually run it?
Much like you would except:
b1 = true; b2=false; result= true;
b1 = false; b2=true; result= true;
b1 = true; b2=true; result= true;
b1 = false; b2=false; result= false;
simple OR
regards, Lars Borup Jensen
http://www.it-arbejde.dk
gk skrev:
> code 1
> ======
[quoted text clipped - 15 lines]
> how it will calculate the similar for boolean type in (code 1),will u
> please tell how it works?
> code 1
> ======
[quoted text clipped - 3 lines]
>
> what does bitwise operator do for boolean variables...?How it works?
There is no bitwise | for boolean variables. The symbol "|" is
overloaded, representing two different operations:
1. If its operands are integers, it represents their bitwise or.
2. If its operands are booleans, it represents their logical or, true
if, and only if, at least one of b1 or b2 is true.
Patricia
Lars Enderin - 04 Nov 2006 16:31 GMT
Patricia Shanahan skrev:
>> code 1
>> ======
[quoted text clipped - 11 lines]
> 2. If its operands are booleans, it represents their logical or, true
> if, and only if, at least one of b1 or b2 is true.
The difference between || and | is that both operands are evaluated in
the single | case, but if the first operand (a) in (a || b) is true, the
second operand is not evaluated, which may avoid null references and
other side-effects.
Patricia Shanahan - 04 Nov 2006 16:50 GMT
> Patricia Shanahan skrev:
>>> code 1
[quoted text clipped - 17 lines]
> second operand is not evaluated, which may avoid null references and
> other side-effects.
All true, but why drag in "||" when the question was only about "|"?
Patricia
Chris Uppal - 04 Nov 2006 17:47 GMT
> All true, but why drag in "||" when the question was only about "|"?
Because without that difference in semantics between
boolean | boolean
and
boolean || boolean
there is no reason to have a specially overloaded meaning of | for booleans at
all.
-- chris
Chris Smith - 17 Nov 2006 04:44 GMT
> > All true, but why drag in "||" when the question was only about "|"?
>
[quoted text clipped - 4 lines]
> there is no reason to have a specially overloaded meaning of | for booleans at
> all.
And because a lot of poor quality books and courseware, busy or
misinformed teachers, etc. continue to go about telling those who try to
learn Java that || is for booleans and | is for bitwise/integer
operations. Explicit misinformation sometimes needs to be corrected,
rather than merely ignored.

Signature
Chris Smith
Tor Iver Wilhelmsen - 04 Nov 2006 16:35 GMT
> 2. If its operands are booleans, it represents their logical or, true
> if, and only if, at least one of b1 or b2 is true.
In particular, the difference from || is that || "short-circuits"
while | doesn't (it evaluates both boolean expressions).
So:
"true||isFoo()" will NOT call isFoo(), while "true|isFoo()" will.
Same holds for & versus && of course.
gk - 04 Nov 2006 20:06 GMT
> > code 1
> > ======
[quoted text clipped - 13 lines]
>
> Patricia
i liked these statements ...point 1 is ok and thats why i gave an byte
example for that ....but where from you get the point 2 ? is it in JLS
? your point 2 is probabily a valid explanation...and i guess that from
the output .....but i dont see any references anywhere which states
something like that ......have you found it anywhere ?
Patricia Shanahan - 04 Nov 2006 21:47 GMT
>>> code 1
>>> ======
[quoted text clipped - 19 lines]
> the output .....but i dont see any references anywhere which states
> something like that ......have you found it anywhere ?
See the JLS, "15.22.2 Boolean Logical Operators &, ^, and |"
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5242,
"For |, the result value is false if both operand values are false;
otherwise, the result is true."
Patricia