• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bitwise Operators -vs- Logical Operators

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Dan. I got it.
This is what I lost and confused.
 
reply
    Bookmark Topic Watch Topic
  • New Topic