# Solutions Chapter 8 challenge? -- spoiler alert!

#1

Hi all,

I calculated the solutions for this chapters challenge, but I’m not sure if I did it the right way. Could someone have a look at it?

The biggest number stored in a signed short (2 bytes) would be (2^15)-1 == 32768-1 == 32767 (the -1 is because it starts at 0)
The smallest number would then be -32768 (we don’t need to reserve space for the zero twice, right?)

An unsigned short can then hold numbers from 0 to (2^16)-1, so the max number stored is 65536-1=65535

Feedback appreciated!
Marga.

#2

Marga,

You are correct

#3

Thank you, glad to hear that!

#4

try this code at XCODE:

``````    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);``````

remember to include the math library `#include <math.h>`

#5

[quote=“codvir”]Marga,

You are correct [/quote]

Thanks codvir, I guess I got it right also then!

In summary:

short = -32,768 - 32,767
unsigned short = 0 - 65,535

#6

[quote=“whakkee”]Hi all,

I calculated the solutions for this chapters challenge, but I’m not sure if I did it the right way. Could someone have a look at it?

The biggest number stored in a signed short (2 bytes) would be (2^15)-1 == 32768-1 == 32767 (the -1 is because it starts at 0)
The smallest number would then be -32768 (we don’t need to reserve space for the zero twice, right?)

An unsigned short can then hold numbers from 0 to (2^16)-1, so the max number stored is 65536-1=65535

Feedback appreciated!
Marga.[/quote]

Am I missing something …

How did you get ^15 ?

where does 15 come from, did I miss something stated in the book?

In understand short is 2 bytes.

#7

[quote]How did you get ^15 ?
where does 15 come from, did I miss something stated in the book?
In understand short is 2 bytes.[/quote]
One bit is for the sign (-) --> 16-1=15

#8

[quote=“Vertex”][quote]How did you get ^15 ?
where does 15 come from, did I miss something stated in the book?
In understand short is 2 bytes.[/quote]
One bit is for the sign (-) --> 16-1=15[/quote]

Thanks for clearing that up

#9

What page and chapter was this on the book? I only vaguely recall this.

#10

wrote a little program to find out the range of an unsigned short and a short

[code]#include <stdio.h>

int main (int argc, const char * argv[])
{
int counter = 0;
unsigned short uS,temp;

``````// figure out range of an unsigned short
for (uS = 0; uS == counter; uS++, counter++) {
// saving state of uS before loop increments
temp = uS;
}
// Output: range unsigned short
printf("Range of an unsigned short is 0 to %d\n", temp);

int countDown = 0;
int countUp = 0;

short sPositive,sNegative, tempNegative,tempPositive;

// figure out negative part range of a short
for (sNegative = 0; sNegative == countDown; sNegative--) {
tempNegative = sNegative;
countDown--;

}
// figure out positive part range of a short
for (sPositive = 0; sPositive == countUp; sPositive++) {
tempPositive = sPositive;
countUp++;

}

// Output: putting together range of a short
printf("Range of a short: %d to %d\n", tempNegative,tempPositive);
``````

return 0;
}[/code]

OUTPUT:
[Switching to process 4906 thread 0x0]
Range of an unsigned short is 0 to 65535
Range of a short: -32768 to 32767

Program ended with exit code: 0

#11

I’m with ithinkimalion. I’m not sure where the reference is to solve this challenge. I believe I understand the code and the solution to the number size, but I’m not sure where in Objective-C Programming this is referenced. I think I knew this from another C book I’ve read before.

#12

It is implied in Figure 8.1, but not actually stated, that there are 8 bits in a byte.

#13

I appreciate everyone sharing their solutions, but (I hope the author is listening) I think for real beginners it would be useful to have an official answer for each challenge. Sometimes I read the explanations for challenges that I have already successfully completed and they confuse me. Other times, I think the shared responses, while correct, are explained in a way that is useful for people who already have a broader coding knowledge base. Other times, people are showing what they can do. All these things are great, but, beginners need a logical plainly worded and organized response. Just my opinion. And absolutely no disrespect for those of you kind enough to share your work.

#14

I agree! The reason I like this book is that I can understand the author’s style of writing. Answers to Challenges written in the same style would be very helpful.

#15

The book is a thoughtful pass through one set of problems. The challenges (another set) are for experimentation and discussion. I’m not planning to publish a set of solutions – I know this is frustrating. That’s good. You’ll learn the most through that frustration.

Is what you’ve done right? Or the best? Not that important. If it works, that is good enough for now. You’ll develop elegance through experimentation, discussion, and frustration.

Rock on people.

#16

my solution :

#include <stdio.h>
#include <limits.h>

int main(int argc, const char * argv[])
{
printf(“A short starts at %.0d and end at %.0d\n”, SHRT_MIN,SHRT_MAX);
printf(“The maximum number of an unsigned short is %.0d\n”, USHRT_MAX);

``````return 0;
``````

}

output:

A short starts at -32768 and end at 32767
The maximum number of an unsigned short is 65535

#17

That was slick. Do the uS and counter simply quit incrementally increasing once they’ve reached the limit uS? Did you have to experiment, or did you know this would happen?

#18

I’m confused as to the reason why this ends at 65535. I understand that that is the upper limit of an unsigned short but shouldn’t this loop back to 0. Every time I tried to write this program it would either

1. print an endless loop or
2. print all the numbers and I had to hunt for the answer or
3. I had to put in some arbitrary break.

Here is an example of an overly convoluted solution I was working on …

[code]
#include <stdio.h>
#include <math.h>

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

{
short i;
int x = 14;
for (i= pow(2, x); i< pow (2,x+22); i++) {

``````    if (i < (((pow(2, x))+(pow(2, x)-1)))) {
continue;
}
printf("%d   ",  i);
if (i == -i) {
break;

}
return 0;
}
``````

}[/code]

Here I thought if I could get a signed answer. Then I would have it loop back with the unsigned version. Then write a way for it to only print the minimum and maximum values. But that ended up being a two day rat hole and I never found a solution

[code]#include <stdio.h>
#include <math.h>

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

{
short i;
for (i=0; i< pow (2,22); i++) {
printf("%d\t",i);
if (i == -i) {
break;
}
}
}[/code]

I tried moving printf around, incerting a continue command, kicking out values.

I may have started to try something that can’t be done with these libraries. It occurs to me looking at Mantis’s answer that by puting in the pow function I am doing some of the computer’s work for it. (A computer is simply a counting machine I know).

These were my thoughts. Any feedback would be appreciated.

Thanks

–s

#19

That’s because after the value of uS reaches its max the value, the next time around the loop its value will be 0 and the value of counter will be 65536 and the test uS == counter will fail, thus stopping the loop.

``````//  main.m

#include <stdio.h>

int main (int argc, const char * argv[])
{
int counter = 0;
unsigned short uS,temp;

// figure out range of an unsigned short
for (uS = 0; uS == counter; uS++, counter++) {
// saving state of uS before loop increments
temp = uS;
}
// Output: range unsigned short
printf ("Range of an unsigned short is 0 to %d\n", temp);
printf ("counter: %d\n", counter);

assert (temp == (unsigned short)(-1));

// uS wraps around to zero after reaching max
temp = -1; // max
printf ("temp = -1: %d\n", temp);

temp += 1; // wraps around to zero
printf ("temp +=1: %d\n", temp);

return 0;
}``````
``````Range of an unsigned short is 0 to 65535
counter: 65536
temp = -1: 65535
temp +=1: 0``````

Given a signed numeric type T, the expression ((unsigned T)(-1)) yields the maximum value that can fit in a variable type of unsigned T.

Thus, the easiest way to set the maximum value of an unsigned variable is to assign it -1:

``````//  main.m

#include <stdio.h>

int main (int argc, const char * argv[])
{
// set max
const unsigned long maxUlong = -1;
const unsigned char maxUchar = -1;

// test
assert (maxUlong == (unsigned long)(-1));
assert (maxUchar == (unsigned char)(-1));
assert (maxUlong != (unsigned char)(-1));
assert (maxUchar != (unsigned long)(-1));

return 0;
}``````

#20

Howdy. I’m new to Objective-C. Just curious about something. I understand the nifty loop approach to solving this challenge. How come the error handler didn’t burp, i.e. terminate main() because a value higher than the maximum possible value was being assigned to a short?

Thanks!