Login
7 branches 0 tags
Ben (X13/Arch) Reimplemented [uppercase][lowercase][capitalize] in Nujel ac56248 3 years ago 839 Commits
nujel / stdlib / bitmanip.nuj
;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;; This project uses the MIT license, a copy should be included under /LICENSE
;;
;; Some functions manipulating binary date

[defn bit-nand l
      "Returns the Nand of its arguments"
      [bit-not [apply bit-and l]]]

[defn bit-and-not [x y]
      "Bitwise and with complement"
      [bit-and x [bit-not y]]]

[defn bit-test? [α i]
      "Test bit at position i"
      [typecheck/only α :int]
      [typecheck/only i :int]
      [not [zero? [bit-and α [bit-shift-left 1 i]]]]]
[def bit-set? bit-test?]

[defn bit-shift-right [α i]
      "Bitwise shift right"
      [bit-shift-left α [- i]]]

[defn bit-set [x i]
      "Set bit at i"
      [bit-or x [bit-shift-left 1 i]]]

[defn bit-flip [x i]
      "Flip bit at i"
      [bit-xor x [bit-shift-left 1 i]]]

[defn bit-clear [x i]
      "Clear bit at i"
      [bit-and x [bit-not [bit-shift-left 1 i]]]]