Elixir-example-bitwise

提供:Dev Guides
移動先:案内検索

Elixir-ビット演算子

ビットごとの演算子はビットに作用し、ビットごとの操作を実行します。 Elixirはパッケージ Bitwise の一部としてビット単位モジュールを提供します。したがって、これらを使用するには、ビット単位モジュールを_使用_する必要があります。 それを使用するには、シェルで次のコマンドを入力します-

use Bitwise

次の例では、Aを5、Bを6と仮定します-

Operator Description Example
&&& Bitwise and operator copies a bit to result if it exists in both operands. A &&& B will give 4
Bitwise or operator copies a bit to result if it exists in either operand. A
B will give 7
>>> Bitwise right shift operator shifts first operand bits to the right by the number specified in second operand. A >>> B will give 0
<<< Bitwise left shift operator shifts first operand bits to the left by the number specified in second operand. A <<< B will give 320
^ Bitwise XOR operator copies a bit to result only if it is different on both operands. A ^ B will give 3
~ Unary bitwise not inverts the bits on the given number. ~A will give -6

次のコードを試して、Elixirのすべての算術演算子を理解してください。

a = 5
b = 6

use Bitwise

IO.puts("a &&& b " <> to_string(a &&& b))

IO.puts("a ||| b " <> to_string(a ||| b))

IO.puts("a >>> b " <> to_string(a >>> b))

IO.puts("a <<< b" <> to_string(a <<< b))

IO.puts("a ^^^ b " <> to_string(a ^^^ b))

IO.puts("~~~a " <> to_string(~~~a))

上記のプログラムは、次の結果を生成します-

a &&& b 4
a ||| b 7
a >>> b 0
a <<< b 320
a ^^^ b 3
~~~a -6