Number of 1 bits (for 32 binary digits 1)

Source: Internet
Author: User

<span style= "FONT-SIZE:24PX;" ></span>

1. Title:

Write a function that takes an unsigned integer and returns the number of ' 1 ' bits it has (also known as the Hamming weigh T).

For example, the 32-bit integer ' One ' 00000000000000000000000000001011 has a binary representation, so the function should return 3.

2. Familiar with bit operations:

1, & (bitwise AND) Conceptually, is to participate in the operation of each of the two components to do the logic and operation, if both are true (equals 1), then the result is true (equals 1). Otherwise they are false (equal to 0).
i.e.: 1 & 1 = 1, 1&0 = 0, 0&1 = 0, 0&0 = 0
Let's take a look at the example of a 8-bit binary in the first place:
7&8 = 0000 0111 & 0000 1000 = 0000 0000 = 0
7&6 = 0000 0111 & 0000 0110 = 0000 0110 = 6

2, | (bitwise OR) is the logic or operation of each of the components that participate in the operation, that is, when both are false (0), it is False (0), otherwise they are true.
namely: 0|0 = 0, 1|0 = 1, 0|1 = 1, 1|1 = 1
Take a look at the 8-bit binary example:
7|8 = 0000 0111 | 0000 1000 = 0000 1111 = 15
7|6 = 0000 0111 | 0000 0110 = 0000 0111 = 7

3, ^ (bitwise XOR) is the participation of each component of the operation to do the XOR or operation, that is, the same is false, the difference is true.
namely: 0|0 = 0, 1|0 = 1, 0|1 = 1, 1|1 = 0
Look at the following example:
7^8 = 0000 0111 ^ 0000 1000 = 0000 0111 = 7
7^6 = 0000 0111 ^ 0000 0100 = 0000 0011 = 3

4, ~ (bitwise negation) is the bits each to take the inverse operation, in short, 1 becomes 0, 0 becomes 1.
See examples directly:
~0000 = 0111 = 1111 1000 = 248

5 >> (Shift right) move the bits whole to the right.
7>>1 = 0000 0111 >> 1 = 0000 0011 = 3
7>>2 = 0000 0111 >> 2 = 0000 0001 = 1
The right shift here is equal to the number of n times except 2, and N is the right shift.

6 << (bitwise left) here is not the details, and the right shift opposite.


3. Understanding uint8_t,uint16_t,uint32_t , etc.

  • A. uint8_t , uint16_t , uint32_t are not new data types, they just use the typedef an alias for the type.
  • Using precompilation and typedef allows you to maintain your code most effectively. For the convenience of users, C99 standard C language hardware for us to define these types, we are assured that the use of it.

    According to the POSIX standard, the *_t type corresponding to the generic shaping is:
    1 bytes uint8_t
    2 bytes uint16_t
    4 bytes uint32_t
    8 bytes uint64_t

  • Use header file to include <stdint.h> (C99 standard, VC6 not supported)


4. Algorithm:
    • The simplest rough method: is the binary number and 1 and the operation, judge the last is not 1, and then move right one bit, until the number is moved.

<span style= "FONT-SIZE:24PX;" >int Hammingweight (uint32_t n) {int weight = 0;while (n) {if ((n & 1) = = 1) {weight++;} n = n >> 1;} return weight;} </span>
algorithm Description: Easy to think, but less efficient

    • Improvement: The efficiency of the last algorithm is low because each person should be compared, when there are for example 0x10000000, obviously a 1, is it necessary to compare so many times?
so we just want to find a non-zero bit to compare: using n& (n-1) each iteration always places the rightmost nonzero position zero, which can improve efficiency
<span style= "FONT-SIZE:24PX;" >int sparse_popcnt (unsigned int n) {int count = 0;while (n) {++count;n &= (n-1);} return count;} </span>


    • Other algorithms can be seen here by the Great God Summary
5. Specific implementation issues that may be encountered:
    • How to output decimal, octal, hexadecimal digits
      cout<< "DEC:" <<dec<<n<<endl;  Decimal cout<< "OCT:" <<oct<<n<<endl;//octal   cout<< "HEX:" <Note: After using a method such as 16 binary, you want to undo, or you will follow the hexadecimal output
    cout << hex<<n << Endl;    COUT.UNSETF (Ios::hex);

    • The binary number cannot be expressed in C + +, so do not attempt to write a 32-bit long binary number, so that it can recognize .... , use hex Bar
    • VS debugging when you want to see the value of 16 binary, select hexadecimal in the right-click value




Number of 1 bits (for 32 binary digits 1)

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.