Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bitwise Operators -vs- Logical Operators

 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Dan. I got it.
This is what I lost and confused.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic