The number of inputs is converted to a binary sequence, and the number of 1 in the sequence is counted

Source: Internet
Author: User

★ The number of inputs into a binary sequence, and statistical sequence of 1 of the number of description: The normal mode in addition to the remainder of the second method is easy to understand, but for the number of inputs are limited to positive and 0, for negative numbers do not adapt, so the use of the method with the post-shift to expand the number of input range. #include <stdio.h>int main () {int M,b,c,i;int count = 0;char a[32];p rintf ("Please enter a number: \ n"), scanf ("%d", &m); for (i = 0; I < 32; i++) {if (M & 1 = = 1) {count++;}        c = m & 1;        With 0001 and 1 anti 0, the remainder is equal to the effect of modulo (%) b = m >> 1; The whole sequence shifts one bit to the right, equivalent to (/2) the effect m = b;a[i] = c;} printf ("The binary sequence of this number is: \ n"), for (i = >=; I, 0; i--) printf ("%d", A[i]);p rintf ("\ n");p rintf ("The number of 1 in the sequence is: \ n");p rintf (" count=%d ", Count);p rintf (" \ n "); return 0;}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/E2/wKioL1YJOWPCpdodAADOg0rvK6c319.jpg "title=" Run Test " alt= "Wkiol1yjowpcpdodaadog0rvk6c319.jpg"/>

This article is from the "Warm Smile" blog, please be sure to keep this source http://10738469.blog.51cto.com/10728469/1698928

The number of inputs is converted to a binary sequence, and the number of 1 in the sequence is counted

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.