boolean bitwise problem

G

gk

code 1
======
boolean b1=true;
boolean b2=true;
System.out.println(b1|b2);

what does bitwise operator do for boolean variables...?How it works?





N.B

Lets look the following piece of code below...

byte b1=3; //011
byte b2=2; //010
System.out.println(b1|b2); //011 == 3

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
please tell how it works?
 
P

Pawel Szulc

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?
 
L

lborupj

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:
 
P

Patricia Shanahan

gk said:
code 1
======
boolean b1=true;
boolean b2=true;
System.out.println(b1|b2);

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
 
L

Lars Enderin

Patricia Shanahan skrev:
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.
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.
 
P

Patricia Shanahan

Lars said:
Patricia Shanahan skrev:
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.

All true, but why drag in "||" when the question was only about "|"?

Patricia
 
T

Tor Iver Wilhelmsen

Patricia Shanahan said:
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.
 
C

Chris Uppal

Patricia said:
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
 
G

gk

Patricia said:
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


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 ?
 
P

Patricia Shanahan

gk said:
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 ?

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
 
C

Chris Smith

Chris Uppal said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top