2

http://ideone.com/hANuZ

#include <stdio.h>
int main(int argc, char *argv[]) {
    /**
     * Prints out powers of 2 for 32 iterations.
     */
    int iterations = 0;
    int value = 1;
    while (iterations <= 32) {
        // Prints in this format: iterations    2^n    value
        printf("%d\t2^%d\t%d", iterations, iterations, value);
        switch (iterations) {
          case 8:
          case 16:
          case 32:
              printf("\t\t%d bit\n", iterations);
              break;
          default:
              printf("\n");
              break;
        }
      value *= 2;
      ++iterations;
    }
    return 0;
}

When I compile and run this piece of code, weird stuff happens when I print 'value' after it is larger than 2^30, even when I declare it as an unsigned long.

What do I do? I am simply a beginner. :-(

3
  • 3
    actually, this runs 33 iterations Commented Aug 9, 2011 at 3:31
  • maybe because I used ++iterations? Commented Aug 9, 2011 at 3:33
  • hmm.... 0-32 = 33. You initialize to 0 and loop until it is <= 32, thus it passes in the loop 33 times Commented Aug 9, 2011 at 3:36

4 Answers 4

4

You are printing it like it is a signed integer. try using the format string

"%u\t2^%u\t%u"

check your printf documentation for all of the formatting codes and what they do. %u is for unsigned int. unsigned long is generally %lu

Sign up to request clarification or add additional context in comments.

4 Comments

this got rid of the - sign on 2^31 on the 31st iteration
only the last one may be %u the first two are the variable iterations and may be %d alright
@Sweetgirl17, 32nd iteration. Your first iteration is 0, so when iterations is 31, you are at your 32nd one.
so for 32 bit integers, the range of values is -2^31 .. 2^31-1. for 32 bit unsigned integers, the range is 0 .. 2^32-1. -2^31 is the signed bit sequence 1 000....00 (first bit 1, everything else is 0). however, that sequence happens to be 2^31 when interpreted as unsigned, which is why the "-" was removed.
2

The simple answer would be to try declaring it as an unsigned long long which is larger than 32 bits (minimum 64 bits according to the standard, thx @caf).

When dealing with types where the specific size is important though, you should use a type with a known size, like int64_t.

4 Comments

unsigned long long is definitely larger than 32 bits: it is required to be at least 64 bits.
@caf: I thought it just had to be bigger than long. Ta.
It only has to be equal or larger than long, and at least 64 bits. Both long and long long being 64 bits is conforming, and in fact is common on 64 bit POSIX systems.
@caf: Thanks for the clarification. I've edited to include your info.
1

Just for fun, I did modify your code example a little for clarity

#include <stdio.h>

int bitcount(unsigned long i) {
   int count = 0;
   while (i > 0) {
      count++;
      i>>=1;
   }
   return count;
}

int main(int argc, char *argv[]) {
   /**
    * Prints out powers of 2 for 32 iterations.
    */
   int iterations = 0;
   int value = 1;
   while (iterations <= 32) {
      // Prints in this format: iterations    2^n    value
      printf("%d\t2^%u\t%u\t\t%d bit\n", iterations+1, iterations, value, bitcount(value));
      value *= 2;
      ++iterations;
   }
   return 0;
}

You'll notice that 127 is 8 bit and 256 is 9 bit. This is because

127 =   1000 0000
256 = 1 0000 0000

Also, 2^32 is 0 because

2^32 = 1 0000 0000 ... 0000 0000       (overflow for int)
     =   0000 0000 ... 0000 0000 = 0   (only the first 32 bits are preserved)

Comments

0

If you need integer precision you need to use an external library for larger numbers. For C GMP http://gmplib.org/ is a well known library. If you don't need integer precision just use a float or a double.

If you're curious about what the limits are based on for types, this page is an ok read up http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/CONCEPT/data_types.html.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.