贺老师教学链接 C语言及程序设计进阶 本课讲解
位运算
#include <stdio.h> int main() { unsigned short int n = 3; int i; for(i=0; i<10; i++) { printf("%d\n",n); n<<=1; //n=n<<1; } return 0; }按位与、或、异或
#include <stdio.h> int main() { unsigned short m = 0x3A, n = 0x02f, t; t = ~m; printf("%x\n", t); t = m & n; printf("%x\n", t); t = m | n; printf("%x\n", t); t = m ^ n; printf("%x\n", t); return 0; }例:跑马灯
#include <stdio.h> #include <windows.h> void show(int m); int main() { unsigned int x = 0x1, y = 0x10; while(1) { show(x); x = (x << 2) | (x >> (30)); show(y); y = (y << 2) | (y >> (30)); Sleep(50); system("cls"); } return 0; } void show(int m) { int i; for (i=0; i<32; ++i) { if (m%2==0) printf("○"); else printf("●"); m/=2; } printf("\n"); }