Find out the number of 1 in an integer

Source: Internet
Author: User

Enter an integer to find the number of binary 1. Consider the point of knowledge: How to ask for negative numbers, because the computer is stored in a complement of the form of storing a number. Because positive source code, anti-code, complement are the same, do not consider. But negative numbers will have to be considered, such as-0, its source code should be 10000000 00000000 00000000 000000000 00000000, so negative numbers to consider.

Here is the code implementation:

#ifndef  _findnumberof1_#define _findnumberof1_/*================================ macros == =================================*/#define  false 0#define ture 1/*======================== ========&NBSP;FUNCTION&NBSP;===================================*/INT&NBSP;FINDNUMBEROF1 (int num); #endif  //_findnumberof1_ /***********************************************************//*32 bit Platform *//* Function Name: findnumberof1*//* functions Port parameter: integer *//* function return value requiring 1: Returns the number of 1 or the error code *//* function: The number of binary 1 in the input integer *//******************************** /int findnumberof1 (Int num) {         int retvalue = 0;if ( -0 == num)   {return 1;} Else if (num >= 0) {while (num) {if (1 ==  (num&1)) {retvalue++;} num >>= 1;}} else{num =  (~num+1);//negative numbers are stored in a complementary fashion, so source while (num) {if (1 ==  (num&1)) {retvalue++;} num >>= 1;} Retvalue += 1;//plus sign bit}        return retvalue;} #include <stdio.h> #include "findNumberOf1.h" Static int inputnumber (int *number) {if (NULL  == number) {return false;} printf ("Please enter an integer:"); scanf ("%d", number), if ((number < -2147483648) && (number>2147483647)) { printf ("Number of inputs out of range \ n"); return false;}} /***********************************************************//*32 bit platform *//* function name: main*//* function Port parameter: No *//* function return value: 0*//* function function: program null *//***********************************************************/int main () {int number =  0;int returnvalue = 0;inputnumber (&number); Returnvalue = findnumberof1 ( Number);p rintf ("%d", returnvalue);p rintf ("\ n"), System ("pause"); return 0;}


Find out the number of 1 in an integer

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.