65

I've got this statement in Java:

System.out.println(3|4); 

Why is the output 7?

Khalil M
  • 1,788
  • 2
  • 22
  • 36
bentham
  • 1,701
  • 3
  • 20
  • 32
  • 2
    What did you expect? And why? – user207421 Jul 23 '10 at 10:27
  • 1
    @EJP: The purpose of operator notation in programming languages is to make it possible to use the same notation in our programs that we use in the rest of our lives. The vertical bar usually means *is divisible by*, so while I can't speak for the OP, I myself would expect the result of `3|4` to be `false` and not `7` and I suspect that pretty much everybody who took math in high school would expect the same. – Jörg W Mittag Jul 23 '10 at 13:08
  • 3
    Over here in the USA, where Java was spec'd, a vertical bar doesn't have anything to do with division. We use horizontal bars (sometimes with dots above and below) and on rarer occasions a forward slash. Never a vertical line. – Brian Knoblauch Jul 23 '10 at 18:22
  • 1
    @Brian, I was thinking that's some German idiosyncrasy, but looking closer, maybe not: http://en.wikipedia.org/wiki/Vertical_bar#Mathematics (look for "divisibility"). But it's definitely not a layman's math symbol either. :) – Jonik Jul 23 '10 at 22:11
  • 5
    The purpose of the | operator is what the language specification says it is. Not to meet your expectations. – user207421 Jul 24 '10 at 08:38

8 Answers8

103

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.

Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
46

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
Mike Daniels
  • 8,582
  • 2
  • 31
  • 44
  • 1
    +1: demonstrating `12|10` might be a more thorough example of the bitwise-or operator. Although the OP here asks specifically about `3|4` – Tim Bender Jul 22 '10 at 19:39
13

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
davbryn
  • 7,156
  • 2
  • 24
  • 47
10

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

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

dcp
  • 54,410
  • 22
  • 144
  • 164
  • 3
    Indeed. To expand on dcp's explanation slightly, in Java a single pipe, `|`, is what is called a 'Bitwise OR'. That means that it performs a low level OR on the actual bits that make up the arguments. In this case, 3 is `0011` and 4 is `0100` (least 4 significant bits shown). A Bitwise OR goes through each bit and sets it to a 1 if *either* of the bits are 1, so in this case you get `0111`, or 7. What were you actually trying to print out, or was this an example from a book/tutorial? – Stephen Jul 22 '10 at 19:31
  • 1
    -1: The purpose of SO is to create the definitive answer. This doesn't explain why `3|4 == 7` – Tim Bender Jul 22 '10 at 19:35
  • 4
    @Tim: Giving an answer that is helpful & correct but not perfect / complete is in no way against the "purpose of SO". – Jonik Jul 22 '10 at 19:41
  • 1
    @Tim Bender - I provided a link for him on how the OR operation works, which if read, would explain the operation in detail so he could understand it. Anyway, you're supposed to downvote if something is incorrect, but there's nothing incorrect there so I'm at a loss. – dcp Jul 22 '10 at 19:41
  • @dcp: What if wiki implodes under its own weight? What then? How meaningful would your answer be in ten years when there is no wikipedia? Anyway, your answer is recursive ("3|4 == 7 because 3|4 == 7"). – Tim Bender Jul 22 '10 at 19:53
  • @Tim Bender - I think you need to re-read my answer. My answer was *not* "3|4 == 7 because 3|4 == 7". My answer was 3 OR 4 is 7, and I explicitly refered to a link explaining the OR operator. Seems to me you're just trolling here. As for you're argument about the wikipedia link, that's true for any link we provide. Web sites come and go. What if MSDN goes away, are you saying we should never provide an MSDN link? – dcp Jul 22 '10 at 20:01
  • 3
    @dcp, I'm sorry you disagree. My personal opinion is that simply saying "3 OR 4 is 7" is a recursive definition. It doesn't explain at all why that is the case. If you want to provide links that expand on your answer, that is fantastic. But your answer should also be definitive and complete. Just my personal opinion. Also see: http://meta.stackexchange.com/questions/8724/how-to-deal-with-google-questions – Tim Bender Jul 22 '10 at 20:32
  • @Tim Bender - Point taken. But I definitely didn't do a "google search" to get this answer though :). Anyway, even though I disagree with your reasons for downvoting, I will take your criticism constructively and try to provide more detailed examples next time. – dcp Jul 22 '10 at 20:49
4

| 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.

4

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)

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
3

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) {
   ...
}
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
2

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

Jimmy Hoffa
  • 5,909
  • 30
  • 53
  • To explain XOR, something like "Would you like ice cream XOR cake for dessert?" might be illustrative. :) Typical case where natural language "or" actually means "either but not both". – Jonik Jul 23 '10 at 22:23