# Challenge Question Solution for the Binary Challenged

#1

A lot of folks have mentioned that they are math challenged, and so had a hard time with the binary calculations required to answer the challenge questions in Chapter 8. Which reminds me of one of my favorite T-shirt quotes: “There are 10 kinds of people in the world. Those who understand binary, and those who don’t.” I was lucky enough to learn binary back when I was in high school, programming sprites on my Commodore 64. If that reference doesn’t date me, I don’t know what will.

In any case, the mathematically challenged need not despair. Here’s a brief snippet of code that yields the correct answers via a programming solution rather than a mathematical one, without having to do any binary math:

``````short infiniteLoop = 1;
short counter = 0;
while (infiniteLoop) {
counter ++;
printf("The counter is now %d.\n", counter);
if (counter < 0) {
printf("The short variable counter has maxed out!\n");
break;
}
}``````

A similar solution for unsigned short variables would read:

``````short infiniteLoop2 = 1;
unsigned short counter2 = 0;
while (infiniteLoop2) {
counter2 ++;
printf("The counter is now %d.\n", counter2);
if (counter2 == 0) {
printf("The unsigned short variable counter has maxed out!\n");
break;
}
}``````

Of course, coming up with both these solutions on your own requires foreknowledge of the fact that signed variables wrap around from their maximum to their minimum value, and that unsigned variables wrap back to zero.

Or, you can do what I did, and omit the ‘if’ clause, let the program run in an infinite loop, push the ‘Stop’ key, and scroll through the output to see what happened. I added the break condition later, once I figured out what was happening.

#2

I found an even quicker solution

1 google minimum value short
2 find:
"Signed: From −32,768 to 32,767,
Unsigned: From 0 to 65,535
"
3 Satisfy your innernerd with checks in xcode
4 note that if you max out, the numbers will count backwards. Interesting!

Motto: less is more.

#3

Man I was in total pain until I read your post… I’m a, non-english native and mathematically challenged designer who spent childhood painting and doing artsy stuff!!! So I just couldn’t get it and the solutions I found had pow() functions included - which I had never even heard of. So I just thought: ‘I’m gonna suck at this’!!!

But then I read your post and I realised I’m not the only one who is mathematically challenged! So is it ok not to be able to write the solution for chapter 8? It doesn’t mean that I will suck at everything else?

I loved your programatic solution. What I understood from it was:

• you wrapped counter in a ‘short’ variable which gives it a 2byte limit
• then you looped it within the 2byte limit
• so it grew from 1 to +32767 and then it tried to mirror and go from the opposite side back to zero
• in order to break the inifiteloop, you just stated that as soon as it got negative | and you knew in advance that the shift to negative would be a radical one from +32767 to -32768
• you broke the loop and got the two maximum values right there, tight and sweet!

My only question (dumb here) is why does it go from +32767 to -32768 instead of +32768 to -32768?

Once again, thanks for your amazing solution!