C语言及程序设计进阶例程-32 位运算及其应用

来源:互联网 发布:dj小可网络电音歌曲 编辑:程序博客网 时间:2024/05/21 16:56

贺老师教学链接 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");}
0 0
原创粉丝点击