18

I've seen the pipe character used in method calls in Java programs.

For example:

public class Testing1 {

    public int print(int i1, int i2){
        return i1 + i2; 
    }
    public static void main(String[] args){
        Testing1 t1 = new Testing1();
        int t3 = t1.print(4, 3 | 2);
        System.out.println(t3);
    }
}

When I run this, I simply get 7.

Can someone explain what the pipe does in the method call and how to use it properly?

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363

3 Answers3

22

The pipe in 3 | 2 is the bitwise inclusive OR operator, which returns 3 in your case (11 | 10 == 11 in binary).

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 6
    No it performs a bitwise OR, by retaining all bits set to 1 in either of the operands. For example, `2 | 1` => `10 | 01` => `11` => `3` - another one: `6 | 5` => `110 | 101` => `111` => `7` etc... – assylias May 08 '13 at 14:23
  • 1
    Can you explain one way this would be useful? – CodyBugstein May 08 '13 at 14:23
  • 1
    For example to pass several options: `int option1 = 1, option2 = 2, option3 = 4, option4 = 8` (so in binary: 1, 10, 100, 1000). You can then do something like: `method(option1|option2|option3)` to say you want all those options used. In method, you can do `argument & 1` to see if option1 was selected. Now it's not something that you'd use everyday but it can be useful in some specific situations. – assylias May 08 '13 at 14:28
  • 1
    @Imray binary masking (via bitwise operations) is a very used technique to set/unset certain fields in integers etc. Very often seen in electronic engineering :) If this sounds new to you, try googling bitwise operations – Morten Jensen May 08 '13 at 14:38
  • While this post answers OP question it would be nice to add informations about `|` for boolean arguments and how is it different than `||`. This way we could use this question as target of more duplicates. – Pshemo Sep 05 '14 at 15:18
7

it's a bitwise OR.

The bitwise representation of numbers is like this:

|2^2|2^1|2^0|
| 4 | 2 | 1 |
  • the bitwise representation of 3 is:
|2^2|2^1|2^0|
| 4 | 2 | 1 |
| - | X | X |  => 3
  • the bitwise representation of 2 is:
|2^2|2^1|2^0|
| 4 | 2 | 1 |
| - | X | - | => 2

The bitwise OR will return 3 because when using OR at least one bit must be "occupied". Since the first and second bit are occupied (3 | 2) will return 3.

Finally, the addition 4 + 3 = 7.

KGolbang
  • 445
  • 1
  • 5
  • 15
5

The | operator performs a bitwise OR on the operands:

3 | 2 --->    0011 (3 in binary)
           OR 0010 (2 in binary)
          ---------
              0011 (3 in binary)

Here's the pattern:

0 OR 0: 0
0 OR 1: 1
1 OR 0: 1
1 OR 1: 1

Using |:

if(someCondition | anotherCondition)
{
    /* this will execute as long as at least one
       condition is true */
}

Note that this is similar to the short-circuit OR (||) commonly used in if statements:

if(someCondition || anotherCondition)
{
    /* this will also execute as long as at least one
       condition is true */
}

(except that || does not enforce the need to continue checking other conditions once a true expression has been found.)

pcnThird
  • 2,362
  • 2
  • 19
  • 33