Clojure - 位運算子



Groovy 提供了四種位運算子。以下是 Groovy 中可用的位運算子。

序號 運算子和描述
1

bit-and

這是按位“與”運算子

2

bit-or

這是按位“或”運算子

3

bit-xor

這是按位“異或”或“排他或”運算子

4

bit-not

這是按位取反運算子

以下是展示這些運算子的真值表。

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

以下程式碼片段展示瞭如何使用各種運算子。

示例

(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)

上述程式產生以下輸出。

輸出

576
37441
36865
clojure_operators.htm
廣告