191 numbers of 1 Bits, 191 bits

Source: Internet
Author: User

191 numbers of 1 Bits, 191 bits

Recently I want to refresh the LeetCode to practice data structure algorithms and so on. Let's start with the water question first.

The question is as follows:

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

For example, the 32-bit integer '11' has binary representation00000000000000000000000000001011, So the function shocould return 3.

This is probably the number of 32-bit integer numbers, and the number of 1 is also the weight of the Hamming code, because I am reviewing the basic knowledge of C recently, I read a book about bitwise operations in the C and pointer sections of operators. Here I will explain this topic. Classic is really a classic! It can also be seen that some large companies also like to find the original questions or ideas from these classic books.

Solution is given below:

 1 class Solution { 2 public: 3     int hammingWeight(uint32_t n) { 4         int ones=0; 5         for(;n!=0;n>>=1){ 6             if((n&1)!=0){ 7                 ones++; 8             } 9         }10         return ones;11     }12 };

In short, it is a loop that cyclically determines whether there is another 1 in the current number.

It is worth noting that (1) n & 1 determines the percentile if it is 0 n> = 1. If it is 1 ones plus one, it indicates one digit plus one.

(2) n> = 1 This is a right shift operation. Let's review the shift operation.

First, move left

The left-shift operation removes n digits from the left and uses 0 to fill in the trailing vacant positions.

The second is the right shift operation.

The right shifting operation is divided into logical right shifting and arithmetic right shifting. The logical right shifting operation is similar to the left shifting operation, but the left vacant position is supplemented by 0 after the right shifting operation is performed; if the arithmetic shift is right, the vacancy position after the right shift is n digits must be supplemented by the sign bit. If it is a negative number, it will be supplemented by 1. If it is a positive number, it will be supplemented by 0.

In the end, the right-shift method is determined by your compiler.

Of course there is a small change to this question:

 1 class Solution { 2 public: 3     int hammingWeight(uint32_t n) { 4         int ones=0; 5         for(;n!=0;n>>=1){ 6             if((n%2)!=0){ 7                 ones++; 8             } 9         }10         return ones;11     }12 };

Just changed the condition judgment to (n % 2 )! = 0. If the effect is the same, it indicates whether the current value is 1.

 

By the way, is java running so slowly? I do not believe

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.