Jump to content

Bits of Fun

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Khomez

Khomez

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, I have a programming assignment to count the number of 1's in a number as a binary representation. I have figured out how to count the 1's in a positive number but I do not know how to count the 1's in a negative number. I know the left shift operator << needs to be used but I don't know how I would use it.



Write a C program that reads in a list of integers between -20000 and 20000 from a file named testdata47. Your program does not know how many integers there are in the file. Your program read these integers one by one and after each integer is read it will then print out the exact number of 1s that exists in the number’s twos complement binary representation. You will need a loop to read in the number from the file until end of file is determined. You will need another loop – inside this loop – to process each bit with a given number. One strategy could be to test the rightmost bit. See if it is a 1 or a 0. You can do this by seeing if the number is an odd number or not. If the number is odd, then a 1 exists in the rightmost position. You can then rely upon C’s right shift operator to move all of the bits to right one position. You could enter the innermost loop sizeof (int) * 8 times. Do you understand these words? Remember: The right shift operator in C does not guarantee that a 0 bit will be rolled in on the left side. You could test to see if the number is negative or not and use a left shift operator instead. The left shift operator in C always guarantees a 0 is rolled in.

Here is what I have so far....

/************************************************************************/
/*   Programmer:   Kyle Homan                                           */
/*                                                                      */
/*   Program 47:   Bits of Fun                                          */
/*                                                                      */
/*   Approximate completion time:    minutes                            */
/************************************************************************/

#include <stdio.h>

int main (int argc, char *argv[]) {

  int n, num, bits = 0;

  FILE *fin = fopen("testdata47", "r");

  while (fscanf(fin, "%d", &n) != EOF) {

    num = n;

    if (n >= 0) {

      while (n > 0) {

        if (n % 2 == 1) bits = bits + 1;

        n = n >> 1;

      }
    }

    else {

      while (n < 0) {

        if (n % 2 == 1) bits = bits + 1;

        n = n << 1;

      }
    }

 printf("\nThere are %d 1's in %d.\n", bits, num);

    bits = 0;

  }

  return (0);
}

Could somebody please help me with this?

Edited by WingedPanther, 05 December 2008 - 10:16 AM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
As in the other question: are you getting the correct results?
Also, please use code tags in the future.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Khomez

Khomez

    Newbie

  • Members
  • Pip
  • 4 posts
I get the correct results with only positive numbers, not negative numbers in the file

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Depending on how a negative number is stored (often 2's complement), mod 2 division does not reflect the state of the bit. I would use bitwise-and with 1.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog