Number of 1 in binary

Source: Internet
Author: User

Title Description: Implement a function, enter an integer, and output the number of 1 in the binary representation.

For example: 9 indicates that binary is 1001, and 2 bits are 1. So if you enter 9, the function outputs 2.


First look at a wrong solution: 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0015.gif "alt=" J_0015.gif "/>

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/83/C8/wKiom1d8a1vwNbUMAADiIv5VDLs700.jpg "title=" 1.jpg " alt= "Wkiom1d8a1vwnbumaadiiv5vdls700.jpg"/>

int NumberOf1 (int n) {int count = 0;        while (n) {if (n & 1) count + +;    n = n >> 1; } return count;}

Note: when using multiplication or division operations, use shift operations as much as possible instead, because the efficiency of the shift operation is much higher than the multiplication method!

The problem with this solution is that you enter a positive number, and the result is fine, but what happens if you enter a negative number?

We know that the highest number of negative binary is 1, when the right shift operation, the highest bit to fill the sign bit is 1, so each right shift one, the highest bit of 1, the final figure will become 0xFFFFFFFF, and into a dead loop.

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/83/C8/wKiom1d8bpTT19zOAAANC53rHBA485.jpg "title=" 2.jpg " Width= "height=" border= "0" hspace= "0" vspace= "0" style= "width:700px;height:48px;" alt= " Wkiom1d8bptt19zoaaanc53rhba485.jpg "/>

Conventional Solutions

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/83/C8/wKiom1d8cerw60fqAAAafKkvY7A798.jpg "style=" float: none; "title=" 3.1.jpg "alt=" Wkiom1d8cerw60fqaaaafkkvy7a798.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/83/C7/wKioL1d8cfDx50DJAABLLqzL5AM908.jpg "style=" float: none; "title=" 3.2.jpg "alt=" Wkiol1d8cfdx50djaabllqzl5am908.jpg "/>

int numberof1_solution1 (int n) {int count = 0;    unsigned int flag = 1;        while (flag) {if (n & Flag) Count + +;    Flag = Flag << 1; } return count;}

The number of loops in this solution is equal to the number of digits in the integer binary, and 32-bit integers need to be cycled 32 times.

High-efficiency solution

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/83/C8/wKiom1d8cv-jpf15AAF4dC7bj5A589.jpg "style=" float: none; "title=" 4.1.jpg "alt=" Wkiom1d8cv-jpf15aaf4dc7bj5a589.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/83/C8/wKiom1d8cwHCuhqFAABjj_fO3Eo006.jpg "style=" float: none; "title=" 4.2.jpg "alt=" Wkiom1d8cwhcuhqfaabjj_fo3eo006.jpg "/>

int NumberOf1 (int n) {int count = 0;        while (n) {+ + count;    n = (n-1) & N; } return count;}

The solution has several 1 in integers and only needs to be looped several times.

Related Topics 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0028.gif "alt=" J_0028.gif "/>

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/83/C7/wKioL1d8ddbj_5NbAAC_jJFfd2w394.jpg "title=" 5.jpg " alt= "Wkiol1d8ddbj_5nbaac_jjffd2w394.jpg"/>

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Ya Black ' , ' Microsoft Yahei '; font-size:36px;white-space:normal; "alt=" J_0063.gif "/>650" this.width=650; "src=" http:// Img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px ; white-space:normal; "alt=" J_0063.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif " Style= "Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px;white-space:normal; alt= j_ 0063.gif "/>650) this.width=650;" src= "Http://img.baidu.com/hi/jx2/j_0063.gif" style= "Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px;white-space:normal; ' alt= ' j_0063.gif '/>650 ' this.width=650 ; "Src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Black ', ' Microsoft Yahei '; FONT-SIZE:36PX, "alt=" J_0063.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/jx2/j_0063.giF "style=" Color:rgb (0,112,192) font-family: ' Microsoft Black ', ' Microsoft Yahei '; font-size:36px; ' alt= ' j_0063.gif '/>650 ' this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Ya Black ', ' Microsoft Yahei '; font-size:36px;white-space:normal; "alt=" J_0063.gif "/>650" this.width=650; "src=" http:// Img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px , "alt=" J_0063.gif "/>650) this.width=650; src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb ( 0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px; alt= ' j_0063.gif '/>650 ' this.width=650; src= Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; Font-size:36px;white-space:normal, "alt=" J_0063.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/jx2/j _0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Black ', ' Microsoft Yahei '; font-size:36px; ' alt= ' j_0063.gif ' />650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft ya Black ', ' Microsoft Yahei '; font-size:36px; "alt=" J_0063.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/ Jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; Font-size:36px;white-space: Normal, "alt=" J_0063.gif "/>650" this.width=650; src= "Http://img.baidu.com/hi/jx2/j_0063.gif" style= "Color:rgb ( 0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px; alt= ' j_0063.gif '/>650 ' this.width=650; src= Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; Font-size:36px;white-space:normal, "alt=" J_0063.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/jx2/j _0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Black ', ' Microsoft Yahei '; font-size:36px; ' alt= ' j_0063.gif '/ >650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px; ' alt= ' j_0063.gif '/>650 ' this.width=650; ' src= ' Img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px ; white-space:normal; "alt=" J_0063.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif " Style= "Color:rgb (0,112,192); font-family: ' Microsoft Black ', ' Microsoft Yahei '; font-size:36px;" alt= "J_0063.gif"/>650) this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Ya Black ', ' Microsoft Yahei '; font-size:36px; "alt=" J_0063.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/jx2/j_ 0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px;white-space:normal; " alt= "J_0063.gif"/>650) this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb ( 0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px;white-space:normal; ' alt= ' j_0063.gif '/>650 ' this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Ya Black ', ' Microsoft Yahei '; font-size:36px;white-space:normal; "alt=" J_0063.gif "/>650" this.width=650; "src=" http:// Img.baidu.com/hi/jx2/j_0063.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px ; white-space:normal; "alt=" J_0063.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif " Style= "Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:36px;white-space:normal; alt= j_ 0063.gif "/>

This article is from the "11408774" blog, please be sure to keep this source http://11418774.blog.51cto.com/11408774/1796584

Number of 1 in binary

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.