0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[tips][cpp] bit shift

Last updated at Posted at 2024-10-05

bitシフト

right shift

例 右に1ビットシフト

int n_right = n >> 1;
cout << std::bitset<8>(n_right) << endl;
00000100

1回シフト

int n_right1 = n >> 1;
std::bitset<8>(n_right1)
// 00000100

2回シフト

int n_right2 = n >> 2;
std::bitset<8>(n_right2)
00000010
int n_right3 = n >> 2;
std::bitset<8>(n_right3)
00000010

left shift

例 左に1ビットシフト

int n_left = n << 1;
cout << std::bitset<8>(n_left) << endl;
00010000

例 左に3ビットシフト

int n_left3 = n << 3;
cout << std::bitset<8>(n_left3) << endl;
01000000

右からm番目のフラグが立っているか

1000
int n = 8;
int m = 3;
cout << "Is 3th bit flag 1? It is " << (n >> m & 1) << endl;
Is 3th bit flag 1? It is 1
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?