Sword refers to Offer interview question 10 (Java edition): number of 1 in binary, and sword refers to offer

Source: Internet
Author: User

Sword refers to Offer interview question 10 (Java edition): number of 1 in binary, and sword refers to offer

Question: Please implement a function, input an integer, and output this number in binary format to represent the number of 1. For example, if 9 is expressed as binary 1001, two digits are 1. Therefore, if 9 is input, this function outputs 2.

1. solutions that may cause an endless loop

This is a basic interview question about binary and bitwise operations. The question is not very difficult. After the interviewer raises a question, we will quickly form a basic idea: first judge whether the rightmost one of the two certificates is 1. next, move the entered certificate to the right. At this time, the second digit from the right tree is moved to the last digit, and then judge whether it is 1. in this way, the entire integer is changed to 0. Now the question is how to judge whether the rightmost side of an integer is 1. This is very simple. You only need to bitwise the integer and 1 to see if the result is 0. 1. All digits except the rightmost one are 0. Based on this idea, we will soon write this Code:

Int numberOf1 (int n)

{

Int count = 0;

While (n! = 0 ){

If (n & 1)

Count ++;

N = n> 1;

}

Return count;

}

After reading the code, the interviewer may ask: It is equivalent to dividing the right digit of the certificate by two and dividing the integer by two. Can I change the right digit to 2 in the above Code? The answer is no.Division is much less efficient than shift operations. In actual programming, shift operations should be used as much as possible to replace multiplication and division.

The interviewer will ask the second question: if the above function is input with a negative number, such as 0x80000000, what will happen during running? When the negative 0x40000000 is shifted to the right, it is not simply to change the first of the highest bits to the second to 0 x, but 0xC0000000. this is because it is a negative number before the shift, and the shift is still a negative number, so the highest bit after the shift will be set to 1. if you keep performing the right shift operation, this number will be programmed to 0xFFFFFFFF andInto an endless loop.

2. Conventional solution:

To avoid endless loops, we can shift the number n from the right. first, perform operations on n and 1 to determine whether the nth percentile is 1. next, shift 1 to the left to get 2, and then perform the operation with n to determine whether the next low position of n is 1 .... In this way, we can judge whether one of n is 1. Based on this idea, we can write this Code:

Int number1 (int n ){

Int count = 0;

Int flag = 1;

While (flag! = 0 ){

If (n & flag)

Count ++;

Flag = flag <1;

}

Return count;

}

In this solution, the number of cycles is equal to the number of digits in the binary, and the 32-bit integer needs to be cyclically 32 times. Next we will introduce an algorithm where the integer contains a few 1 and only loops several times.

3. algorithms that can surprise the interviewer.

In our analysis, we subtract 1 from an integer, and then perform an operation with the original integer to convert the rightmost 1 of the integer to 0. the binary value of an integer indicates the number of 1 operations. Based on this idea, we can write such code:

/*** Question: implement a function, input an integer, and output the number of 1 in the Binary Expression of this number. * For example, if the binary value of 9 is 1001, two digits are 1. therefore, if 9 is input, the function outputs 2. */package swordForOffer;/*** @ author JInShuangQi * July 30, 2015 */public class E10NumberOf1InBinary {public int numberOf1 (int num) {int count = 0; while (num! = 0) {count ++; num = num & (num-1);} return count;} public static void main (String [] args) {E10NumberOf1InBinary test = new E10NumberOf1InBinary (); System. out. println (test. numberOf1 (9 ));}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.