module Comandante::Bits

Overview

Some bitwise helper functions

Defined in:

comandante/bits.cr

Constant Summary

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 Method Summary

Macro Summary

Class Method Detail

def self.bit_set?(pos, uint) : Bool #

Tests if a bit is set


[View source]
def self.bits_to_string(uint) : String #

Returns binary representation as String


[View source]
def self.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])

[View source]
def self.loop_over_bits(uint, &) : Nil #

Loop over bits of a bitmap (UInt types)


[View source]
def self.loop_over_set_bits(uint, &) : Nil #

Loop over set bits of a bitmap (UInt types)


[View source]
def self.popcount_bits(uint, from, count) : Int32 #

Popcount n bits


[View source]
def self.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])

[View source]
def self.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])

[View source]
def self.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])

[View source]
def self.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])

[View source]
def self.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])

[View source]
def self.store_as_int(uint : UInt8) : Int8 #

Stores uint in int so we can serialize/store in places where uint is not supported


[View source]
def self.store_as_int(uint : UInt16) : Int16 #

Stores uint in int so we can serialize/store in places where uint is not supported


[View source]
def self.store_as_int(uint : UInt32) : Int32 #

Stores uint in int so we can serialize/store in places where uint is not supported


[View source]
def self.store_as_int(uint : UInt64) : Int64 #

Stores uint in int so we can serialize/store in places where uint is not supported


[View source]
def self.store_as_int(uint : UInt128) : Int128 #

Stores uint in int so we can serialize/store in places where uint is not supported


[View source]
def self.store_as_uint(int : Int8) : UInt8 #

Stores int as uint for conversion serialize/store in places where uint is not supported


[View source]
def self.store_as_uint(int : Int16) : UInt16 #

Stores int as uint for conversion serialize/store in places where uint is not supported


[View source]
def self.store_as_uint(int : Int32) : UInt32 #

Stores int as uint for conversion serialize/store in places where uint is not supported


[View source]
def self.store_as_uint(int : Int64) : UInt64 #

Stores int as uint for conversion serialize/store in places where uint is not supported


[View source]
def self.store_as_uint(int : Int128) : UInt128 #

Stores int as uint for conversion serialize/store in places where uint is not supported


[View source]

Macro Detail

macro type_bits(t) #

Returns the number of bits in a type


[View source]