The beauty of programming 2.1 is the number of 1 in binary, And the beauty 2.1

Source: Internet
Author: User
Tags decimal to binary

The beauty of programming 2.1 is the number of 1 in binary, And the beauty 2.1

Recently, I have been reading algorithm books such as the beauty of programming. At the beginning, I started to look at the beauty of programming. I feel that it is too difficult and sometimes I am not willing to flip this book. However, after some time of practice, I also liked this book. The algorithms in this book involve many aspects, such as trees, linked lists, bitwise operations, arrays, and hash table applications.

Recently, my work has been almost busy. I have re-written the algorithm in the beauty of programming and recorded it here for future convenience.


Since the first question was written in 2.1, this question is not very difficult. First, the function declaration of this question is given:

/* 2.1 calculate the number of 1 in binary */int DutCountOf1InBin_1 (unsigned int); int DutCountOf1InBin_2 (unsigned int );

Here we provide two very common and very good algorithm implementations. The Code has been annotated, So I directly paste the Code:

/* Method 1 */int DutCountOf1InBin_1 (unsigned int v) {/* Number of 1 in binary */int count = 0; while (v) {++ count; /* remove the rightmost 1 (Binary) */v & = (v-1) each time./* You can determine whether a number is a power of 2: v> 0 & (v-1) = 0) */} return count;}/* method 2 */int DutCountOf1InBin_2 (unsigned int v) {/* the idea of this algorithm is to add the binary 1 of each adjacent bit of a number, and finally obtain the total number of 1 */v = (v & 0x55555555) + (v> 1) & 0x55555555); v = (v & 0x33333333) + (v> 2) & 0x33333333 ); v = (v & 0x0f0f0f) + (v> 4) & 0x0f0f0f); v = (v & 0x00ff00ff) + (v> 8) & 0x00ff00ff ); v = (v & 0x0000ffff) + (v> 16) & 0x0000ffff); return v ;}



1 In the binary number

This method should be relatively simple when we use Shift and one-digit retrieval and judgment. In addition, it can be calculated based on the decimal to binary algorithm. Another way is to format it into a string in the form of % B, so that the binary number becomes a string and can be judged one by one.

The number of 1 in the binary of an integer, for example, input 10. Because the binary value is 1010 and there are two ones, the output is 2.

# Include <stdio. h> void print_2 (int val2) {unsigned char * p = (unsigned char *) & val2 + 3; // from low to high, low-end byte computer for (int k = 0; k <= 3; k ++) {int val2 = * (p-k); for (int I = 7; I >= 0; I --) {if (val2 & (1 <I) printf ("1"); else printf ("0 ");} printf ("");} printf ("\ n");} int main () {int Data, count = 0; printf ("Please Enter Data :"); scanf ("% d", & Data); printf ("Binary is:"); print_2 (Data); while (Data! = 0) {if (Data & 1) {count ++;} Data >>=1;} printf ("count = % d \ n", count ); return 0;} uses bitwise operations, and the operation section is provided.

Hope to help you.



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.