# My Solution to Challenge 2

#1

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

int main(int argc, const char * argv[])
{
/**Challenge Part 1/

``````/*On a Mac, a short is a 2-byte integer, and one bit is used to hold the sign (positive or negative).
``````

What is the smallest number that a short can store? What is the largest?*/

``````// Define data type
short stored;
// Byte size of data type
int byteSizeOfMemory = sizeof(stored);
printf("The byte size of a short is %zd.\n", byteSizeOfMemory);

// Bit size of data type
int bitSizeOfMemory = byteSizeOfMemory*8;
printf("The bit size of a short in a 64 bit archeticture is %d.\n",bitSizeOfMemory);

// Power of the short data type
int powerOfShort = bitSizeOfMemory - 1;
printf("The size of the power for a short is %d.\n", powerOfShort);

// Smallest number in the short data type
double sizeOfShort = pow(2, powerOfShort);
printf("The smallest number, the quantity of a negative 2 to the power of %d, that a short can hold is %f.\n", powerOfShort, -(sizeOfShort));

// Largest number in the short data type
double sizeOFUShort = (pow(2,powerOfShort+1)) - 1;
printf("The largest number, the quantity of a positive 2 to the power of %d minus 1, that a short can hold is %f.\n", powerOfShort,(sizeOfShort -1));

/**********************************Challenge Part 2********************************/

/*An unsigned short only holds non-negative numbers.  What is the largest number that an unsign short can store?*/

printf("The largest number, the quantity of a positive 2 to the power of %d minus 1, that an unsigned short can store is %f\n",powerOfShort+1, sizeOFUShort);

/********************************References************************************/

/*  1. http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf page 523 of 701
2. https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/64bitPorting/64bitPorting.pdf page 13 of 68
3. http://en.cppreference.com/w/cpp/language/types
4. http://en.wikipedia.org/wiki/Integer_%28computer_science%29*/

return 0;
``````

}[/code]

#2

Thank you for posting your answer to this challenge, it revealed great insights to me with all the lovely commentary and references. It really helped clarify a lot of the issues I had solving the problem! Thank you again!

#3

Thanks for the solution, I was looking to see what other people have done.

I notices a small error in your code though:

[quote]// Smallest number in the short data type
double [color=#0080FF]sizeOfShort[/color] = pow(2, powerOfShort);
printf(“The smallest number, the quantity of a negative 2 to the power of %d, that a short can hold is %f.\n”, powerOfShort, -(sizeOfShort));

``````// Largest number in the short data type
``````

double [color=#FF0000]sizeOFUShort [/color]= (pow(2,powerOfShort+1)) - 1;
printf(“The largest number, the quantity of a positive 2 to the power of %d minus 1, that a short can hold is %f.\n”, powerOfShort,([color=#0080FF]sizeOfShort [/color]-1));
[/quote]
The math of the bold code doesn’t add up properly, because the power is 16 instead of 15, but you still display the correct answer because you print the previous integer