Skip to content

Instantly share code, notes, and snippets.

@adityabhaskar
Forked from LouisCAD/BitFlags.kt
Created October 4, 2019 16:46
Show Gist options
  • Save adityabhaskar/fe76f4ae7be7e5373d88fb9b1c3c765a to your computer and use it in GitHub Desktop.
Save adityabhaskar/fe76f4ae7be7e5373d88fb9b1c3c765a 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