Sword Point of Offer (Java version): 1 of the number of binary

Source: Internet
Author: User

Title: Please implement a function, enter an integer, output the number of binary representation of 1. For example, 9 is represented as a binary is 1001, and 2 bits is 1, so if you enter 9, the function outputs 2.

1, may cause the death cycle the solution

This is a very basic examination of binary and bit operations. The problem is not very difficult, after the interviewer ask questions, We quickly formed a basic idea: First judge the certificate binary representation of the rightmost bit is not 1. Then the input certificate is moved to the right, at this time the second position from the right tree is moved to the last one, and then judge is not 1. This does not move one, knowing that the whole integer becomes 0. Now the question becomes how to judge the rightmost of an integer is not 1. This is very simple, as long as the integers and 1 to do bits and arithmetic to see the result is not 0 to know. 1 All the bits except the one on the far right are 0. Based on this idea, we quickly 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: "To move the certificate to the right of one and the integer divided by 2 is mathematically equivalent, then the above code can shift right to divide by 2?" The answer is in the negative. because the efficiency of division is much lower than the shift operation, in practical programming we should replace the multiplication method with the shift operation as much as possible.

The interviewer will ask the second question: What happens when the above function enters a negative number, such as 0x80000000, when it is running? When you move the negative 0x80000000 right one bit, it is not simply to move the top bit 1 to the second position into the 0x40000000, It's 0xc0000000. This is because the shift is a negative number, and the shift is still guaranteed to be negative, so the highest bit after the shift is set to 1. If you do a right shift operation, the figure will eventually be programmed 0xFFFFFFFF and trapped in a dead loop.

2. Conventional solution:

In order to avoid a dead loop, we can not shift the input number n to the right. First, the N and 1 are done with the operation to determine whether the lowest bit of n is 1. Then 1 left one to get 2, and then n to do with the operation, you can determine the second low of n is not 1 .... This way, you can judge whether one of N is 1. Based on this idea, we can write code like this:

int number1 (int n) {

int count = 0;

int flag= 1;

while (flag! =0) {

if (n& flag)

count++;

Flag =flag <<1;

}

return count;

}

The number of cycles in this solution is equal to the number of bits in the binary, 32-bit integers need to be cycled 32 times, and here we introduce an algorithm that has several 1 loops in integers.

3, can bring surprise to the interviewer algorithm.

Our analysis is: the integer minus 1, and then the original integer and the operation, the integer will be the rightmost one 1 to 0. Then how many operations can be performed by the number of 1 in the binary representation of an integer. Based on this idea, we can write a code like this:

/** * Title: Implement a function, enter an integer, output the number of 1 in the binary representation. * For example, change 9 to binary is 1001, 2 bits is 1. So if you enter 9, the function outputs 2.  */package swordforoffer;/** * @author Jinshuangqi * *         July 30, 2015 */public class E10numberof1inbinary {public int numbe ROF1 (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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sword Point of Offer (Java version): 1 of the number of binary

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.