Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe (|) operator in Java

Tags:

java

operators

I've got this statement in Java:

System.out.println(3|4);  

Why is the output 7?

like image 869
bentham Avatar asked Jul 22 '10 19:07

bentham


People also ask

What does this mean |=?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

Does || mean OR in Java?

|| operator in Java|| is a type of Logical Operator and is read as “OR OR” or “Logical OR“. This operator is used to perform “logical OR” operation, i.e. the function similar to OR gate in digital electronics.

What does |= in Java mean?

operator. ~ is bitwise complement bits, 0 to 1 and 1 to 0 (Unary operator) but ~= not an operator. Additionally, ! Called Logical NOT Operator, but != Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

What do && mean in Java?

The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.


1 Answers

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.

like image 60
Jonathon Faust Avatar answered Oct 05 '22 10:10

Jonathon Faust