bitwise-utils

  1. bitwise-utils
  2. Documentation
    1. bitwise-mask
    2. bitwise-join
    3. bitwise-split
    4. bitwise-count
    5. logical-shift-left
    6. logical-shift-right
    7. arithmetic-shift-left
    8. arithmetic-shift-right
    9. Usage
  3. Author
  4. Version history
  5. License

Documentation

Additional bitwise routines.

bitwise-mask

[procedure] (bitwise-mask WID [ON?]) --> integer

Returns WID low-order bits ON?.

WID
fixnum ; mask bit width.
ON?]
boolean ; #t is #b1, #f is #b0; default #t.
(bitwise-mask 5 #t) ;=> +0...011111 (#b+011111)
(bitwise-mask 5 #f) ;=> -1...100000 (#b-100000)

bitwise-join

[procedure] (bitwise-join N0 [N1 ...]) --> integer

Returns adjoined bits.

N0
integer ; seed.
N1 ...
integer ... ; adjoins seed left-to-right.
(bitwise-join #b10 #b0000001 #b101) ;=> #b101101

bitwise-split

[procedure] (bitwise-split N WID) --> (list-of integer)

Returns N disjoined every WID bits.

N
integer ; seed.
WID
fixnum ; bit width.
(bitwise-split #xabcdef 4) ;=> (#xa #xb #xc #xd #xe #xf)
(bitwise-split #x-abcdef 4) ;=> (#x-a #x-b #x-c #x-d #x-e #x-f)

bitwise-count

[procedure] (bitwise-count N) --> fixnum

Returns number of set bits in N.

N
integer

logical-shift-left

[procedure] (logical-shift-left N1 N2) --> integer

Returns left shift of N1 by N2 bits.

N1
integer ; what to shift.
N2
fixnum ; amount to shift.

Aliases arithmetic-shift.

logical-shift-right

[procedure] (logical-shift-right N1 N2) --> integer

Returns right shift of N1 by N2 bits. Sign preserving.

N1
integer ; what to shift.
N2
fixnum ; amount to shift.

arithmetic-shift-left

[procedure] (arithmetic-shift-left N1 N2) --> integer

Returns left shift of N1 by N2 bits.

N1
integer ; what to shift.
N2
fixnum ; amount to shift.

Aliases arithmetic-shift.

arithmetic-shift-right

[procedure] (arithmetic-shift-right N1 N2) --> integer

Returns right shift of N1 by N2 bits. Sign extending.

N1
integer ; what to shift.
N2
fixnum ; amount to shift.

Essentially (arithmetic-shift <> (- <>)).

Usage

(import bitwise-utils)

Author

Kon Lovett

Version history

1.0.0
Release.

License

This code is in the public domain.