0

I've been doing some homework on basically just converting an inputted integer into it's binary form. So far I've made great progress on converting the inputted integer into a binary number as well as declining any number that is negative.

The problem I'm having however is counting the number of 1's within the binary code and printing how many one's are in the binary representation. I've tried many examples of code from the internet and my book(Not direct copy and paste, I've tried fitting it to my needs) but so far have come to no avail. I believe it's cause I'm not focusing on a specific number.

Basically, I'm new to C, very new. I've done most of my past 2 semesters in Java and have NO programming experience in any other language, just looking for a helping hand. Just to re-iterate the question:

How can I count the number of one's in an inputted binary representation?

Example: Inputted 44, binary is 101100 Number of 1's: 3

EDIT: So far with the implemented code my new problem now is that whatever integer I input, the output remains 12.

#include <stdio.h>

int main(void)
{
     int integer; // number to be entered by user
     int i, b, v;
     unsigned n = 0;
     while(v) {
        ++n;
        v &= v - 1;
      }
     printf("Please type in a decimal integer\n"); // prompt
     fflush(stdout);
     scanf("%d", &integer); // read an integer

      if(integer < 0)
{
    printf("Input value is negative!"); // if integer is less than
    fflush(stdout);

    return;                  // zero, print statement
}
     else{
    printf("Binary Representation:\n", integer);
    fflush(stdout);}

  for(i = 31; i >= 0; --i) //code to convert inputted integer to binary form
{
    b = integer >> i;
    if(b&1){
    printf("1");
    fflush(stdout);
    }
    else{
    printf("0");
    fflush(stdout);
    }
    }
     printf("\n");
     fflush(stdout);
     printf("Number of 1's in Binary Representation:%d\n", n);
     fflush(stdout);
     printf("\n");
     fflush(stdout);
     return 0;

}//end function main
David Robinson
  • 77,383
  • 16
  • 167
  • 187
user1664272
  • 1
  • 1
  • 5
  • `unsigned n = 0; while (v) { ++n; v &= v - 1; }` – obataku Sep 11 '12 at 23:40
  • Note this is called the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight). You also need not `fflush` after each `printf`. – obataku Sep 11 '12 at 23:41
  • The reason for fflush is because I'm transferring this to the unix database for my school, in which the professor asked us to use fflush after every printf statement. And so far I've used oldrinb's code(Before he posted it, I used ones=0 and while(n) instead. I put this while statement within the for statement though which isn't right because now I get a constant value of 12 as "Number of 1's". – user1664272 Sep 11 '12 at 23:46
  • My basic question is, where is my while statement going and how do I go about implementing it's print-out? Do I use printf("%d", ones)? – user1664272 Sep 11 '12 at 23:56
  • bad indentation - please edit question – tucuxi Sep 12 '12 at 00:04

1 Answers1

0

Edit: Use it here :

ones = 0;
while (integer) { ++ones; integer &= integer - 1; } 
printf("Number of 1's in Binary Representation: %d\n",ones);
fflush(stdout);

And Declare at first the ones:

unsigned int ones;

Full Code :

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int integer; // number to be entered by user
    int i, b, n;
    unsigned int ones;
    printf("Please type in a decimal integer\n"); // prompt
    fflush(stdout);
    scanf("%d", &integer); // read an integer

    if(integer < 0)
    {
        printf("Input value is negative!"); // if integer is less than
        fflush(stdout);

        return;                  // zero, print statement
    }
    else{
        printf("Binary Representation:\n", integer);
        fflush(stdout);}

     for(i = 31; i >= 0; --i) //code to convert inputted integer to binary form
    {
        b = integer >> i;
        if(b&1){
        printf("1");
        fflush(stdout);
        }
        else{
        printf("0");
        fflush(stdout);
        }
        }
    printf("\n");
    fflush(stdout);
    ones = 0;
    while (integer) { ++ones; integer &= integer - 1; } 
    printf("Number of 1's in Binary Representation: %d\n",ones);
    fflush(stdout);
    printf("\n");
    fflush(stdout);
    return 0;
}
Rami Jarrar
  • 4,523
  • 7
  • 36
  • 52
  • I already have no problems printing the binary representation of an inputted number. I just want to understand where to implement the while statement above. – user1664272 Sep 12 '12 at 00:04
  • For some reason it's giving me an output of 12 indefinitely(Encountered this before). I honestly don't know what is going wrong. – user1664272 Sep 12 '12 at 00:12
  • Still an output of 12. There must be something I have done within the for statement that is making it this way. I'll get back in a few minutes after scourging for it. – user1664272 Sep 12 '12 at 00:21
  • I've tried it and it gave me correct answers !! – Rami Jarrar Sep 12 '12 at 00:24
  • !? I honestly changed the values back to my original(what you inputted) and it works! What a stupid error, I completely disregarded using integer. Forgive me, it's always the smallest details that always cause a big impact. Thank you for consistently staying with me on this one, I'm hitting my head against my desk. Instead of using integer I used v which wasn't my set integer value....I can't believe I had the answer the entire time but was silly enough to use a different variable! – user1664272 Sep 12 '12 at 00:30
  • so this will get as accepted answer ? :) – Rami Jarrar Sep 12 '12 at 20:59