CidrNumber

Number with a byte layout and semantics optimized for CIDR/IP operations. The maximum number of addresses inside an IP address space is 2^32 for IPv4 and 2^128 for IPv6. Hence, 32/128-bit unsigned integers are not enough to represent the size of /0 networks, and an additional bit is needed. At the same time, the actual number of bytes used for addresses and netmasks is 4/16, which is why (by default) a CidrNumber's byte representation is truncated to 4/16 bytes to keep it efficient for CIDR operations.

Ranges:

Overflow/underflow semantics:

  • Arithmetic operations (+ and -) return null on overflow/underflow, ensuring values stay within the valid ranges above.

  • Bitwise operations (and, or, xor), shifts (shl, shr), and inversion (inv) operate within the modeled bit-width and always yield a representable value in the respective range.

Its String representation is the hex-encoded byte-representation of the number produced by toByteArray(truncate = false). I.e., 5 bytes for IPv4 when equal to 2^32 and 17 bytes for IPv6 when equal to 2^128; otherwise 4/16 bytes.

See also

Inheritors

Types

Link copied to clipboard
object Companion
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
abstract infix fun and(other: TSelf): TSelf

Bitwise AND within the modeled bit width.

Link copied to clipboard
abstract operator fun compareTo(other: TSelf): Int
Link copied to clipboard
abstract operator fun inv(): TSelf

Bitwise inversion within the modeled bit width.

Link copied to clipboard
abstract operator fun minus(other: TSelf): TSelf?

Subtracts other from this CIDR number.

abstract operator fun minus(other: UInt): TSelf?

Subtracts an unsigned 32-bit integer from this CIDR number.

Link copied to clipboard
abstract infix fun or(other: TSelf): TSelf

Bitwise OR within the modeled bit width.

Link copied to clipboard
abstract operator fun plus(other: TSelf): TSelf?

Adds other to this CIDR number.

abstract operator fun plus(other: UInt): TSelf?

Adds an unsigned 32-bit integer to this CIDR number.

Link copied to clipboard
abstract infix fun shl(bits: Int): TSelf

Logical left shift within the modeled bit width.

Link copied to clipboard
abstract infix fun shr(bits: Int): TSelf

Logical right shift within the modeled bit width.

Link copied to clipboard
abstract fun toByteArray(truncate: Boolean = true): ByteArray

Converts the internal representation of the object into a ByteArray optimized for CIDR/IP operations. The maximum number of addresses inside an IP address space is 2^32 for IPv4 and 2^128 for IPv6. Hence, 32/128-bit numbers are not enough to represent the size of /0 networks, and an additional bit is needed.

Link copied to clipboard
abstract infix fun xor(other: TSelf): TSelf

Bitwise XOR within the modeled bit width.