• Post Reply Bookmark Topic Watch Topic
  • New Topic

& and &&  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between those two operators?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Conrado.

The difference is that && is a logical short-circuit operator, and & can be used as either a binary operator or a logical non-short-circuit operator.
In this code, & is used as the binary AND operator. The value of c will be 7.

A short-circuit operator will not even evaluate the right hand side if it can determine its outcome from just evaluating the left hand side. A non-short-circuit operator will always evaluate both sides.
The above could should print

Short-circuit: false
Non-short-circuit: false true


It does this because && determines that it doesn't need to evaluate printAndReturn(true), because no matter what value it evaluates to, the result of the operation will never change.
On the other hand, & always evaluates the right hand side.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The single keystroke & operator is a bitwise operator which has been overloaded to operate on booleans too.
You can read about bitwise operators here (mostly shift, and I think there is an error later on about -16>>>3) and here. Note the Java Tutorials links. I found little else about bitwise operators and there are 7 of them:
  • 1: ~ the bitwise one's complement operator. Higher precedence than any of the arithmetic operators.
  • 2 3 4: << >> >>> the left‑shift, ordinary (=signed) right‑shift and unsigned right‑shift operators. Lower precedence than + or -
  • 5: & the bitwise AND operator. Precedence 5 6 7 in that order, below shift
  • 6: ^ the bitwise exclusive or (=XOR) operator
  • 7: | the bitwise inclusive or (=OR) operator
  • ~ converts all the 1 bits in the operand to 0 and vice versa. There is an easy explanation in the Java Language Specification (=JLS).
    You can read about & ^ | in the JLS too. If you scroll down it tells you the difference between && and & (|| and | are similar).

    ~1010_0110_1001_0011 is 0101_1001_0110_1100

    AND tests whether the bits in the same position in both numbers are 1. Using the same values as in the JLS link, 0xff00 = 1111_1111_0000_0000 and 0xf0f0 = 1111_0000_1111_0000:
    1111_1111_0000_0000
    1111_0000_1111_0000 &
    1111_0000_0000_0000

    XOR tests whether the bits in the same position in both numbers are different.
    1111_1111_0000_0000
    1111_0000_1111_0000 ^
    0000_1111_1111_0000

    OR tests whether the bits in the same position in either number is 1.
    1111_1111_0000_0000
    1111_0000_1111_0000 |
    1111_1111_1111_0000

    They have different uses. You can set a mask with ORYou can read about using & instead of % in this thread about HashMap, where it is used frequently.

    If you use ^ twice you can get the original value back.… and you can swap two numbers with ^ thrice but that is only a party trick not to be used in real life.

    You would notice not difference in output if you use && or ||


     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!