bitwise-utils

  1. bitwise-utils
  2. Documentation
    1. Usage
    2. logical-shift-left
    3. logical-shift-right
    4. arithmetic-shift-left
    5. arithmetic-shift-right
    6. bitwise-mask
    7. bitwise-join
    8. bitwise-split
    9. bitwise-count
    10. bitwise-merge
    11. bitwise-nth?
    12. bitwise-any?
    13. bitwise-first-set
    14. bitwise-reverse
    15. bitwise-rotate
    16. bitwise-set-nth
    17. bitwise-field
    18. bitwise-field-copy
    19. bitwise-field-reverse
    20. number->boolean
    21. boolean->number
    22. integer->list
    23. list->integer
    24. bitwise-zeros
    25. bitwise-ones
    26. bitwise-abs
    27. bitwise-drop-right
    28. bitwise-cons
  3. Requirements
  4. Author
  5. Repository
  6. Version history
  7. License

Documentation

Additional bitwise routines.

Usage

(import bitwise-utils)

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 <> (- <>)).

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

bitwise-merge

[procedure] (bitwise-merge MASK N0 N1) -> integer
MASK
integer
N0
integer
N1
integer

Renamed SRFI-60 bitwise-if.

bitwise-nth?

[procedure] (bitwise-nth? INDEX N) -> boolean
INDEX
integer
N
fixnum

Renamed SRFI-60 bit-set?.

bitwise-any?

[procedure] (bitwise-any? N1 N2) -> boolean
N1
integer
N2
integer

Renamed SRFI-60 any-bits-set?.

bitwise-first-set

[procedure] (bitwise-first-set N) -> fixnum
N
integer

Renamed SRFI-60 first-set-bit.

bitwise-reverse

[procedure] (bitwise-reverse N K) -> integer
N
integer
K
fixnum

bitwise-rotate

[procedure] (bitwise-rotate K COUNT LEN) -> integer

Performs (bitwise-field-rotate K COUNT 0 LEN).

K
integer
COUNT
fixnum
LEN
fixnum

bitwise-set-nth

[procedure] (bitwise-set-nth TO INDEX ON?) -> integer
TO
integer
INDEX
fixnum
ON?
boolean ; #t is #b1, #f is #b0

Renamed SRFI-60 (copy-bit k n b); note argument order change.

bitwise-field

[procedure] (bitwise-field N START END) -> integer
N
integer
START
fixnum
END
fixnum

Renamed SRFI-60 bit-field.

bitwise-field-copy

[procedure] (bitwise-field-copy TO FROM START END) -> integer
TO
integer
FROM
integer
START
fixnum
END
fixnum

Renamed SRFI-60 copy-bit-field.

bitwise-field-reverse

[procedure] (bitwise-field-reverse N START END) -> integer
N
integer
START
fixnum
END
fixnum

Renamed SRFI-60 reverse-bit-field.

[procedure] (bitwise-field-rotate N COUNT START END) -> integer
N
integer
COUNT
fixnum
START
fixnum
END
fixnum

Renamed SRFI-60 rotate-bit-field.

number->boolean

[procedure] (number->boolean NUM) -> boolean

0 => #f & ¬ 0 => #t.

boolean->number

[procedure] (boolean->number ANY) -> fixnum

#f => 0 & #t => 1.

integer->list

[procedure] (integer->list K [LEN]) -> (list-of boolean)
K
integer ; integer to represent as a bit list.
LEN
fixnum ; number of bits, default is all bits.

list->integer

[procedure] (list->integer BOOLS) -> integer
BOOLS
list ; treated as (list-of boolean-like)

bitwise-zeros

[procedure] (bitwise-zeros N) -> integer

Return N zero bits.

N
fixnum

bitwise-ones

[procedure] (bitwise-ones N) -> integer

Return N one bits.

N
fixnum

bitwise-abs

[procedure] (bitwise-abs N) -> integer

Should N be negative bitwise-not the result.

N
integer

bitwise-drop-right

[procedure] (bitwise-drop-right N W) -> integer

Drop W right bits of N.

N
integer
W
fixnum

bitwise-cons

[procedure] (bitwise-cons N M) -> integer

Concatenate N & M.

N
integer
M
integer

Requirements

test test-utils

Author

Kon Lovett

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/bitwise-utils

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Version history

1.4.1
Fix list->integer type regression. Fix boolean->number signature.
1.4.0
Add {{}} & {{}}. Fix signatures (clean, not pure).
1.3.2
Slight speed-up.
1.3.1
New test-runner, add tests.
1.3.0
Export useful helpers bitwise-zeros, bitwise-ones, bitwise-abs, bitwise-drop-right, & bitwise-cons.
1.2.2
Fix bitwise->count.
1.2.2
Fix integer->list/list->integer so invertible.
1.2.1
.
1.2.0
Remove srfi-60 module. (Use srfi-60 egg.)
1.1.4
Fix integer->list type.
1.1.3
.
1.1.2
.
1.1.1
.
1.1.0
Add srfi-60 module.
1.0.1
.
1.0.0
Release.

License

This code is in the public domain.