This was a program I worked on early on in the semester. I was told that Unsigned max values should be ( 2 * signed max value +1). I am just trying to figure out how and where this is suppose to be coded. I tried a couple of different places but the result was always 1 for the unsigned values. Can anyone tell/show me how and where this was suppose to be coded to be right?
#include <iostream>
#include <climits>
#include <cmath>
//Included using namespace std;
usingnamespace std;
int main()
{
//Declaration of the variables and define functions for part 1 and 2 of the program
short shortMinimum = SHRT_MIN, shortMaximum = SHRT_MAX;
int intMinimum = INT_MIN, intMaximum = INT_MAX;
long longMinimum = LONG_MIN, longMaximum = LONG_MAX;
unsignedshort uShortMax = USHRT_MAX;
unsignedint uIntMax = UINT_MAX;
unsignedlong uLongMax = ULONG_MAX;
//Declaration of the variables for part 3(using pow function)
//I separated the sections in order to keep things simple and not confuse myself
short shortMinP;
short shortMaxP;
int intMinP;
int intMaxP;
long longMinP;
long longMaxP;
unsignedshort uShortMaxP;
unsignedint uIntMaxP;
unsignedlong uLongMaxP;
//Defining the variables for the pow function
shortMinP = 0- pow(2, (sizeof(short)*8-1));
shortMaxP = 2*(pow(2, (sizeof(short)*8-1)-1));
intMinP = 0- pow(2, (sizeof(int)*8-1));
intMaxP = 2* pow(2, (sizeof(int)*8-1)-1);
longMinP = 0- pow(2, (sizeof(long)*8-1));
longMaxP = 2* pow(2, (sizeof(long)*8-1)-1);
uShortMaxP = (2,(sizeof(unsignedshort*8) -1);
uShortMaxP = (2,(sizeof(unsignedshort*8) -1);
uIntMaxP = pow(2, (sizeof(unsignedint)*8)-1);
uLongMaxP = pow(2, (sizeof(unsignedlong)*8)-1);
//Printout will be the results of the following
cout <<sizeof(short) <<endl;
cout << "The short minimum value using decimal is:" <<std::dec <<shortMinimum <<endl;
cout << "The short minimum value using hexadecimal is:" <<std::hex <<shortMinimum <<endl;
cout << "The short minimum value using octal is:" <<std::oct <<shortMinimum <<endl;
cout << "The short maximum value using decimal is:" <<std::dec <<shortMaximum <<endl;
cout << "The short maximum value using hexidecimal is:" <<std::hex <<shortMaximum <<endl;
cout << "The short maximum value using octal is:" <<std::oct <<shortMaximum <<endl;
cout << endl;
cout << "The int minimum value using decimal is:" <<std::dec <<intMinimum <<endl;
cout << "The int minimum value using hexidecimal is:" <<std::hex <<intMinimum << endl;
cout << "The int minimum value using octal is:" <<std::oct <<intMinimum << endl;
cout << "The int maximum value using decimal is:" <<std::dec <<intMaximum << endl;
cout << "The int maximum value using hexadecimal is:" <<std::hex <<intMaximum <<endl;
cout << "The int maximum value using octal is:" <<std::oct <<intMaximum <<endl;
cout << endl;
cout << "The long minimum value using decimal is:" <<std::dec <<longMinimum <<endl;
cout << "The long minimum value using hexadecimal is:" <<std::hex <<longMinimum <<endl;
cout << "The long minimum value using octal is:" <<std::oct <<longMinimum <<endl;
cout << "The long maximum value using decimal is:" <<std::dec <<longMaximum <<endl;
cout << "The long maximum value using hexadecimal is:" <<std::hex <<longMaximum <<endl;
cout << "The long maximum value using octal is:" <<std::oct <<longMaximum <<endl;
cout <<endl;
cout << "The unsigned short maximum value using decimal is:" <<std::dec <<uShortMax <<endl;
cout << "The unsigned short maximum value using hexidecimal is:" <<std::hex <<uShortMax <<endl;
cout << "The unsigned short maximum value using octal is:" <<std::oct <<uShortMax <<endl;
cout << endl;
cout << "The unsigned int maximum value using decimal is:" <<std::dec <<uIntMax <<endl;
cout << "The unsigned int maximum value using hexadecimal is:" <<std::hex <<uIntMax <<endl;
cout << "The unsigned int maximum value using octal is:" <<std::oct <<uIntMax <<endl;
cout << endl;
cout << "The unsigned long maximum value using decimal is:" <<std::dec <<uLongMax <<endl;
cout << "The unsigned long maximum value using hexadecimal is:" <<std::hex <<uLongMax <<endl;
cout << "The unsigned long maximum value using octal is:" <<std::oct <<uLongMax <<endl;
cout << endl;
shortMaximum = shortMaximum + 1;
intMaximum = intMaximum + 1;
longMaximum = longMaximum + 1;
cout << "The short maximum value plus one using decimal is:" <<std::dec <<(shortMaximum) <<endl;
cout << "The int maximum value plus one using decimal is:" <<std::dec <<(intMaximum + 1) <<endl;
cout << "The long maximum value plus one using decimal is:" <<std::dec <<(longMaximum + 1) <<endl;
cout << endl;
shortMinimum = shortMinimum - 1;
intMinimum = intMinimum - 1;
longMinimum = longMinimum - 1;
cout << "The short minimum value minus one using decimal is:" <<std::dec <<(shortMinimum) <<endl;
cout << "The int minimum value minus one using decimal is:" <<std::dec <<(intMinimum - 1) <<endl;
cout << "The long minimum value minus one using decimal is:" <<std::dec <<(longMaximum - 1) <<endl;
cout << endl;
//the following will print out the results of the computation of the largest and smallest short int and lone using the pow function
cout << "The short minimum in decimal using pow function is: "<<shortMinP << endl;
cout << "The short maximum in decimal using the pow function is: " <<shortMaxP << endl;
cout << endl;
cout << "The int minimum in decimal using the pow function is: " <<intMinP << endl;
cout << "The int maximum in decimal using the pow funtion is: " <<intMaxP << endl;
cout << endl;
cout << "The long minimum in decimal using the pow function is: " <<longMinP << endl;
cout << "The long maximum in decimal using the pow function is: " <<longMaxP << endl;
cout << endl;
cout << "The unsigned short maximum in decimal using the pow function is: " <<uShortMaxP << endl;
cout << "The unsigned int maximum in decimal using the pow function is: " <<uIntMaxP << endl;
cout << "The unsigned long maximum in decimal using the pow function is: " <<uLongMaxP << endl;
cout << endl;
return 0;
}
Let's take a 4-bit number in binary. The largest unsigned number is 1111. When we convert this to decimal, we get 15. So the maximum unsigned number represented by a 4-bit number is 2*2*2*2-1=15. That's 2^4-1. 2-because we are using base 2 in a computer. 4-because that's how many bits we are using, 1-because we also need to be able to represent 0 which takes up a spot (16 combinations, 16 different numbers represented, including 0 makes 15).
So yeah, I think that the maximum number can be calculated with: sizeof(unsignedint) * 8 - 1. The sizeof returns teh size in bytes. The *8 converts that to bits. The -1 is as described above.
A REALLY easy was to do this is:
1 2 3 4 5 6 7 8 9
#include <limits>
#include <iostream>
#define TYPE unsigned char
int main()
{
std::cout << "Maximum TYPE is " << (int)std::numeric_limits<TYPE>::max();
}
Edit: not sure how to convert TYPE to a char array for the cout.
For this sort of thing, I certainly recommend sticking to 4-bit numbers until you understand the pattern and can expand the principle to other numbers.
In a signed number, the 1000 bit is the sign bit which makes it negative. Therefore the maximum number is 0111, or 7. This is 2(number of bit-1)-1.
When we start to go negative we do this:
1111 = -1
1110 = -2
1101 = -3
1100 = -4
1011 = -5
1010 = -6
1001 = -7
1000 = Not really a number (-0)
In terms of a function it is: -2(number of bits-1)+1