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.
[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;