1. Basic type
The range represented by a 32-bit signed int type integer:-2^31~2^31-1
The range represented by a 32-bit unsigned int type integer: 0~2^32-1
(I'm very speechless, I haven't figured it out... always wrong)
The range represented by an 8-bit signed char type integer:-2^7~2^7-1
The range represented by an 8-bit unsigned char type integer:0~2^8-1
The meanings of HEX, DEC, OCT and BIN in the win10 programmer's calculator:
HEX, Hexadecimal, hexadecimal.
DEC, Decimal, decimal.
OCT, Octal, octal.
BIN, Binary, binary.
INT_MAX:2^31-1 int占4个字节 32位二进制
32位第一位代表符号位
1111 1111 1111 1111 = 2^16(有多少连续1,幂就是多少) -1 = 65535
0111 1111 1111 1111 1111 1111 1111 1111 = 2^31-1=2147483647 10位数
1000 0000 0000 0000 0000 0000 0000 0000 = -2^31=-2147483648
1111 1111 1111 1111 1111 1111 1111 1111 = -1
0000 0000 0000 0000 0000 0000 0000 0000 = 0
2. Bit operations
1<<2:1的二进制是00000001
“<<”意思是将二进制编码向左移动2位并将空位补0,即00000001转化为00000100=4。
同理,“1>>n”为将二进制码向右移动n位。
1<<i:是将1左移i位,即第i位为1,其余位为0;
n&(1<<i):
是将左移i位的1与n进行按位与,即为保留n的第i位,其余位置零。
如果n第i位为0,则n&(1<<i)的值为0。否则不为0。
常用if(n&(1<<i)==0)用于判断n的第i位是否为0。
0&0=0; 0&1=0; 1&0=0; 1&1=1;
1、按位与运算符(&):0&0=0;0&1=0;1&0=0;1&1=1;
2、按位或运算符(|):0|0=0;0|1=1;1|0=1;1|1=1;
3、取反运算符(~):~1=0;~0=1;
4、异或运算符(^):相同为0,相异为1
0^0=0;0^1=1;1^0=1;1^1=0;
&=例:a&=b相当于a=a & b、|=例:a|=b相当于a=a|b
>>=例:a>>=b相当于a=a>>b、<<=例:a<<=b相当于a=a<<b
^=例:a^=b相当于a=a^b
x &= x-1 可以将x的二进制中的 最低位的1置0;x=x&(x−1)
__builtin_popcount() 返回二进制中1的个数
int d= x&(−x) 可以获得 x的二进制表示中的最低位的1的位置
int k= __builtin_ctz(d) 返回二进制末尾后0的个数