Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help required

 
VINCE CARTER
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following will not compile without error?
A:
boolean b1=true;
boolean b2=true;
System.out.println(b1|b2);
B:
boolean b1=true;
boolean b2=true;
System.out.println(b1||b2);
Why the result is B not A ??
Thanks!!!
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b1|b2 = bitwise manipulation
b1||b2 = boolean logic read b1 or b2.

Let's use a simple example for bitwise manipulation

int a = 1, b = 2, c;

c = a | b;

binary value a 0001 = 1
binary value b 0010 = 2
---------------------------
binary value c 0011 = 3

Rules for bit wise or are if on of the bits is 1, the result is 1.

Hope this helps.
-C
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of these examples compile, and when run result in true.

The one involving the operator is the "non-shortcutting" logical OR operator when its operands are boolean. (It is also a bitwise or operator when its operands are integer types).

The second operator || is the "shortcutting" logical OR operator which takes only boolean operands.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic