 # Challenge 2

I didn’t create a program for this. It can be done by hand.

A signed short is 2 bytes (8 bits). 1 bit is used to indicate positive or negative which leaves us with 7 bits.

1111111 in decimal is 127. At first I was going to say the range was -63 through 63 for 127 values, but then I facepalmed to remember there is a bit for the sign so it indeed extends from -127 to 127.

As for the unsigned short (no negative values). I assumed there is no bit to indicate it is positive. This would be pointless to me, but if someone knows for sure please tell me This means it has the full 8 bits. 11111111 = 255. There is no negative numbers so it ranges from 0 to 254 (must include 0).

I guess I could make a program to show this. I will see what I come up with and post later.

That should be “extends from -128 to 127”.

Take a look at the following example.

``````//  main.m

int main (int argc, const char * argv[])
{
// signed values (in two's complement) - one bit for the sign
char charMinValue = 0x80;
char charMaxValue = ~charMinValue; // 0x7f;

printf ("char: [min, max] = [%d, %d]\n", charMinValue, charMaxValue);

// unsigned values - no sign bit
unsigned char ucharMinValue = 0;
unsigned char ucharMaxValue = 0xff;

printf ("unsigned char: [min, max] = [%u, %u]\n", ucharMinValue, ucharMaxValue);

// signed values (in two's complement) - one bit for the sign
short shortMinValue = 0x8000;
short shortMaxValue = ~shortMinValue; // 0x7fff;

printf ("short: [min, max] = [%d, %d]\n", shortMinValue, shortMaxValue);

// unsigned values - no sign bit
unsigned short ushortMinValue = 0;
unsigned short ushortMaxValue = 0xffff;

printf ("unsigned short: [min, max] = [%u, %u]\n", ushortMinValue, ushortMaxValue);

return 0;
}``````
``````char: [min, max] = [-128, 127]
unsigned char: [min, max] = [0, 255]
short: [min, max] = [-32768, 32767]
unsigned short: [min, max] = [0, 65535]``````

[Accelerate your learning and become a competent programmer: pretty-function.org]

A byte is 8 bits, so two bytes is 16 bits. So the range of a signed short is -32768 to 32767 and unsigned is 0 to 65535.

[quote]I didn’t create a program for this. It can be done by hand.

A signed short is 2 bytes (8 bits). 1 bit is used to indicate positive or negative which leaves us with 7 bits. [/quote]

Creating a program to solve this challenge could be the way to go.

I’ve come up with this “brute force” program, having no clue of the answer before writing the code and using the tools included in the book up to this point.

[code]#include <stdio.h>

int main(int argc, const char * argv[]) {
short largestShort = 0;
while (largestShort >= 0) {
++largestShort;
}
printf(“The largest number a short can store is %d\n”, --largestShort);

``````short smallestShort = 0;
while (smallestShort <= 0) {
--smallestShort;
}
printf("The smallest number a short can store is %d\n", ++smallestShort);

unsigned short largestUnsignedShort = 1;
while (largestUnsignedShort > 0) {
++largestUnsignedShort;
}
printf("The largest number an unsigned short can store is %d\n", --largestUnsignedShort);
return 0;
``````

}
[/code]

Hope it helps Hi,

I like your solution to this Challange with code, but there is some things I cannot wrap my had around 1. When you execute this code, why it shows negative result?
``````short largestShort = 0;
while (largestShort >= 0) {
++largestShort;
}``````
1. Maybe I missed something, why does this “–” before largestShort makes number positive?
`` printf("The largest number a short can store is %d\n", --largestShort);``

Thank you