您的位置:首页 > 编程语言 > C语言/C++

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

2015-06-22 22:42 465 查看
贺老师教学链接 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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: