# Clojure - 运算符

Clojure具有以下类型的运算符-

• 算术运算符
• 关系运算符
• 逻辑运算符
• 按位运算符

`(operator operand1 operand2 operandn)`

## 运算符示例

`(+ 1 2)`

## 算术运算符

Clojure语言支持普通的算术运算符，就像任何语言一样。以下是Clojure中可用的算术运算符。

Operator描述Example
+相加(+ 1 2)=3
-相减(- 2 1)=1
*相乘(* 2 2)=4
/相除(float (/3 2))=1.5
inc递增inc 5=6
dec递减dec 5=4
max最大值max 1 2 3 will return 3
min最小值min 1 2 3 will return 1
rem余数rem 3 2=1

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

(defn Example []
(def x (+ 2 2))
(println x)

(def x (- 2 1))
(println x)

(def x (* 2 2))
(println x)

(def x (float(/ 2 1)))
(println x)

(def x (inc 2))
(println x)

(def x (dec 2))
(println x)

(def x (max 1 2 3))
(println x)

(def x (min 1 2 3))
(println x)

(def x (rem 3 2))
(println x))
(Example)```

```4
1
4
2.0
3
1
3
1
1```

## 关系运算符

Operator描述Example
=判断是否相等(= 2 2)=true
not=判断是否不相等(not=3 2)=true
<小于(< 2 3)=true
<=小于或等于(<= 2 3)=true
>大于(> 3 2)=true
>=大于或等于(>= 3 2)=true
```(ns clojure.examples.hello
(:gen-class))

(defn Example []
(def x (= 2 2))
(println x)

(def x (not= 3 2))
(println x)

(def x (< 2 3))
(println x)

(def x (<= 2 3))
(println x)

(def x (> 3 2))
(println x)

(def x (>= 3 2))
(println x))
(Example)```

```true
true
true
true
true
true```

## 逻辑运算符

Operator描述Example
and这是逻辑“and”运算符(or true true)=true
or这是逻辑“or”运算符(and true false)=false
not这是逻辑“not”运算符(not false)=true

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

;; This program displays Hello Learnfk
(defn Example []
(def x (or true true))
(println x)

(def x (and true false))
(println x)

(def x (not true))
(println x))
(Example)```

```true
false
false```

## 按位运算符

Clojure提供了四个按位运算符。以下是Clojure中可用的按位运算符。

Sr.No.Operator & 描述
1

bit-and

2

bit-or

3

bit-xor

4

bit-not

pqp&qp | qp ^ q
00000
01011
11110
10011
```(ns clojure.examples.hello
(:gen-class))

;; This program displays Hello Learnfk
(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```

## 相关推荐

Vim 实用技巧必知必会 -〔吴咏炜 - 〕

Redis核心技术与实战 -〔蒋德钧 - 〕