Leetcode:number of 1 Bits-the Hamming weight of integers

Source: Internet
Author: User
Tags bitwise operators

1. Title

Number of 1 Bits (integer hamming weight)

2. Address of the topic

https://leetcode.com/problems/number-of-1-bits/

3. Topic content

English: Write a function that takes an unsigned integer and returns the number of ' 1 ' bits it has.

English: Write a function, enter an unsigned integer, return the number of bits where the value is 1 (this value is also known as the number Hamming weight)

For example, the 32-bit integer number 11 can be represented by the binary number "00000000000000000000000000001011", its Hamming weight is 3.

4. Methods of Solving problems

This problem can be solved by using bit operations. Each loop shifts the number right one bit, then observes whether the number after the shift is odd or even, and if it is odd, the last one is 1, otherwise the last one is 0. Ends the loop when the last entered number passes through a constant right shift to 0 o'clock.

The code I wrote at the beginning was this:

/** *  function Description:leetcode 191 - number of 1 bits *  Developer:tsybius2014 *  Development time: August 12, 2015  */public class Solution {     /**     *  Figure Hamming weight      *  @param  n  Digital      *  @return   Hamming weight      */     public int hammingweight (int n)  {                 int counter = 0;         while  (n != 0)         {             if  (n % 2 != 0)  {                 counter++;             }            n =  n >> 1;        }         return counter;    }}

However, this "seemingly no problem" code will burst after the submission of the tle (time Limit exceeded) error.

Then I looked for a reason and found that the input was 2147483648 when the decision was wrong, but the input exceeded the upper limit of the integer in the Java language. Later I looked at the topics in other languages, such as C + +:

Class Solution {Public:int hammingweight (uint32_t n) {}};

The original problem is that the input may be unsigned numbers! When the input is 2147483648, Java will determine the number of bits-1, and the right to move the symbol >> in the calculation will maintain the number of sign bit, that is, positive shift to the right of high 0, negative right shift high 1. Using this rule to move right causes the number to be continuously 1 during the right shift so that the loop never stops! Therefore, if the input is negative, you should also keep the high fill 0 when you move right, and the bitwise operator >>> can help us solve this problem.

A section of the Java code that can be AC is as follows:

/** *  function Description:leetcode 191 - number of 1 bits *  Developer:tsybius2014 *  Development time: August 12, 2015  */public class Solution {     /**     *  Figure Hamming weight      *  @param  n  Digital      *  @return   Hamming weight      */     public int hammingweight (int n)  {                 int counter = 0;         while  (n != 0)         {             if  (n % 2 != 0)  {                 counter++;             }            n =  n >>> 1;        }         return counter;    }}

Attached: Three bitwise operators for shift in Java:>>, <<, >>>

    1. >> is signed right shift, negative high 1, positive complement 0

    2. << left shift no matter negative or positive, always 0 in low position

    3. >>> the right shift without a symbol, whether negative or positive, high 0

END

Leetcode:number of 1 Bits-the Hamming weight of integers

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.