Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In Kotlin, the “or” operation is implemented via two distinct mechanisms: the or infix function, which performs bitwise operations on specific integral types and non-short-circuiting logical operations on booleans, and the || operator, which performs short-circuiting logical operations strictly on boolean expressions.

The or Infix Function

The or identifier is a standard library function declared with the infix modifier. It is not a language keyword. It is overloaded to handle specific integral types and boolean types. Bitwise Evaluation (Integral Types) When applied to signed (Int, Long) or unsigned (UInt, ULong, UShort, UByte) integral types, or performs a bitwise inclusive OR operation. It compares each bit of the left-hand operand to the corresponding bit of the right-hand operand. If either bit is 1, the resulting bit at that position is 1. If both bits are 0, the resulting bit is 0. Note on Signed Byte and Short: Kotlin does not define bitwise functions natively for signed Byte and Short types. To perform a bitwise OR on these types, they must be explicitly converted to Int first.
val a: Int = 0b0101 // Decimal: 5
val b: Int = 0b0011 // Decimal: 3

// Infix notation
val resultInfix: Int = a or b // Result: 0b0111 (Decimal: 7)

// Standard function call notation
val resultStandard: Int = a.or(b) 

// Signed Byte/Short require explicit conversion to Int
val byteA: Byte = 0b0101
val byteB: Byte = 0b0011
val byteResult: Int = byteA.toInt() or byteB.toInt()
Non-Short-Circuiting Logical Evaluation (Boolean Type) When applied to Boolean types, Boolean.or(Boolean) performs a logical inclusive OR. Unlike the || operator, the or function does not short-circuit. Because it is a standard function call, both the left-hand and right-hand operands are strictly evaluated before the function is invoked, regardless of the left-hand operand’s value.
val conditionA: Boolean = true
val conditionB: Boolean = false

// Both operands are evaluated before the 'or' function executes
val result: Boolean = conditionA or conditionB // Evaluates to true
Precedence Note: Unlike languages such as Java or C++ where bitwise AND (&) has higher precedence than bitwise OR (|), Kotlin implements these as standard infix functions. All infix functions share the exact same precedence level. Therefore, chained infix operations evaluate strictly left-to-right unless explicitly grouped with parentheses.
// Evaluates strictly left-to-right: (1 or 4) and 2
// Step 1: 1 or 4  -> 0b0001 | 0b0100 = 0b0101 (5)
// Step 2: 5 and 2 -> 0b0101 & 0b0010 = 0b0000 (0)
val chainedResult = 1 or 4 and 2 // Result: 0

// Note: If 'and' had higher precedence, this would evaluate as 1 or (4 and 2) -> 1 or 0 -> 1

The Logical || Operator

The || operator performs a logical inclusive OR operation exclusively on Boolean expressions. It evaluates to true if at least one of its operands evaluates to true. If both operands evaluate to false, the expression returns false. Short-Circuit Evaluation: The || operator is lazily evaluated. The left-hand operand is evaluated first. If it resolves to true, the overall expression is guaranteed to be true, and the right-hand operand is completely bypassed. The right-hand operand is evaluated if and only if the left-hand operand resolves to false.
fun isTrue(): Boolean = true
fun isFalse(): Boolean = false

// isFalse() is never executed because the left operand resolves to true
val result: Boolean = isTrue() || isFalse() 
Precedence Note: The logical || operator has lower precedence than the logical AND operator (&&).
// Evaluates as false || (true && false) -> false || false -> false
val logicalResult = false || true && false 
Master Kotlin with Deep Grasping Methodology!Learn More