module Comandante::Bits
Some bitwise helper functions
Constants
BIT_MASKS = {8 => BitMasks(UInt8).get_masks, 16 => BitMasks(UInt16).get_masks, 32 => BitMasks(UInt32).get_masks, 64 => BitMasks(UInt64).get_masks, 128 => BitMasks(UInt128).get_masks}
Bit masks for every bit position for a UInt type
MASKS = {8 => {0.to_u8, 1.to_u8, UInt8::MAX}, 16 => {0.to_u16, 1.to_u16, UInt16::MAX}, 32 => {0.to_u32, 1.to_u32, UInt32::MAX}, 64 => {0.to_u64, 1.to_u64, UInt64::MAX}, 128 => {0.to_u128, 1.to_u128, UInt128::MAX}}
Masks for each size of UInt of 0,1, and MAX
Class methods
.get_bits(uint, from : Int32, count : Int32)
Returns n bits from position
Example
To get a new uint with only count bits from given position
x = get_bits(u, from: 3, count 2])
.set_bits(uint, a : Array(Int8))
Sets bits on Uint Types
Example
To set bits 1,7 and 8
set_bits(x, [1, 7, 8])
.set_bits(uint, a : Array(Int16))
Sets bits on Uint Types
Example
To set bits 1,7 and 8
set_bits(x, [1, 7, 8])
.set_bits(uint, a : Array(Int32))
Sets bits on Uint Types
Example
To set bits 1,7 and 8
set_bits(x, [1, 7, 8])
.set_bits(uint, a : Array(Int64))
Sets bits on Uint Types
Example
To set bits 1,7 and 8
set_bits(x, [1, 7, 8])
.set_bits(uint, a : Array(Int128))
Sets bits on Uint Types
Example
To set bits 1,7 and 8
set_bits(x, [1, 7, 8])
.store_as_int(uint : UInt8) : Int8
Stores uint in int so we can serialize/store in places where uint is not supported
.store_as_int(uint : UInt16) : Int16
Stores uint in int so we can serialize/store in places where uint is not supported
.store_as_int(uint : UInt32) : Int32
Stores uint in int so we can serialize/store in places where uint is not supported
.store_as_int(uint : UInt64) : Int64
Stores uint in int so we can serialize/store in places where uint is not supported
.store_as_int(uint : UInt128) : Int128
Stores uint in int so we can serialize/store in places where uint is not supported
.store_as_uint(int : Int8) : UInt8
Stores int as uint for conversion serialize/store in places where uint is not supported
.store_as_uint(int : Int16) : UInt16
Stores int as uint for conversion serialize/store in places where uint is not supported
.store_as_uint(int : Int32) : UInt32
Stores int as uint for conversion serialize/store in places where uint is not supported
.store_as_uint(int : Int64) : UInt64
Stores int as uint for conversion serialize/store in places where uint is not supported
.store_as_uint(int : Int128) : UInt128
Stores int as uint for conversion serialize/store in places where uint is not supported