crow/bits.crow
(source)
Functions for operating on the individual bits in nats / ints.
Bit indexes are counted from the right, so the least-siginificant digit is 0.
So, the number 5 has bits 0 and 2 set, since 5 is 2**0 + 2**2
.
~
nat8
(a
nat8
) bare
Negates every bit.
0 bits become 1 bits and vice versa.
~
nat16
(a
nat16
) bare
~
nat32
(a
nat32
) bare
~
nat64
(a
nat64
) bare
&
nat8
(a
nat8
, b
nat8
) bare
Intersection of bits; 'and's each corresponding bit in parallel.
An output bit will be 1 iff both corresponding input bits are 1.
&
nat16
(a
nat16
, b
nat16
) bare
&
nat32
(a
nat32
, b
nat32
) bare
&
nat64
(a
nat64
, b
nat64
) bare
&
int8
(a
int8
, b
int8
) bare
&
int16
(a
int16
, b
int16
) bare
&
int32
(a
int32
, b
int32
) bare
&
int64
(a
int64
, b
int64
) bare
|
nat8
(a
nat8
, b
nat8
) bare
Union of bits; 'or's each corresponding bit in parallel.
An output bit will be 1 iff either corresponding input bit is 1.
|
nat16
(a
nat16
, b
nat16
) bare
|
nat32
(a
nat32
, b
nat32
) bare
|
nat64
(a
nat64
, b
nat64
) bare
|
int16
(a
int16
, b
int16
) bare
|
int32
(a
int32
, b
int32
) bare
|
int64
(a
int64
, b
int64
) bare
^
nat8
(a
nat8
, b
nat8
) bare
Performs exclusive or of each bit in parallel.
Output bits will be 1 iff the corresponding input bits are not equal.
^
nat16
(a
nat16
, b
nat16
) bare
^
nat32
(a
nat32
, b
nat32
) bare
^
nat64
(a
nat64
, b
nat64
) bare
^
int8
(a
int8
, b
int8
) bare
^
int16
(a
int16
, b
int16
) bare
^
int32
(a
int32
, b
int32
) bare
^
int64
(a
int64
, b
int64
) bare
<<
nat8
(a
nat8
, b
nat8
) bare
Shifts the bits of 'a' left by 'b' bits.
Does not rotate.
<<
nat16
(a
nat16
, b
nat16
) bare
<<
nat32
(a
nat32
, b
nat32
) bare
<<
nat64
(a
nat64
, b
nat64
) bare
>>
nat32
(a
nat32
, b
nat32
) bare
Shifts the bits of 'a' right by 'b' bits.
Does not rotate.
>>
nat64
(a
nat64
, b
nat64
) bare
>>
int32
(a
int32
, b
int32
) bare
bits-intersect
bool
(a
int16
, b
int16
) bare
True iff the numbers have at least one bit in common.
bits-intersect
bool
(a
int32
, b
int32
) bare
bits-intersect
bool
(a
nat32
, b
nat32
) bare
bits-intersect
bool
(a
nat64
, b
nat64
) bare
has-all-bits
bool
(a
nat8
, b
nat8
) bare
True iff for every 1 bit in b
, the corresponding bit in a
is also a 1.
has-all-bits
bool
(a
nat16
, b
nat16
) bare
has-all-bits
bool
(a
nat32
, b
nat32
) bare
has-all-bits
bool
(a
nat64
, b
nat64
) bare
nth-bit
nat64
(bit-index
nat64
)
Number with only the bit at bit-index
set.
Same as 1 << bit-index
.
has-bit
bool
(a
nat64
, bit-index
nat64
)
True if the bit at bit-index
is set.
add-bit
nat64
(a
nat64
, bit-index
nat64
)
Sets the bit at bit-index
to 1.
sub-bit
nat64
(a
nat64
, bit-index
nat64
)
Sets the bit at bit-index
to 0.
count-ones
nat64
(a
nat64
) bare
Returns the number of 1 bits in a
.
high-n-bits
nat64
(n
nat64
) bare
Number with the highest n
bits set.
low-n-bits
nat64
(n
nat64
) bare
Number with the lowest n
bits set.