2nd Challenge: Better way than brute force?

For the second challenge (“How much range?”) I basically brute forced the answer and then cleaned up my code (below).

I suppose I could’ve solved this with math by applying some knowledge I’ve gleaned, i.e. 2 bytes = 16 bits, so:

  • signed short: 1 bit for the sign leaves 2^15 - 1 as the largest number (-1 because we’re counting zero), -1 * 2^15 as the “smallest”
  • unsigned short: no bit for the sign leaves 2^16 - 1 as the largest number.

While my math checks out with the answers I got from my code, I’m still struggling to relate this challenge to the chapter. I feel like my answer was supposed to involve pointers or addresses in some way, but I can’t figure out how pointers apply here.

    // How Much Range?
    // What is the smallest number a short can store? What is the largest?
    // This code suggests 32767 and -32768, respectively
    printf("Challenge: What's the largest and smallest short?\n");
    short x = 32766;
    for (int count = 32766; count < 32769; count++) {
        printf("%d: %d\n", count, x);
        x++;
    }
    
    // What is the largest number an unsigned short can store?
    // This code suggests 65535
    printf("Challenge: What's the largest unsigned short?\n");
    unsigned short y = 65534;
    for (int count = 65534; count < 65537; count++) {
        printf("%d: %d\n", count, y);
        y++;
    }

That challenge does not ask you to write a program. It’s just a question for you to solve on paper or see the reference.

en.wikipedia.org/wiki/Integer_%2 … rt_integer