day2
1、不同的进制
#include<stdio.h> int main() { int a = 10; //10进制 int b = 010; //8进制 int c = 0x10; //16进制 int d = 0b1010; //2进制 printf("%d %d %d %d\n", a, b, c, d); printf("%o %o %o %o\n", a, b, c, d); printf("%x %x %x %x\n", a, b, c, d); return 0; }
2、按位与、异或、逆反进行运算
#include<stdio.h> int main() { char a = 67; char b = 0x54; char c = 0; c = a & b; printf("%x \n", c); c = a | b; printf("%x \n", c); c = a ~ b; printf("%x \n", c); return 0; }
3、位运算适合char,short,int,long int,long long;
不适合float,double,long double。
4、左移运算
#include<stdio.h> int main() { char a = 5; a = a << 1; printf("%x\n", a); a = a << 1; printf("%x\n", a); a = a << 1; printf("%x\n", a); a = a << 1; printf("%x\n", a); return 0; }
5、按要求保留前四位不变,将后四位的值作出变化
#include<stdio.h> int main() { char a = 0x20; a = a | 0x05; char ch = a; printf("%d\n", a); return 0; }
6、一个例子
#include<stdio.h> int Get1Bit(int x) int sum = 0; while (x != 0) { if (x & 0x01)//判断 { sum += 1; } x = x >> 1; } return sum; } int main() { int x = 23; int sum = Get1Bit(x); printf("%d\n", sum); return 0; }