位运算()

位运算的基本操作

1.求n的二进制表示中第k位是几

(1)先把第k位移到最后一位 n >> k;
(2)看个位是几 n & 1;

#include <bits/stdc++.h>

using namespace std;

int main(){
	int n = 10;
	
	for(int k = 3; k >= 0; k--) cout << (n >> k & 1);
	
	return 0;
}
输入:10
输出:1010

2.返回n的最后一位1

lowbit(x):返回x的最后一位1
x = 1010,lowbit(x) = 10;
x = 101000, lowbit(x) = 1000;

————————

位运算的基本操作

1.求n的二进制表示中第k位是几

(1)先把第k位移到最后一位 n >> k;
(2)看个位是几 n & 1;

#include <bits/stdc++.h>

using namespace std;

int main(){
	int n = 10;
	
	for(int k = 3; k >= 0; k--) cout << (n >> k & 1);
	
	return 0;
}
输入:10
输出:1010

2.返回n的最后一位1

lowbit(x):返回x的最后一位1
x = 1010,lowbit(x) = 10;
x = 101000, lowbit(x) = 1000;