Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

boolean bitwise problem

Thread view: 
gk - 04 Nov 2006 13:24 GMT
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?
Pawel Szulc - 04 Nov 2006 14:54 GMT
> 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?
lborupj@gmail.com - 04 Nov 2006 15:18 GMT
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?
Patricia Shanahan - 04 Nov 2006 15:52 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.