Skip to content

Instantly share code, notes, and snippets.

@Privilger
Last active March 4, 2020 06:50
Show Gist options
  • Save Privilger/81c95ca4eb2f2306d8a1cd8c9f3212ef to your computer and use it in GitHub Desktop.
Save Privilger/81c95ca4eb2f2306d8a1cd8c9f3212ef to your computer and use it in GitHub Desktop.
整数n的二进制运算操作
/*
整数n的二进制表示下第k为是几
右移k位 然后 与运算1
*/
n >> k & 1;
/*
x -> 表示整数n的二进制,但实际使用的时候, 传入n就行
lowbit(x): 返回x的最后一位1
// 二进制表示下
lowbit(1010) -> 10
lowbit(10100) -> 100
用处: 统计x里面1的个数
*/
inline int lowbit(int x){
return x & -x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment