wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bitwise Operators -vs- Logical Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bitwise Operators -vs- Logical Operators" Watch "Bitwise Operators -vs- Logical Operators" New topic
Author

Bitwise Operators -vs- Logical Operators

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
In Dan's mock Topic exam (Operators)
-------------------------------------------------
class B {
static boolean m(boolean b) {
System.out.print(b + " ");
return b;
}
public static void main(String[] args) {
boolean a = false;
boolean b = false;
boolean c = true;
boolean d = false;
m(m(a | b == c & d) == m((a | b) == (c & d)));
}
}
--------------------------------------------------
The answer is "false, true, false".
I was confused with the last line code. If I changed "&" with "&&", "|" with "||", the result is the same. Does this mean that Bitwise operators are equal to logical operators when they were applied on boolean types?
In Java Specification, "Bitwise Operators are to manipulate individual bits in an integral primitive data type". How to represent data of boolean type into bits?
TIA.
-Yan


SCJP 1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Yan,
You have applied the syntax and rules of the C programming language to Java. In reality, the rules are not the same. For example, && and || are called logical operators in C; however, in Java they are called the conditional-and operator and the conditional-or operator.
Please see
Section 15.23 of the Java Language Specification.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865

In Java Specification, "Bitwise Operators are to manipulate individual bits in an integral primitive data type". How to represent data of boolean type into bits?

The Java programming language does not allow booleans to be converted to other data types. It is not possible to view the bit representation of a boolean. As far as the programmer is concerned, a boolean just has two values: true and false.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Thanks, Dan. I got it.
This is what I lost and confused.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bitwise Operators -vs- Logical Operators
 
Similar Threads
& and &&
Bitwise & and | Siera Bates
short-circuit logical operator mystery
Jxam
Q on Operators in Dan Chisholm's site