The rookie series of the C + + Classic questions (eight)

Source: Internet
Author: User

calculating binary in binary 1 the number

topic: bit arithmetic programming is seldom encountered, but it's also a heavy one, just a point, a more common topic is to calculate the binary representation of a number. 1 the number of.

Analysis: A 1 is not 0, our idea is a one of the operation, we quickly think of the following practices:

int countBit1 (int val) {    Register int count = 0;    while (Val)    {        if (1 & val)        {            ++count;        }        Val >>= 1;    }    return count;}

A look at this implementation is good, look carefully there will be a big problem, that is, when val is negative, there will be a dead loop, Because the sign is the arithmetic shift , we will take into account the problem of the sign bit, so we have modified this, in exchange for a way of thinking:

int countBit2 (int val) {    Register int count = 0;    Register unsigned int flag = 1;    while (flag)    {        if (Val & flag)        {            ++count;        }        Flag <<= 1;    }    return count;}

Well, this implementation solves one of the above unsigned problems, but there is a drawback, that is , regardless of the number of 1 of the number of a few, have to cycle three times (sizeof (int) = the time ), This does not meet the efficiency requirements, so take a closer look at the implementation of one, the problem is that there is now a symbolic shift, we look at the strong system to convert signed to unsigned in the operation :

int countBit3 (int val) {    Register int count = 0;    Register unsigned int _val = val;    while (_val)    {        if (1 & _val)        {            ++count;        }        _val >>= 1;    }    return count;}

Well, good work, this implementation is OK, but for the pursuit of the perfect programmer, can not stop, we can have a few 1 on the loop several times, and, the circulation body has branch jump, which will affect performance, so I think:

For example: x = 1101 Number, we perform the following operations:

x–1 = 1100, the attentive person will find that when the lowest bit is 1, only change the most, when the bit is 0, the lowest bit of 1 and the left bit are reversed, so we can use N &= (n–1) to clear the right 1.Whyn &= (n–1)to clear the far right.1it? Because from the point of view of the binary,Nequivalent ton-1the lowest bit plus1. To give an example,8( +)= 7(0111)+ 1(0001), so8 & 7 =( +)&(0111)= 0(0000), clear the8on the far right.1(in fact, the most high1, because8In binary , there is only one1). Another example7(0111)= 6(0110)+ 1(0001), so7 & 6 =(0111)&(0110)= 6(0110), clear the7the binary representation of the rightmost1(i.e. the lowest bit1)

The implementation is as follows:

int countBit4 (int val) {    Register int count = 0;    while (Val)    {        ++count;        Val &= val-1;    }    return count;}
Of course there are other ways to do it, and I'm not going to list it here.

If there is a mistake welcome to point out, share please identify

Feel good words on the top one, feel good words on the step.

The rookie series of the C + + Classic questions (eight)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.