I've got this statement in Java:
System.out.println(3|4);
Why is the output 7?
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.
|| 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.
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.
The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With