Skip to content

Instantly share code, notes, and snippets.

@jongha
Forked from LouisCAD/BitFlags.kt
Created December 30, 2018 05:14
Show Gist options
  • Save jongha/dfaba0851a79a92cf813f748cf54ac56 to your computer and use it in GitHub Desktop.
Save jongha/dfaba0851a79a92cf813f748cf54ac56 to your computer and use it in GitHub Desktop.
Allows simple bit flags operation on int values in kotlin. Inspired by: http://stackoverflow.com/a/40588216/4433326
@file:Suppress("NOTHING_TO_INLINE")
import kotlin.experimental.and // Used for Byte
import kotlin.experimental.inv // Used for Byte
import kotlin.experimental.or // Used for Byte
inline fun Int.hasFlag(flag: Int) = flag and this == flag
inline fun Int.withFlag(flag: Int) = this or flag
inline fun Int.minusFlag(flag: Int) = this and flag.inv()
inline fun Byte.hasFlag(flag: Byte) = flag and this == flag
inline fun Byte.withFlag(flag: Byte) = this or flag
inline fun Byte.minusFlag(flag: Byte) = this and flag.inv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment