BitField

value class BitField<T : Enum<T>>(value: ULong)(source)

A type-safe bit field implementation for efficient flag operations.

This class provides a zero-cost abstraction over integer bit operations while maintaining type safety through Kotlin's inline value classes. It's designed to work with enum classes that represent bit flags, ensuring compile-time safety and runtime efficiency.

Key Features:

  • Zero runtime overhead through inline value classes

  • Type-safe flag operations with enum constraints

  • Immutable by default with functional operations

  • Comprehensive operator overloading for natural syntax

  • Full interoperability with integer types when needed

Usage Examples:

enum class FilePermissions(val bit: Int) {
READ(1 shl 0),
WRITE(1 shl 1),
EXECUTE(1 shl 2);

companion object : BitFieldEnum<FilePermissions> {
override fun fromValue(value: ULong): FilePermissions? {
return entries.find { it.bit.toULong() == value }
}
override fun BitFieldEnum.Companion.allFlags(): BitField<FilePermissions> {
return BitField(entries.fold(0UL) { acc, perm -> acc or perm.bit.toULong() })
}
}
}

// Create and manipulate bit fields
val permissions = BitField<FilePermissions>()
.setFlag(FilePermissions.READ)
.setFlag(FilePermissions.WRITE)

if (permissions.hasFlag(FilePermissions.READ)) {
println("Has read permission")
}

val combined = permissions or BitField(FilePermissions.EXECUTE)
val readOnly = permissions and BitField(FilePermissions.READ)

Parameters

T

The enum type representing individual flags

Constructors

Link copied to clipboard
constructor(value: ULong)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
inline fun <T : Enum<T>> BitField<T>.allFlags(enumValues: Array<T>, predicate: (T) -> Boolean): Boolean

Checks if all set flags match the predicate.

Link copied to clipboard
infix fun and(flag: T): BitField<T>

infix fun and(other: BitField<T>): BitField<T>

Bitwise AND

Link copied to clipboard
inline fun <T : Enum<T>> BitField<T>.anyFlag(enumValues: Array<T>, predicate: (T) -> Boolean): Boolean

Checks if any set flag matches the predicate.

Link copied to clipboard
fun clear(): BitField<T>

Clears all flags, returning an empty bit field.

Link copied to clipboard
fun clearFlag(flag: T): BitField<T>

Clears the specified flag from this bit field. Returns a new BitField with the flag cleared.

fun clearFlag(other: BitField<T>): BitField<T>

Clears the specified flags from another BitField.

Link copied to clipboard
fun compareTo(other: BitField<T>): Int

Compares this BitField with another based on their underlying values.

Link copied to clipboard
operator fun contains(flag: T): Boolean

Contains operator - checks if flag is set

operator fun contains(other: BitField<T>): Boolean
Link copied to clipboard

Returns the number of flags set in this bit field.

Link copied to clipboard
inline fun <T : Enum<T>> BitField<T>.filterFlags(enumValues: Array<T>, predicate: (T) -> Boolean): List<T>

Filters the set flags based on a predicate.

Link copied to clipboard
inline fun <T : Enum<T>> BitField<T>.forEachFlag(enumValues: Array<T>, action: (T) -> Unit)

Applies an action to each set flag.

Link copied to clipboard
inline fun forEachSetFlag(enumValues: Array<T>, action: (T) -> Unit)

Iterates over all set flags in this bit field.

Link copied to clipboard

Returns the union of this bit field and another (logical OR).

Link copied to clipboard

Returns the symmetric difference of this bit field and another (logical XOR).

Link copied to clipboard

Returns the complement of this bit field (logical NOT). Note: This inverts all bits, which may include more than just the enum values.

Link copied to clipboard
fun getSetFlags(enumValues: Array<T>): List<T>

Returns a list of all flags that are set in this bit field.

Link copied to clipboard
fun getShared(other: BitField<T>): BitField<T>

Returns the intersection of this bit field and another (logical AND).

Link copied to clipboard

Checks if all flags in the other BitField are set.

Link copied to clipboard

Checks if any of the flags in the other BitField are set.

Link copied to clipboard
fun hasFlag(flag: T): Boolean

Checks if the specified flag is set in this bit field.

Link copied to clipboard

Returns the position of the highest set bit (0-based).

Link copied to clipboard
inline fun <T : Enum<T>, R> BitField<T>.ifNotEmpty(transform: (BitField<T>) -> R): R?

Applies a transformation to a BitField if it's not empty.

Link copied to clipboard

Checks if this bit field is empty (no flags set).

Link copied to clipboard

Checks if this bit field is not empty (has flags set).

Link copied to clipboard

Returns the position of the lowest set bit (0-based).

Link copied to clipboard
inline fun <T : Enum<T>, R> BitField<T>.mapFlags(enumValues: Array<T>, transform: (T) -> R): List<R>

Maps the set flags to a list of results.

Link copied to clipboard
operator fun minus(flag: T): BitField<T>

operator fun minus(other: BitField<T>): BitField<T>

Subtraction operator - removes flags from this bit field

Link copied to clipboard
operator fun not(): BitField<T>

Bitwise NOT

Link copied to clipboard
infix fun or(flag: T): BitField<T>

infix fun or(other: BitField<T>): BitField<T>

Bitwise OR

Link copied to clipboard
operator fun plus(flag: T): BitField<T>

operator fun plus(other: BitField<T>): BitField<T>

Logical OR operator - combines two bit fields

Link copied to clipboard
fun setFlag(flag: T): BitField<T>

Sets the specified flag in this bit field. Returns a new BitField with the flag set.

fun setFlag(other: BitField<T>): BitField<T>

Sets the specified flag from another BitField.

Link copied to clipboard
fun setFlagsSequence(enumValues: Array<T>): Sequence<T>

Creates a sequence of all set flags.

Link copied to clipboard
operator fun times(other: BitField<T>): BitField<T>

Logical AND operator - intersects two bit fields

Link copied to clipboard

Returns a string representation showing the binary value.

Link copied to clipboard
fun toDetailedString(enumValues: Array<T>): String

Returns a detailed string representation with set flags.

Link copied to clipboard
fun toggleFlag(flag: T): BitField<T>

Toggles the specified flag in this bit field.

Link copied to clipboard

Returns a hexadecimal string representation.

Link copied to clipboard
fun toInt(): Int

Converts this BitField to Int. Note: This may truncate the value if it doesn't fit in 32 bits.

Link copied to clipboard
fun toLong(): Long

Converts this BitField to Long.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun toUInt(): UInt

Converts this BitField to UInt. Note: This may truncate the value if it doesn't fit in 32 bits.

Link copied to clipboard
fun toULong(): ULong

Converts this BitField to its underlying ULong value.

Link copied to clipboard
infix fun xor(flag: T): BitField<T>

infix fun xor(other: BitField<T>): BitField<T>

Bitwise XOR