Clojure - Bitwise Operators



Groovy provides four bitwise operators. Following are the bitwise operators available in Groovy.

Sr.No. Operator & Description
1

bit-and

This is the bitwise “and” operator

2

bit-or

This is the bitwise “or” operator

3

bit-xor

This is the bitwise “xor” or Exclusive ‘or’ operator

4

bit-not

This is the bitwise negation operator

Following is the truth table showcasing these operators.

p q p&q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

The following code snippet shows how the various operators can be used.

Example

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (bit-and 00111100 00001101))
   (println x)
   
   (def x (bit-or 00111100 00001101))
   (println x)
   
   (def x (bit-xor 00111100 00001101))
   (println x)) 
(Example)

The above program produces the following output.

Output

576
37441
36865
clojure_operators.htm
Advertisements