bentham bentham - 1 year ago 108
Java Question

Pipe (|) operator in Java

I've got this statement in Java:


Why is the output 7?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download