# Bitwise & and | Siera Bates

Erez Pitchon

Greenhorn

Posts: 13

posted 7 years ago

Hi

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."

http://issuu.com/andhymao/docs/scjp_chapter_4/24

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?

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."

http://issuu.com/andhymao/docs/scjp_chapter_4/24

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?

posted 7 years ago

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

Alejandro Galvan

Ranch Hand

Posts: 48

posted 7 years ago

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.

Regards,

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.

Regards,

SCJP 5.0<br />Preparing SCWCD

Alejandro Galvan

Ranch Hand

Posts: 48

It is sorta covered in the JavaRanch Style Guide. |