6
3

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.

ビット演算 [2]:特定のビットを1にする

Last updated at Posted at 2014-09-07

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

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

x |= 0x0f;

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?