Skip to content

Commit

Permalink
Added bits functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Izaakwltn committed Oct 24, 2024
1 parent c54feeb commit b81a420
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions library/bits.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
#:or
#:xor
#:not
#:shift))
#:shift
#:dpb
#:ldb
#:bits->string))

(in-package #:coalton-library/bits)

Expand All @@ -36,7 +39,25 @@
(not "The bitwise logical `not` of two integers"
(:int -> :int))
(shift "The arithmetic left-shift of an integer by an integer number of bits"
(Integer -> :int -> :int))))
(Integer -> :int -> :int)))

(declare dpb (Bits :a => :a -> UFix -> UFix -> :a -> :a))
(define (dpb newbyte size position bitstring)
"Deposits a byte `newbyte` of size `size` into a bitstring `bitstring` at a position `position`."
(lisp :a (newbyte bitstring size position)
(cl:dpb newbyte (cl:byte size position) bitstring)))

(declare ldb (Bits :a => UFix -> UFix -> :a -> :a))
(define (ldb size position bitstring)
"Deposits a byte of size `size` into a bitstring at a position `position`."
(lisp :a (bitstring size position)
(cl:ldb (cl:byte size position) bitstring)))

(declare bits->string (Bits :a => UFix -> :a -> String))
(define (bits->string width x)
"Prints an integer to a binary string, preserving leading zeros to a given width."
(lisp String (x width)
(cl:format cl:nil "~v,'0b" width x))))

#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/BITS")
Expand Down

0 comments on commit b81a420

Please sign in to comment.