Skip to content

Instantly share code, notes, and snippets.

@aw
Created October 12, 2020 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aw/9c1e96fc91da733cea87a85b827eab42 to your computer and use it in GitHub Desktop.
Save aw/9c1e96fc91da733cea87a85b827eab42 to your computer and use it in GitHub Desktop.
Twos complement and reverse in PicoLisp

This obtains the twos complement of a signed integer (from signed -> unsigned), or reverse (from unsigned -> signed)

  • Int: an positive or negative integer
  • Size: expressed in bits (ex: 8, 16, 32, 64...)
(de twos-complement (Int Size)
  (if (lt0 Int)
      (+ Int (>> (- Size) 1))
      (if (=0 (& Int (>> (- (- Size 1)) 1)))
          Int
          (- Int (>> (- Size) 1)) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment