Skip to main content
Advertisement

3.6 Other Operators

Beyond the fundamental arithmetic, comparative, and logical operators learned previously, it is highly crucial to understand the heavily utilized Ternary Operator and the extremely fast Bitwise Operators that require basic computer science binary knowledge.

1. Ternary Operator

The Ternary Operator is the only built-in operator in Java that mandates exactly three operands. It possesses the incredible advantage of condensing multiple if-else lines into a concise single line.

// Syntax: (Condition) ? (Value If True) : (Value If False)

int score = 85;
String result = (score >= 80) ? "Pass" : "Fail";
System.out.println(result); // Outputs: Pass

Because a bulky if-else block can be successfully condensed into a single line during variable assignment, this structural mechanism is highly utilized in professional environments to maximize code readability.

2. Bitwise Operators

Bitwise operators deliberately process operands not as decimals, but strictly down to the core binary level (0s and 1s). They are incredibly fast and memory-efficient, mainly utilized in low-level system programming, cryptographic encoding, or network protocols.

OperatorSymbolDescription
AND&Outputs 1 only if both bits are 1
OR|Outputs 1 if at least one bit is 1
XOR^Outputs 1 only if the bits are different
NOT~Inverts all bits (0 to 1, 1 to 0)
int a = 5;  // Native binary 0101
int b = 3; // Native binary 0011

System.out.println(a & b); // 0001 (Decimal: 1)
System.out.println(a | b); // 0111 (Decimal: 7)
System.out.println(a ^ b); // 0110 (Decimal: 6)

3. Shift Operators

Shift operators physically push bits to the left or right by a specified number of times. Due to processor architecture, shifting is the fastest way to multiply or divide by 2.

  • << (Left Shift): Moves bits to the left, filling empty spaces with 0. (Equivalent to multiplying by 2^n)
  • >> (Right Shift): Moves bits to the right, preserving the sign bit. (Equivalent to dividing by 2^n)
  • >>> (Unsigned Right Shift): Moves bits to the right, filling empty spaces with 0 regardless of the sign.
Advertisement