> ## 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.

# Kotlin Long

`Long` is a built-in numeric data type in Kotlin representing a 64-bit signed two's complement integer. It accommodates values ranging from -9,223,372,036,854,775,808 (-2<sup>63</sup>) to 9,223,372,036,854,775,807 (2<sup>63</sup> - 1).

## Instantiation and Syntax

When assigning an integer literal to a variable explicitly typed as `Long`, the compiler automatically instantiates the literal as a `Long`. If the variable's type is not explicitly declared, appending an uppercase `L` suffix forces the compiler to infer the type as `Long`. Additionally, any integer literal exceeding the maximum value of a 32-bit `Int` is automatically inferred as a `Long`. Kotlin strictly rejects the lowercase `l` suffix to prevent visual confusion with the digit `1`.

```kotlin theme={"dark"}
val explicitLong: Long = 42    // Automatically instantiated as Long due to explicit type
val inferredLong = 42L         // 'L' suffix forces Long inference
val largeInferred = 3000000000 // Automatically inferred as Long because it exceeds Int.MAX_VALUE

// Supported literal formats
val decLong = 1_000_000_000_000L // Underscores permitted for readability
val hexLong = 0x0DE0B6B3A7640000L
val binLong = 0b110100101011L
// Note: Octal literals are not supported in Kotlin
```

## Memory Representation and Boxing

When targeting the JVM, Kotlin optimizes memory by compiling a non-nullable `Long` directly to the Java primitive `long`.

However, if the variable is declared as nullable (`Long?`) or used in a generic context (e.g., `List<Long>`), the compiler boxes the value into a `java.lang.Long` object. This boxing process allocates memory on the heap and introduces object overhead.

```kotlin theme={"dark"}
val primitive: Long = 100L    // Compiles to primitive 'long'
val boxed: Long? = 100L       // Compiles to 'java.lang.Long'
```

## Type Conversion

While Kotlin automatically adapts integer *literals* to an explicitly declared `Long` type, it enforces strict type safety for *variables* and does not support implicit widening conversions. You cannot directly assign an existing `Int`, `Short`, or `Byte` variable to a `Long` variable. Explicit conversion using the `toLong()` function is mandatory.

```kotlin theme={"dark"}
val intValue: Int = 1024
// val invalidLong: Long = intValue // Compilation error: Type mismatch
val validLong: Long = intValue.toLong()
```

Conversely, converting a `Long` to a smaller numeric type (like `Int`) requires `toInt()`. If the `Long` value exceeds the target type's maximum capacity, the resulting value is truncated to the least significant bits.

## Bitwise Operations

Kotlin does not use standard C-style bitwise operators (e.g., `<<`, `|`, `&`). Instead, `Long` supports bitwise manipulation through named infix functions and standard functions. These operate directly on the 64-bit binary representation.

```kotlin theme={"dark"}
val base: Long = 0b0010L

// Infix functions
val shiftedLeft = base shl 2      // Shift left (0b1000L)
val shiftedRight = base shr 1     // Signed shift right (0b0001L)
val unsignedShift = base ushr 1   // Unsigned shift right (zero-fills MSB)

val bitwiseAnd = base and 0b0011L // Bitwise AND
val bitwiseOr = base or 0b0100L   // Bitwise OR
val bitwiseXor = base xor 0b1111L // Bitwise XOR

// Standard function (takes no arguments, cannot be called as infix)
val inverted = base.inv()         // Bitwise inversion (flips all 64 bits)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
