You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
/**
阅读材料:
https://blog.csdn.net/weixin_41183791/article/details/84886700
1.去掉最后一位 x>>1
2.在最后加一个0 x<<1
3.在最后加一个1 (x<<1)+1
4.把最后一位变成1 x|1
5.把最后一位变成0 (x|1)-1
6.最后一位取反 x^1
7.把右数第k位变成1 x|(1<<(k-1))
8.把右数第k位变成0 x&~(1<<(k-1))
9.右数第k位取反 x^(1<<(k-1))
10.取末三位 x&7
11.取第k位 x&(1<<(k-1))
12.取右数第k位 x>>(k-1)&1 //右移k位再与1一下就知道这位是1是0了
13.把右数第k位变成1 x|(1<<k-1)
14.末k位取反 x^(1<<k-1)
15.把右边连续的1变成0 x&(x+1)
16.把右起第一个0变成1 x|(x+1)
17.把右边连续的0变成1 1|(x-1)
18.取右边连续的1 (x^(x+1))>>1
19.去掉右起第一个1的左边 x&(x^(x-1))
习题答案:
1、奇偶性判断
a & 1 =1 奇数, a & 1 =0 偶数
2、右边数第k位变成0 x&~(1<<(k-1))
3、从右边数末k位取反 x^(1<<k-1)
4、判断从右边数第k位是1还是0 x>>(k-1)&1
5、将从右边数的第一个0变成1 x|(x+1)
*/
return 0;
}