#1

The second paragraph of the challenge asked a few questions. Here are my answers:

2 bytes contains 16 bits. 16 one’s in a row (ie: binary 1111111111111111) = 65535

Thus the larges unsigned number a short can hold is 65,535

If one bit is taken for the sign, then there are only 15 bits remaining. 15 one’s in a row (ie: binary 1111111111111111) = 32767

So the smallest signed short can be -32767 and the largest signed short can be 32767.

I did these conversinos using this binary to decimal converter:

mathisfun.com/binary-decimal … erter.html

Am I right?

…Dale

#2

That looks wright.

You can also use the calculator Application on your Mac.

There is also an iPad app called RadixJam on the App Store (soon there will be a Mac version, I know because I am the creator), that shows you how the numeral systems work.

#3

Ha! Good answer! But I HATE the Mac! Funny eh. I really enjoy iOS but hate the mac. I’m a Windows user through and through and only use the bare minimum on the Mac that I need to use xCode. In fact, I have my mac tehtered to my PC using ShareMouse to control my Mac’s keyboard and mouse with my PC’s keyboard and mouse.

#4

Windows! I call it TOS, Terrible Operating System. The worst operating system one can ever imagine and design!

If you enjoy iOS, you should take the plunge into the Mac OS X world. You will never regret. It is a paradise waiting for the real programmer in you

#5

Ha! Ain’t going to happen. I’ve been a DOS/Windows user for 30 years now. If you still believe Windows is a TOS then you haven’t seriously used Windows since Windows 7. I believe both platforms have largely reached parity when it comes to stability and ease of use. I leave my Win7 machine running 24/7, 7 days a week, controlling 5 monitors and running dozens of apps and it NEVER crashes. I only reboot when I want to apply updates that require rebooting and even that is rare these days.

Plus, I’d like to devvelop for the Android and Windows phone platforms too.

I am quite happy with my progress here. I think I’ve learned enough (up to Chapter 7 now) to attempt my first single view app. It’s going to be a basic Tip Calculator. Yes, I know there are already 20 or more of them in the app store, but not a single one of them does everything i want mine to do (and what I think most people will want). Besides, its only a learning project for me. Then off to two more I have planned for now. Lot’s of ideas I’ve always wanted to implement. I’m finally getting to the place where I think I actually will be able to pull this off.

#6

Actually, the smallest number that can be held is -32768, not -32767. The full explanation for that is a little obtuse, but it revolves around the fact that there is only one 0, not a +0 and a -0. So you get to squeeze in one more negative number than you can have positive numbers.

If you REALLY want the extended explanation: http://en.wikipedia.org/wiki/Two’s_complement

#7

bcarter5876 is right, but I didn’t know the “obtuse” reason behind it (although I see it now… sort of). But I came up with an answer (the wrong one, at first, but I was close) and used this code to check it:

``````    short smallestNumber = -32768;
short largestNumber = 32767;
printf("smallestNumber = %d\n", smallestNumber);
printf("largestNumber = %d\n", largestNumber);
return 0;``````

If it printed the correct number, I knew largestNumber wasn’t too big, but it might be too small. But if the number wasn’t displayed as I assigned it, I knew it was too big. So in the example above, if I increase largestNumber by 1, I get a value other than what I assigned to it (-32768 if short largestNumber = 32768; wrong sign!), and I knew the number just below that was the largest number possible. I did the same with the smallestNumber variable, but i decreased it.

I know this isn’t elegant, but it is a way to prove which number is correct if you think you have the right answer without looking at the forums for the answer. I didn’t see anyone else mention this, so I thought I’d share. Not sure if that helps anyone.

#8

Anyone whose head hurts every time they try to do binary calculations might appreciate my challenge solution, which I posted under a separate topic, here:

http://forums.bignerdranch.com/viewtopic.php?f=140&t=5437

#9

Well i hope to be in the right place, because i do not know where to put my answer to the challenge of this chapter… if im wrong please tell me and i will write in the right post…

And then this is my code for the challenge…

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
printf(“Size of FLOAT variable is %zu\n”,sizeof(float));
printf(“Size of FLOAT pointer is %zu\n”,sizeof(float *));

``````short MaxShort = 1;
short Test = 1;
while (MaxShort) {
MaxShort++;
if (Test == MaxShort) {
MaxShort = (MaxShort * -1) -1;
printf("max value for short is %d\n", MaxShort);
printf("min value for short is %d\n", Test);
break;
}
Test = (MaxShort * -1) - 1;
}
return 0;
``````

}
[/code]
I think this code could help to check others variable types.

#10

You meant MaxShort += 1.

Please post again after fixing the syntax error:

``````while (MaxShort) {
MaxShort = ++;
...
}``````

#11

[quote=“ibex10”]You meant MaxShort += 1.

Please post again after fixing the syntax error:

``` while (MaxShort) { MaxShort = ++; ... } ```[/quote]

ibex10 I was wrong… the correct way is:

```...while (MaxShort) { MaxShort++; if (Test == MaxShort) {...```

and this next code has the same result

``````MaxShort++;
MaxShort += 1;``````

increase the variable in one.
Thanks!

#12

is anyone able to share how they did the first part of the challenge - finding how much memory a float consumes? I can’t seem to get it :<

#13

You use the sizeof operator to find out how many bytes are needed for an instance of a given type. For example:

``sizeof (char), sizeof (float), sizeof (unsigned int), sizeof (char*), etc.``

You can also apply it to variables:

``sizeof (weight), sizeof (waypoints), sizeof (pointerToSomeType), sizeof (*pointerToSomeType), etc.``

And you can use it to calculate how many items there are in a statically allocated array:

``````const float weights [] = {32.5, 32.7, 31.2, ...};

const unsigned int numberOfWeights = sizeof (weights) / sizeof (weights [0]);``````

#14

Thanks for a very detailed answer!

The output said the size of a float is 4 bytes, is that correct?

``````printf("The float consumes %zu bytes\n", sizeof(float));
return 0;``````

#15

Do the following…

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

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

``````// float takes up how many bytes
printf("An float is %zu bytes\n", sizeof(float));

// size of float pointer
printf("A float pointer is %zu bytes\n", sizeof(float *));

// size of short pointer
printf("A short pointer is %zu bytes\n", sizeof(short *));

// min size of short
printf("A short minimum value is %d\n", SHRT_MIN);

// max size of short
printf("A short maximum value is %d\n", SHRT_MAX);

// max size of unsigned short
printf("A unsigned short maximum value is %d\n", USHRT_MAX);

return 0;
``````

}[/code]