View Source Wafer.Twiddles (wafer v1.1.0)
Handy functions for dealing with bits and bytes in the wild.
Summary
Functions
Set the specified bit
to 0
within the byte
.
Returns the number of 1's in byte
.
Returns the number of 0's in byte
.
Find all the 1
bits in byte
and return a list of bit_number
s.
Find all the 0
bits in byte
and return a list of bit_number
s.
Get the bit at the specified bit_number
within byte
.
Return a bit out of a binary as a boolean value.
Set a the specified bit_number
to 1
within the byte
.
Set the bit at the specified bit_number
within the byte
to the bit
value.
Types
@type bit() :: 0..1
@type bit_number() :: 0..7
@type single_byte_binary() :: <<_::8>>
Functions
@spec clear_bit(byte() | single_byte_binary(), bit_number()) :: byte()
Set the specified bit
to 0
within the byte
.
Example
iex> clear_bit(3, 0)
2
iex> clear_bit(<<3>>, 0)
<<2>>
@spec count_ones(byte() | single_byte_binary()) :: non_neg_integer()
Returns the number of 1's in byte
.
Example
iex> count_ones(0x7f)
7
iex> count_ones(<<0x7F>>)
7
@spec count_zeroes(byte() | single_byte_binary()) :: non_neg_integer()
Returns the number of 0's in byte
.
Example
iex> count_zeroes(0x7f)
1
iex> count_zeroes(<<0x7F>>)
1
@spec find_ones(byte() | single_byte_binary()) :: [non_neg_integer()]
Find all the 1
bits in byte
and return a list of bit_number
s.
Example
iex> find_ones(0x0A)
[1, 3]
iex> find_ones(<<0x0A>>)
[1, 3]
@spec find_zeroes(byte() | single_byte_binary()) :: [non_neg_integer()]
Find all the 0
bits in byte
and return a list of bit_number
s.
Example
iex> find_zeroes(0xFA)
[0, 2]
iex> find_zeroes(<<0xFA>>)
[0, 2]
@spec get_bit(byte() | single_byte_binary(), bit_number()) :: bit()
Get the bit at the specified bit_number
within byte
.
Example
iex> get_bit(0x7f, 6)
1
iex> get_bit(<<0x7F>>, 6)
1
@spec get_bool(byte() | single_byte_binary(), bit_number()) :: boolean()
Return a bit out of a binary as a boolean value.
Example
iex> get_bool(0x7f, 6)
true
iex> get_bool(<<0x7F>>, 6)
true
@spec set_bit(byte() | single_byte_binary(), bit_number()) :: byte()
Set a the specified bit_number
to 1
within the byte
.
Example
iex> set_bit(0, 0)
1
iex> set_bit(<<0>>, 0)
<<1>>
@spec set_bit(byte() | single_byte_binary(), bit_number(), bit()) :: byte()
Set the bit at the specified bit_number
within the byte
to the bit
value.
Example
iex> set_bit(0, 1, 1)
2
iex> set_bit(<<0>>, 1, 1)
<<2>>
iex> set_bit(0, 1, true)
2
iex> set_bit(<<0>>, 1, true)
<<2>>