tags:

views:

348

answers:

7
+7  Q: 

| operator in Java

I've got this statement in Java:

System.out.println(3|4); 

Why is the output 7? I can't understand, can someone help me?

+8  A: 

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

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

dcp
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
-1: The purpose of SO is to create the definitive answer. This doesn't explain why `3|4 == 7`
Tim Bender
thanks for answering
GeorgeBecj
Jonik
@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
@dcp: Don't mind him. Tim is my snarky bastard coworker. :)
Mike Daniels
@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
@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
@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.stackoverflow.com/questions/8724/how-to-deal-with-google-questions
Tim Bender
@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
+20  A: 

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
thanks for asnwering
GeorgeBecj
+16  A: 

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
thanks for answering
GeorgeBecj
+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
+1. Better than Jonathon's highest-voted answer because this gives the example *and* briefly explains how bitwise OR works (and indeed mentions the operator's name).
Jonik
+3  A: 

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

delnan
thanks for answering
GeorgeBecj
+4  A: 

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
+1  A: 

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
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
+1  A: 

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)

Gabriel
i'm new here - is it downgraded for not being the answer to the question or the personal commentary?
Gabriel
@Gabriel - downvotes occur for any reason or no reason. Do not take it personally! Your downvote is probably from not answering the question, but only the voter really knows why. Sometimes you'll get downvoted because the voter is trying to increase another answer's relative rank, so it's no reflection on your answer other than you weren't the voter's favorite. Have a +1!
Jonathon