Is it too bad if I don't really understand the challenge cha


#1

Hey guys,

So far I’m absolutely loving the book. Super clear introduction to programming.
I’ve just finished chapter 8. Addresses and Pointers and for the first time I was literally: "What??"
So I came here, and saw the solution and saw a bunch of code that I know, for a fact, that I would have NOT written by myself:

    printf("A short number starts at %.0f until %.0f\n", -(pow(2,sizeof(short)*8-1)), pow(2,sizeof(short)*8-1)-1);
    printf("A unsigned short number starts at 0 until %.0f\n", pow(2,sizeof(short)*8)-1);

Does it come with time and over the course of the book?
Or should I stop and try to understand everything written above?
I tried, but it just feels outside of my current scope.

Thanks in advance
Cheers
Cuchi


#2

[quote] printf("A short number starts at %.0f until %.0f\n", -(pow(2,sizeof(short)*8-1)), pow(2,sizeof(short)*8-1)-1); printf("A unsigned short number starts at 0 until %.0f\n", pow(2,sizeof(short)*8)-1); [/quote]
Understanding the above code requires an awareness of fundamental concepts.

First of all pow is a function: double pow (double x, double y). It computes x raised to the power y.
For example: pow (2, 0) yields 1, pow (2, 1) yields 2, pow (2, 3) yields 8 and pow (2, 5) yields 32.

And sizeof is a unary operator that yields the size (in bytes) of a type or a fundamental object.
For example: sizeof (char) yields 1 (one byte) and sizeof (short) yields 2 (two bytes), etc.

Finally, an understanding of how integer numbers are represented (in binary form), both signed and unsigned.

To make it easier to understand, the second printf statement can be written as shown below.

    double number_of_bytes = sizeof (short);
    double number_of_bits  = number_of_bytes * 8;
        
    unsigned short giantest_unsigned_short = pow (2, number_of_bits) - 1;

    printf ("A unsigned short - smallest: 0 largest: %u\n", giantest_unsigned_short);

    // Or
    giantest_unsigned_short = 0xFFFF; // set all bits

    printf ("A unsigned short - smallest: 0 largest: %u\n", giantest_unsigned_short);

The first printf statement can also be written similarly, but it requires an explanation of how signed numbers are represented.


#3

Dude… you just saved my day! You rock!!!


#4

Ok… Sorry for asking more stuff man! I’m non native in english, a designer, son of two painters, and maths was never a thing in my childhood :stuck_out_tongue:

I think I almost get it. At least now I can read the pow function and understand it. But there are two things I still don’t get:

The first:

  • I get the: sizeof(short)*8-1
  • Which means that if a short occupies 2 bytes | 2 * 8 = 16 switches | -1 because of the sign (+ / -)
  • But why the ‘2’ on the pow(2, sizeof(short)*8-1)?
  • Is it because of the 2bytes? Is it because it’s two slots of 8? I can’t get it… :frowning:

The second:

  • At the end of the code bellow:
  • Why substract another ‘1’ on the second pow?

Thanks in advance man…


#5

2 is the base (or radix) we use to represent numbers in binary form, because each bit (or switch) has two states: 0 or 1. A number with N bits will thus have 2^N (= 2x2x2…x2 = 2 multiplied by itself N times) different states. For example: If N=4, then 2^4 = 2x2x2x2 = 16 different states.

To represent negative integer numbers (in N bits) we use two’s complement. 2^N - k is the two’s complement of a negative number k.

Note: 2^N - k + k = 2^N = [color=#FF0000]1[/color]000 . . . 0 ([color=#FF0000]1[/color] followed by N zeros.) That is, 2^N is zero when written in N bits.

Thus for N=4: 2^4 - 1 is the two’s complement of -1, which is 15 in decimal and 1111 in binary ; 2^4 - 8 is the two’s complement of -8, which is 8 in decimal and 1000 in binary. 2^4 - 3 is the two’s complement of -3, which is 13 in decimal and 1101 in binary.

To represent a positive signed integer number in N bits we set aside 1 bit for the sign (which is always 0), and we use the remaining N-1 bits to represent the number. Thus, the largest number that fits in N-1 bits is 2^(N-1)-1. ((N-1) bits have 2^(N-1) different states but we start numbering them from 0: 0, 1, 2, . . ., 2^(N-1)-1.)

For example: for N=4, 2^(4-1)-1 = 7 = 0111 in binary; for N=8, 2^(8-1)-1 = 255 = 01111111 in binary.


#6

Thank you so much man!!! :slight_smile: You’re super helpful!