elmw/extra-bit/1.0.10

NOTE: the latest version is 1.0.14

Renamed to nodef/extra-bit.elm. That is where the repository lives now. It has not been published as a package under the new name.

Versions published as elmw/extra-bit still install under that name. 15 versions of 15 come from the new location (see their notes).

Rerouted (renamed, verified). The GitHub user or repository was renamed. The tag is still there, at the new name. The Lamdera package server serves a copy recovered from https://github.com/nodef/extra-bit.elm.

What we checked: The repo moved to github.com/nodef/extra-bit.elm. Rebuilt with git archive from commit e4a4e020cbe6 of https://github.com/nodef/extra-bit.elm (zip prefix elmw-extra-bit-e407832/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 44794e680d71d6372c0ca1ba34ecae6420a443a7.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details ยท package.elm-lang.org's zip: https://github.com/elmw/extra-bit/zipball/1.0.10/

Bit

The bit is a basic unit of information in information theory, computing. This package includes bit twiddling hacks by Sean Eron Anderson and many others.
๐Ÿ“ฆ Package, ๐Ÿ“˜ Wiki.

count : Int -> Int

Count bits set. ๐Ÿ“˜

-- x: an Int
count 7  == 3 (111    => 3)
count 12 == 2 (1100   => 2)
count 63 == 6 (111111 => 6)
get : Int -> Int -> Int

Get a bit. ๐Ÿ“˜

-- x: an Int
-- i: bit index
get 6 0 == 0 (110,0 => 0)
get 6 1 == 1 (110,1 => 1)
get 6 2 == 1 (110,2 => 1)
getAs : Int -> Int -> Int

Get bits as per mask. ๐Ÿ“˜

-- x: an Int
-- m: bit mask
getAs 6 4 == 4 (110,100 => 100)
getAs 6 7 == 6 (110,111 => 110)
getAs 6 5 == 4 (110,101 => 100)
interleave : Int -> Int -> Int

Interleave bits of two Int16s. ๐Ÿ“˜

-- x: first Int16
-- y: second Int16
interleave 0x0000 0xFFFF == 1431655765 (0x55555555)
interleave 0x1234 0x1234 == 51121968   (0x030C0F30)
interleave 0x1234 0x4321 == 302845473  (0x120D0E21)
merge : Int -> Int -> Int -> Int

Merge bits as per mask. ๐Ÿ“˜

-- x: first Int
-- y: second Int
-- m: bit mask (0 => from x)
merge 0x12 0x24 0x0F       == 20    (0x14)
merge 0x1234 0xABCD 0x0F0F == 6973  (0x1B3D)
merge 0xAAAA 0xBBBB 0x3333 == 48059 (0xBBBB)
parity : Int -> Int -> Int

Get n-bit parity. ๐Ÿ“˜

-- x: an Int
-- n: number of bits (1)
parity 7 1  == 1  (1,1,1   => 1)
parity 5 1  == 0  (1,0,1   => 0)
parity 8 2  == 2  (10,00   => 10)
parity 63 4 == 12 (11,1111 => 1100)
reverse : Int -> Int

Reverse all bits. ๐Ÿ“˜

-- x: an Int
reverse 0xFFFF0000 == 65535     (0x0000FFFF)
reverse 0x00AABBCC == 870143232 (0x33DD5500)
reverse 0x1234     == 742916096 (0x2C480000)
rotate : Int -> Int -> Int

Rotate bits. ๐Ÿ“˜

-- x: an Int
-- n: rotate amount (+ve: left, -ve: right)
rotate 0x11112222 4  == 286401057 (0x11122221)
rotate 0x11112222 -4 == 554766882 (0x21111222)
scan : Int -> Int

Get index of first set bit from LSB. ๐Ÿ“˜

-- x: an Int
scan 7  == 0 (111     => 0)
scan 12 == 2 (1100    => 2)
scan 64 == 6 (1000000 => 6)
scanReverse : Int -> Int

Gets index of first set bit from MSB. ๐Ÿ“˜

-- x: an Int
scanReverse 13 == 3 (1101 => 3)
scanReverse 5  == 2 (101  => 2)
scanReverse 1  == 0 (1    => 0)
set : Int -> Int -> Int -> Int

Set a bit. ๐Ÿ“˜

-- x: an Int
-- i: bit index
-- f: bit value (1)
set 6 0 1 == 7 (110,0,1 => 111)
set 6 2 1 == 6 (110,2,1 => 110)
set 6 2 0 == 2 (110,2,0 => 010)
setAs : Int -> Int -> Int -> Int

Set bits as per mask. ๐Ÿ“˜

-- x: an Int
-- m: bit mask
-- f: bit value (1)
setAs 8 2 1          == 10   (0x8 set 0x2      => 0xA)
setAs 15 3 0         == 12   (0xF clear 0x3    => 0xC)
setAs 0x1234 0x430 1 == 5684 (0x1234 set 0x430 => 0x1634)
signExtend : Int -> Int -> Int

Sign extend variable bit-width integer. ๐Ÿ“˜

-- x: variable bit-width Int
-- w: bit width (32)
signExtend 15 4 == -1 (1111 => -1)
signExtend 3 3  == 3  (011  => 3)
signExtend 4 3  == -4 (100  => -4)
swap : Int -> Int -> Int -> Int -> Int

Swap bit sequences. ๐Ÿ“˜

-- x: an Int
-- i: first bit index
-- j: second bit index
-- n: bit width (1)
swap 6 1 0 1      == 5     (110    => 101)
swap 0x1234 8 4 4 == 4900  (0x1234 => 0x1324)
swap 0x4AAB 8 0 8 == 43850 (0x4AAB => 0xAB4A)
toggle : Int -> Int -> Int

Toggle a bit. ๐Ÿ“˜

-- x: an Int
-- i: bit index
toggle 6 0 == 7 (110,0 => 111)
toggle 6 1 == 4 (110,1 => 100)
toggle 6 2 == 2 (110,2 => 010)
toggleAs : Int -> Int -> Int

Toggle bits as per mask. ๐Ÿ“˜

-- x: an Int
-- m: bit mask
toggleAs 6 1 == 7 (110,000 => 111)
toggleAs 6 7 == 1 (110,111 => 001)
toggleAs 6 3 == 5 (110,011 => 101)