compute short, int, long using pow

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?

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  #include <iostream>
  #include <climits>
  #include <cmath>
 
  //Included using namespace std;
  using namespace 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;
  unsigned short uShortMax = USHRT_MAX;
  unsigned int uIntMax = UINT_MAX;
  unsigned long 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;
  unsigned short uShortMaxP;
  unsigned int uIntMaxP;
  unsigned long 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(unsigned short*8) -1);
uShortMaxP = (2,(sizeof(unsigned short*8) -1);
  uIntMaxP = pow(2, (sizeof(unsigned int)*8)-1);
  uLongMaxP = pow(2, (sizeof(unsigned long)*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(unsigned int) * 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.
Last edited on
BUT, I was told that Unsigned max values should be ( 2 * signed max value +1). Which I guess I did not do? because I had points taken off for that.

How/where would I correct the formulas in my program for the unsigned max values to reflect what the instructor told me
I think your problems were in your signed values.

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
Last edited on
Check out http://www.cplusplus.com/reference/climits/

This may help you out.
Topic archived. No new replies allowed.