Logical and bitwise operations and operators in Java

Logical operations and operators

  • & : Logical AND;
  • && : Shorthand AND;
  • | : Logical OR;
  • || : Shorthand OR;
  • ^ : Logical XOR (exclusive OR);
  • ! : Logical unary NOT;
  • &= : AND with assignment;
  • |= : OR with assignment;
  • ^= : XOR with assignment;
  • == : Equal;
  • != : Not equal;
  • ?: : Ternary conditional operator.

What is the ternary selection operator

Ternary conditional operator ?: is an operator that can replace some constructions of if-then-else statements.

The expression is written in the following form:

condition ? expression1 : expression2

If the condition is met, then expression1 is evaluated and its result becomes the result of the entire statement. If the condition is false, then expression2 is evaluated and its value becomes the result of the operator's operation. Both expression1 and expression2 must return the same (or compatible) type.

Bitwise operations

  • ~ : Bitwise unary NOT operator;
  • & : Bitwise AND;
  • &= : Bitwise AND assignment;
  • | : Bitwise OR;
  • |= : Bitwise OR with assignment;
  • ^ : Bitwise exclusive XOR;
  • ^= : Bitwise exclusive XOR with assignment;
  • >> : Shift to the right (division by 2 in the degree of shift);
  • >>= : Shift right with assignment;
  • >>> : Shift to the right without taking into account the sign;
  • >>>= : Shift to the right ignore the sign with assignment;
  • << : Shift left (multiply by 2 in the degree of shift);
  • <<= : Left shift with assignment.

Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed