10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ビット演算 [1]:特定のビットの状態のみを取り出す

Last updated at Posted at 2014-09-07

算術論理積(&)は、1 & 1 の場合のみ 1 で他は 0 を返す。
(1&0 = 0, 0&1 = 0, 0&0 = 0)
見方を変えると x & 1 = 1, x & 0 = 0 となるので、
取り出したい場所のビットを1にした mask と、調べたい変数との算術論理積演算を行うと、目的とする場所のビットのみを取り出すことが出来るぞ。

例えば、変数 x の下位4bitのみを取り出したい場合は、以下のように記述する。

x & 0x0f

10
9
2

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
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?