Working with Bits
Introduction
Every primitive data-type is a collection of bits. The Bits
module provides a set of functions to work with bits.
Datatype | Bits |
---|---|
Byte | 8 |
Short | 16 |
Int | 32 |
Long | 64 |
Float | 32 |
Double | 64 |
UByte | 8 |
UShort | 16 |
UInt | 32 |
ULong | 64 |
Char | 16 |
Note: This table only shows data-types supported by Bits
module. Boolean
is not supported,
as booleans are implemented as one byte, but acts as a single bit and you can't modify individual bits.
Getting Access
bit{n}
You can access the nth bit of a value using the bit{n}
value, it returns the value of the nth bit.
This provides a dedicated property for each bit of each data-type and is extremely fast.
import com.shakelang.util.primitives.bits.*
val value: Int = 1
val bit0 = value.bit0 // true (1)
val bit1 = value.bit1 // false (0)
View all bit properties
- Byte.bit{0-7} e.g.
value.bit0
,value.bit1
, ...,value.bit7
- Short.bit{0-15} e.g.
value.bit0
,value.bit1
, ...,value.bit15
- Int.bit{0-31} e.g.
value.bit0
,value.bit1
, ...,value.bit31
- Long.bit{0-63} e.g.
value.bit0
,value.bit1
, ...,value.bit63
- Float.bit{0-31} e.g.
value.bit0
,value.bit1
, ...,value.bit31
- Double.bit{0-63} e.g.
value.bit0
,value.bit1
, ...,value.bit63
- UByte.bit{0-7} e.g.
value.bit0
,value.bit1
, ...,value.bit7
- UShort.bit{0-15} e.g.
value.bit0
,value.bit1
, ...,value.bit15
- UInt.bit{0-31} e.g.
value.bit0
,value.bit1
, ...,value.bit31
- ULong.bit{0-63} e.g.
value.bit0
,value.bit1
, ...,value.bit63
- Char.bit{0-15} e.g.
value.bit0
,value.bit1
, ...,value.bit15
bit(n)
You can access the nth bit of a value using the bit(n)
function, it returns the value of the nth bit.
This provides a function, which is slower than the property, but allows you to access bits dynamically.
import com.shakelang.util.primitives.bits.*
val value: Int = 1
val bit0 = value.bit(0) // true (1)
val bit1 = value.bit(1) // false (0)
Modifying Bits
Primitive data-types are immutable, so you can't change the value of a bit directly. You can create a new value with the bit changed.
Therefor, the Bits
module provides a module to modify
the value of a bit and return the changed value.
For this reason the methods are prefixed with with
instead of set
.
withBit{n}(value)
You can set the nth bit of a value using the setBit{n}(value)
function, it sets the nth bit to the given value.
This provides a dedicated function for each bit of each data-type and is extremely fast.
import com.shakelang.util.primitives.bits.*
var value: Int = 1
value = value.setBit0(false) // 0
value = value.setBit1(true) // 2
View all setBit functions
- Byte.setBit{0-7}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit7(true)
- Short.setBit{0-15}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit15(true)
- Int.setBit{0-31}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit31(true)
- Long.setBit{0-63}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit63(true)
- Float.setBit{0-31}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit31(true)
- Double.setBit{0-63}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit63(true)
- UByte.setBit{0-7}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit7(true)
- UShort.setBit{0-15}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit15(true)
- UInt.setBit{0-31}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit31(true)
- ULong.setBit{0-63}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit63(true)
- Char.setBit{0-15}(value) e.g.
value.setBit0(true)
,value.setBit1(false)
, ...,value.setBit15(true)
setBit(n, value)
You can set the nth bit of a value using the setBit(n, value)
function, it sets the nth bit to the given value.
import com.shakelang.util.primitives.bits.*
var value: Int = 1
value = value.setBit(0, false) // 0
value = value.setBit(1, true) // 2