Functions for bitwise manipulation.

Functions defined in this namespace work with any integer type. More performant functions that work with Int type can be found in dunaj.host.int. Boolean logic functions can be found in dunaj.boolean.

JVM host specific: bitwise operations are not supported for integers of BigInteger or BigInt type.
JVM host specific: It is usually more performant to use dunaj.host.int when handling numeric data from host (e.g. array indexes, enums, lenghts).

When using numeric constants, remember that CLJ format supports hexadecimal and binary notations for integer literals. It will make your code more readable.

1
2
3
4
5
(binary (bit/and 2r10110 2r11011))
;;=> "00010010"

(hexa (bit/and 0x0F0 0x1BC))
;;=> "0x00B0"
Prefer aliasing this namespace (as shown in the usage example) to refering its functions.
1
2
3
4
5
6
7
8
9
(ns foo.bar
  (:api dunaj)
  (:require [dunaj.bit :as bit]))

(def ALL_OPTS
  (bit/or java.nio.channels.SelectionKey/OP_READ
          java.nio.channels.SelectionKey/OP_WRITE
          java.nio.channels.SelectionKey/OP_ACCEPT
          java.nio.channels.SelectionKey/OP_CONNECT))

Primary

In this section: clear flip set test

clear

Available since version 1.0 (alias of clojure.core/bit-clear)

not referred automatically

Usage:
  • (clear x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Returns the number x with cleared (set to 0) bit at zero-based index n.

flip

Available since version 1.0 (alias of clojure.core/bit-flip)

not referred automatically

Usage:
  • (flip x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Returns the number x with flipped bit at zero-based index n.

set

Available since version 1.0 (alias of clojure.core/bit-set)

not referred automatically

Usage:
  • (set x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Returns the number x with set (to 1) bit at zero-based index n.

test

Available since version 1.0 (alias of clojure.core/bit-test)

not referred automatically

Usage:
  • (test x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Boolean+

Returns true if number x has bit at zero-based index n set to 1, otherwise returns false.

Logic

In this section: and and-not not or xor

and

Available since version 1.0 (alias of clojure.core/bit-and)

not referred automatically

Usage:
  • (and x y)

  • (and x y & more)

Type signatures:
  • (Integer+ ⨯ Integer+) → Integer+

  • (Integer+ ⨯ Integer+ ⨯ (Va Integer+)) → Integer+

Returns the result of performing logical AND on each group of corresponding bits.

and-not

Available since version 1.0 (alias of clojure.core/bit-and-not)

not referred automatically

Usage:
  • (and-not x y)

  • (and-not x y & more)

Type signatures:
  • (Integer+ ⨯ Integer+) → Integer+

  • (Integer+ ⨯ Integer+ ⨯ (Va Integer+)) → Integer+

Returns bitwise and with complement (a material non-implication) of x and y.

not

Available since version 1.0 (alias of clojure.core/bit-not)

not referred automatically

Usage:
  • (not x)

Type signature:
  • (Integer+) → Integer+

Returns the result of performing logical NOT on each bit of x.

or

Available since version 1.0 (alias of clojure.core/bit-or)

not referred automatically

Usage:
  • (or x y)

  • (or x y & more)

Type signatures:
  • (Integer+ ⨯ Integer+) → Integer+

  • (Integer+ ⨯ Integer+ ⨯ (Va Integer+)) → Integer+

Returns the result of performing logical OR on each group of corresponding bits.

xor

Available since version 1.0 (alias of clojure.core/bit-xor)

not referred automatically

Usage:
  • (xor x y)

  • (xor x y & more)

Type signatures:
  • (Integer+ ⨯ Integer+) → Integer+

  • (Integer+ ⨯ Integer+ ⨯ (Va Integer+)) → Integer+

Returns the result of performing logical XOR on each group of corresponding bits.

Shifts

<<

Available since version 1.0 (alias of shift-left)

not referred automatically

Usage:
  • (<< x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Alias of shift-left. Returns the result of shifting x to the left by n bits.

>>

Available since version 1.0 (alias of shift-right)

not referred automatically

Usage:
  • (>> x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Alias of shift-right. Returns the result of shifting x to the left by n bits.

>>>

Available since version 1.0 (alias of unsigned-shift-right)

not referred automatically

Usage:
  • (>>> x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Alias of unsigned-shift-right. Returns the result of shifting x to the left by n bits.

shift-left

Available since version 1.0 (alias of clojure.core/bit-shift-left)

not referred automatically

Usage:
  • (shift-left x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Returns the result of shifting x to the left by n bits.

See also: <<, dunaj.host.int/i<<

shift-right

Available since version 1.0 (alias of clojure.core/bit-shift-right)

not referred automatically

Usage:
  • (shift-right x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Returns the result of shifting x to the right by n bits. Performs an arithmetic shift, preserving the sign of the input number.

unsigned-shift-right

Available since version 1.0 (alias of clojure.core/unsigned-bit-shift-right)

not referred automatically

Usage:
  • (unsigned-shift-right x n)

Type signature:
  • (Integer+ ⨯ Integer+) → Integer+

Returns the result of shifting x to the right by n bits. Performs an unsigned (logical) shift, inserting binary value 0.