2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ビット演算とBitwiseについて理解する

Posted at

はじめに

ビット演算の理解とelixirのライブラリであるBitwiseを使った実装ができるようになることを目指す

ビット演算とは?

ビット演算は、データを2進数で表して演算するものです。
ビット演算には、「ビット単位の論理演算」と「ビット単位のシフト演算」があります。
ビットは、「2進数の桁」という意味です。ビット単位の論理演算は、2つの2進数のデータで桁ごとに論理演算を行います。

論理回路については以下を参照

Bitwiseとは?

A set of functions that perform calculations on bits.

訳:ビットに対して計算を実行する関数のセット。

実際にLivebook上で検証をしてみる

Bitwise.band(7, 4)
> 4

Bitwise.band(9, 3)
> 1

bandは引数のビットごとのANDを計算する

Bitwise.band(7, 4)を読み解く

7を2進数で表現すると
111となる
4を2進数で表現すると
100となる
この2つの数値を比較しビットごとのANDを計算すると
100となる
100は10進数に直すと4となるため4が出力される

同様にBitwise.band(9, 3)を読み解く

9を2進数で表現すると
10001となる
3を2進数で表現すると
11となる
ビットごとのANDを計算すると
1となる
1を10進数に直すと1になるため1が出力される

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?