File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bitwise & and |  Siera Bates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bitwise & and |  Siera Bates" Watch "Bitwise & and |  Siera Bates" New topic

Bitwise & and | Siera Bates

Erez Pitchon

Joined: Dec 19, 2008
Posts: 13

Re: Java 5 Cert.

I'm looking at page 298 in this guide and the following statement makes no sense to me:

"The logical operators (&& , ||, &, |, and ^) can be used only to evaluate two boolean expressions."

But, & and | are bitwise operators and can be used on integers for instance. Moreover, in this chapter, the book states that bitwise operators are NOT ON THE EXAM (pg 293)
page 293

then goes on to say that it is all over the exam (top of pg 297)
Page 297.

So which is it?
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Erez | and & can be used on booleans and integers. using | and & with booleans is on the exam. But using them with integers is not on the exam...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Just to clarify, it is also allowed with long's.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Alejandro Galvan
Ranch Hand

Joined: Jan 02, 2008
Posts: 48
Hi Erez,

I would like to add something to the Ankit's comment. The &&, ||, & and | can be used to compare logical expressions that result in a boolean, also the &, | and ^ can be used as a bitwise operator. To be a bit more clear, here is the explain:

- && and || are also known as short circuit logic comparators
- & and | are also known (used in a logical expression) as log circuit comparators.

This means you can use any of those to compare expressions that results in a boolean, like a == b or "".equals(someString), the difference between the use of the short and long circuits stands on the comparisions thos made, I mean, if you have
false && someBooleanExpression
which ever expression you have on the right side it will not be evaluated, this is cause with an AND logical operation, if only one of the operands is false, all the expression is false, the AND operation needs all the operands in true to the expression be true, so, the short circuit evaluates the rigth side only if it is possible to have a true expression result (this possibility is only if you have true on the left side), son in this example no matters what is the result on the rigth side, all the expression will be false since you have false in one side, but in case you type:
false & someBooleanExpression, the rigth side will be always evaluated

The same is with the ||, the OR logical comparission always need both side in false to the expression be false, if one of the operands is true, all the expression will be true, so the || just evaluate the rigth side if the left side is false, if the left side is true, it doesn't care of the rigth side, however the expression will be true. In the same way, the | operator always evaluate both sides.

Why you need to know this, cause, if in any situation you use short or long circuit operators it can change your result, in example:

The bitwise operator works in bit level of the operands, I mean the and operation will make a logical AND (&) between the bits of the operands, in example, the value 1 has the bits (just show 4 for this example): 0001, the 5 value has the bits: 0101, if you make and AND between these numbers (1 and 5) in bit level, you'll have 0001, the other bitwise operators, both OR (|) and XOR(^) works in the same way.

Hopefully my explanaition is not hard and helps you.


SCJP 5.0<br />Preparing SCWCD
Alejandro Galvan
Ranch Hand

Joined: Jan 02, 2008
Posts: 48
I forgot to tell you about the ^ logical operation, it is a long circuit because it always need to evaluate both sides, if both sides are equals, the result is false, if they are differents, the result is true, so:

I agree. Here's the link:
subject: Bitwise & and | Siera Bates
It's not a secret anymore!