按位与、按位或操作结果

内容纲要

二进制数,
任何数与 N 个 1 进行按位与,结果等于自身;或者与 N 个 0 进行按位或,结果等于自身。
任何数与 N 个 0 进行按位与,结果是 N 个 0;任何数与 N 个 1 进行按位或,结果是 N 个 1;

与 N 个 1 按位与。
1 0 1 0 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 1 1 0 1 1 1 1

可以通过此特性来获取某几位上的数。

获取某几位上的数字

例如获取一个 int 类型 8-11 位的数据。
C#版

            int num, mark;          
            Console.WriteLine("输入一个整数(大于256)");
            num = Convert.ToInt32(Console.ReadLine());
            num = num >> 8;
            mark = ~(~0 << 4);    // 构造 00000000 00000000 00000000 00001111
            // mark = 0b00000000_00000000_00000000_00001111;
            num = num & mark;
            Console.WriteLine(num);
            Console.ReadKey();

C 语言

#include <stdio.h>
int main()
{
    printf( "输入数字(256以上)" );
    int num;
    int mark;
    scanf( "%d", &num );
    num = num >> 8; /* 移动8位,使8-11变成0-4位 */
    mark    = num & ~(0 << 4);
    printf( "result = 0x%x", mark );
    return(0);
}

输出一个数的二进制

原理是,0或1 跟 1 的按位与,等于自身,跟 0 的按位与等于 0 。
任意输入一个正整数,输出这个数的二进制。
C语言版本

#include <stdio.h>
int main()
{
    printf( "输入数字(256以上)" );
    int num;
    int mark;
    printf( "%d", '1' );
    scanf( "%d", &num );
    mark = 1 << 31;

    int i = 0;
    for ( i = 0; i <= 31; i++ )
    {
        printf( "%c", num & mark ? '1' : '0' ); /* 结果要么大于0,要么等于0 */
        /* putchar( num & mark ? '1' : '0' ); / * 强制转化为8位变量(低八位) * / */
        num = num << 1;
        if ( (i + 1) % 8 == 0 )
            putchar( ',' );
    }
    putwchar( 'B' );
    return(0);
}

C# 版本

            uint num;
            int mark;
            Console.WriteLine("输入一个整数(大于256)");
            num = Convert.ToUInt32(Console.ReadLine());
            mark = 1 << 31;
            string str = string.Empty;

            for (int i = 0; i < 31; i++)
            {
                Console.Write((num & mark) == 0 ? '0' : '1'); // 每一步的结果,首位要么为1,要么为0,
                                                              // 结果要么大于0,要么等于0
                num = num << 1;
                if ((i + 1) % 8 == 0)
                    Console.Write(',');
            }
            Console.Write("B");
            Console.ReadKey();
点赞
  1. sinequan说道:

    I precisely had to appreciate you all over again. I'm not certain the things I might have followed in the absence of the actual information documented by you directly on such field. It became a real distressing crisis for me personally, but encountering a new professional form you managed it forced me to jump for delight. I'm just happier for the guidance and even have high hopes you find out what a powerful job you're doing instructing most people through your blog post. I am sure you've never encountered all of us.

  2. My spouse and i have been really joyous that Jordan managed to do his reports through your precious recommendations he had through the web pages. It is now and again perplexing to just find yourself handing out secrets and techniques which people today may have been selling. So we know we need the writer to give thanks to for that. The most important illustrations you have made, the simple web site navigation, the relationships you will help to instill - it's got most overwhelming, and it's really leading our son in addition to the family reason why the situation is exciting, which is really mandatory. Thank you for the whole lot!

  3. I am only writing to make you understand what a really good experience my wife's princess obtained viewing your webblog. She noticed some things, which included what it is like to possess an ideal teaching spirit to make others with no trouble learn about specific tortuous things. You undoubtedly surpassed readers' expected results. Thanks for providing those productive, trusted, informative as well as unique tips on your topic to Mary.

发表评论

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!