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, and is a standard library member function—a regular identifier marked with the infix modifier—that performs a bitwise AND operation on specific integer types (Int, Long, UInt, ULong, UByte, UShort), as well as a non-short-circuiting logical AND operation on Boolean types. For integers, it evaluates the binary representations of both operands, returning a value where each bit is 1 strictly if the corresponding bits in both operands are 1. For booleans, it evaluates both operands strictly, returning true only if both operands evaluate to true. Unlike languages such as Java or C++ that use the & symbol for these operations, Kotlin implements them via named functions. Because and is not a hardcoded language keyword, it can even be used as a variable name without escaping, though it is primarily recognized as the standard bitwise/logical function.

Syntax

Because and is an infix function, it can be invoked using either standard dot-notation or infix notation:
// Infix notation
val result = operand1 and operand2

// Standard function call notation
val result = operand1.and(operand2)

Standard Library Signatures

The and function is explicitly defined in the Kotlin Standard Library as a member function of specific primitive classes. The return type always matches the type of the operands.
class Int {
    public infix fun and(other: Int): Int
}

class Long {
    public infix fun and(other: Long): Long
}

class UInt {
    public infix fun and(other: UInt): UInt
}

class ULong {
    public infix fun and(other: ULong): ULong
}

class UByte {
    public infix fun and(other: UByte): UByte
}

class UShort {
    public infix fun and(other: UShort): UShort
}

class Boolean {
    public infix fun and(other: Boolean): Boolean
}
Note: Kotlin does not define bitwise operations directly on signed 8-bit (Byte) or 16-bit (Short) integers. To perform a bitwise AND on these signed types, they must be explicitly promoted to Int first (e.g., byteVal.toInt() and 0xFF). Unsigned variants (UByte and UShort), however, fully support the and function natively.

Technical Mechanics: Integers

For integer types, the operation compares the operands bit-by-bit based on the following truth table:
  • 1 and 1 = 1
  • 1 and 0 = 0
  • 0 and 1 = 0
  • 0 and 0 = 0
Binary Evaluation Example: Evaluating 12 and 25 (showing the lowest 8 bits of the 32-bit Int for brevity):
0000 1100  (12 in binary)
0001 1001  (25 in binary)

0000 1000  (Result: 8 in decimal)

Technical Mechanics: Booleans and Distinction from &&

When applied to Boolean types, and functions as a logical AND, but it differs fundamentally from the standard && operator in its evaluation strategy:
  • and (Non-short-circuiting): Evaluates both the left-hand and right-hand operands regardless of the left-hand operand’s value.
  • && (Short-circuiting): Evaluates the right-hand operand only if the left-hand operand evaluates to true. If the left-hand operand is false, the expression immediately returns false without evaluating the right side.
// Bitwise operation on Int
val bitwiseResult: Int = 5 and 3 

// Non-short-circuiting logical operation on Boolean
// Both sides are evaluated even if the left side is false
val booleanResult: Boolean = false and true 

// Short-circuiting logical operation on Boolean
// The right side is skipped because the left side is false
val logicalResult: Boolean = false && true
Master Kotlin with Deep Grasping Methodology!Learn More