| 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.
|
 |
 |
|
|
subject: Bitwise Operators -vs- Logical Operators
|
|
|