Pipe (|) operator in Java

JavaOperators

Java Problem Overview


I've got this statement in Java:

System.out.println(3|4); 

Why is the output 7?

Java Solutions


Solution 1 - Java

It's a bitwise OR operation. It's modifying things at a binary level.

             011                     3
in binary: | 100     in decimal:  |  4
             ___                   ___
             111                     7

Open Windows calc using scientific mode. You can flip between decimal and binary (and hex) and perform bitwise operations including or, and, xor, etc.

To do a bitwise or in your head or on paper, compare each digit of the same ordinal. If either number is a 1, the result at that ordinal will be 1.

Solution 2 - Java

The operator | does a "bitwise OR". The output of bitwise OR on two bits is 1 if either bit is 1 or 0 if both bits are 0. Bitwise OR on two numbers just does a bitwise OR on each bit individually.

Heres how 3|4 works:

  3:  00000011
  4:  00000100
--------------
3|4:  00000111 = 7

Solution 3 - Java

It's doing a bitwise OR operation, and 3 OR 4 is 7.

See here: http://en.wikipedia.org/wiki/Bitwise_OR#OR

Solution 4 - Java

Binary representation:

 3 = 00000011
 4 = 00000100

| is bitwise OR operator

when you OR two numbers, you take the binary representation and the OR result is 1 IFF for that column at least one column is set true (1)

So

00000011
00000100
--------
00000111

then, columns tell you the value at that position:

128, 64, 32, 16, 8, 4, 2, 1

so

128, 64, 32, 16, 8, 4, 2, 1
 0 ,  0,  0,  0, 0, 1, 1, 1  

any column with a 1 means you add that column's value:

4 + 2 + 1 = 7

Solution 5 - Java

| is the "bitwise or" operator. in a|b, if nth bit of a and/or b is 1, the nth bit of the result will be 1. 3 is 11 in binary. 4 is 100 in binary.

0  1  1
or or or
1  0  0
=  =  =
1  1  1

And 111 happens to be the binary representation of 7.

Solution 6 - Java

It's useful to realize there is a generalized system for counting underlying this. Binary is base-2. Familiar decimal is base-10. Linux permission octal is base 8.

A number's value is obtained by adding together the individual values of each of its digits. For any digit, the value is derived from a simple formula.

(digit) * (base) ^ (number of places to the left of the decimal point)

123 = one hundred and twenty three = (1 * 10^2) + (2 * 10^1) + (3 * 10^0) = 100 + 20 + 3

I learned that in CS211 (not bragging, just remembering)

Solution 7 - Java

As bitwise operators can be a little confusing without something to correlate them to, the way I've explained their function to non-programmers even is that you simply subtitute 1 for true and 0 for false, and then they behave identically to the operators in the english language:

the moon is blue AND the sky is blue, is false

0 and 1 is 0

the moon is blue OR the sky is blue, is true

0 or 1 is 1

but the analogy breaks down when I get to:

the ocean is blue XOR the trees are green, is false

Solution 8 - Java

Since question asks what is pipe operator (|) in Java, without specifying anything particularly about "OR" logic, it may be useful to note, that this operator is so to say - redefined, when we deal with exceptions.

Since Java SE 7, catching multiple, disjointtypes having no inheritance relation exceptions in one catch block, also involves | operator, which, in this case, serves as just piping/chaining logic, and has nothing to do with "OR", because in case of "OR", disjoint would have been allowed.

If, before Java 7, whenever you needed to catch multiple exceptions, you needed to write separate catch blocks, since Java 7, you can do:

try {
   ...
} catch (FileNotFoundException | SQLException e) {
   ...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbenthamView Question on Stackoverflow
Solution 1 - JavaJonathon FaustView Answer on Stackoverflow
Solution 2 - JavaMike DanielsView Answer on Stackoverflow
Solution 3 - JavadcpView Answer on Stackoverflow
Solution 4 - JavadavbrynView Answer on Stackoverflow
Solution 5 - Javauser395760View Answer on Stackoverflow
Solution 6 - JavaAaron AnodideView Answer on Stackoverflow
Solution 7 - JavaJimmy HoffaView Answer on Stackoverflow
Solution 8 - JavaGiorgi TsiklauriView Answer on Stackoverflow