The binary value of an integer indicates the number of values in 1.

Source: Internet
Author: User

The methods that can be thought of are provided in the book C and pointer.All of the following are non-negative integers.

/* This method checks whether the rightmost binary of a number is 1 in a loop (if the number can be divisible by 2 ). After each loop, this number shifts one digit to the right. Therefore, each bit in the binary representation of the number is traversed. */INT count_one_bits1 (INT value) {int count; For (COUNT = 0; value! = 0; value >>= 1) if (Value % 2! = 0) Count ++; return count;}/* is similar to the preceding method. It also shifts the number right one bit each time and traverses each bit in the binary representation of the number. However, here we perform and calculate the number and 1. In this way, if the result is not zero, the rightmost bit of the binary representation of the number is 1. */INT count_one_bits2 (INT value) {int count; For (COUNT = 0; value! = 0; value >>=1) {If (Value & 1 )! = 0) Count ++;} return count ;}

The following introduces another method, which can calculate the number of 1 in the binary representation by comparing the number of times.

First, perform the following analysis:

When a number value is the power of n (n> 0) of 2, the Binary Expression of the number must contain only one 1 in the highest bit, then the binary form of value-1 must be a number with the highest bit and other bits being 1. Therefore

The result of value & (value-1) must be 0. The problem arises:

We can use this method to determine whether the number value is the N power of 2:

! (Value & (value-1) True indicates that the number is the N power of 2, and false indicates that the number is not the N power of 2.


Therefore, if a certain number value satisfies Value & (value-1) = 0, it is proved that this number contains only one 1. Take a look at the following functions:

int count_one_bits3(int value) {int count = 0;while (value) {count++;value = value & (value - 1);int v1 = value;cout << value << " ";}cout << endl;return count;}

When the while loop ends, the condition is that the value is 0, that is, when the value is the N power of 2, the next loop ends, because the n-power binary representation of 2 contains only one 1. In a loop, value = Value & (value-1) causes one fewer value 1 in the binary representation of value after each loop. This method has fewer cycles than the first two methods.

For example, if the input value is 8888, we can see that method 1 executes the while loop 14 times and method 3 executes the while loop 6 times. Method 3: after each loop, the number of values in the binary value is one less than one.


The binary value of an integer indicates the number of values in 1.

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.