Problem Statement
The largest number that can fit in an integer on a computer is dependent on two parameters: the number of bits used, and whether the integer is signed or unsigned. If we denote the number of bits by n then the largest number that can fit in an integer can be calculated according to this table ('^' denote exponentiation):
- Number is signed: (2^(n-1))-1
- Number is unsigned: (2^n)-1
For instance, the largest number to fit in a 16 bit signed integer is (2^(16-1))-1 = 32767 while the largest number to fit in a 8 bit unsigned integer is (2^8)-1 = 255.
Create a class IntRange containing the method largest which takes as input an int n and a String type and returns an int, the largest number than can fit in an integer having n bits and which is signed if type equals "signed" and unsigned if type equals "unsigned" (quotes for clarity only).
Definition
- Class:
- IntRange
- Method:
- largest
- Parameters:
- int, String
- Returns:
- int
- Method signature:
- int largest(int n, String type)
- (be sure your method is public)
Constraints
- n will be between 2 and 30, inclusive.
- type will be either "signed" or "unsigned" (quotes for clarity only).
Examples
16
"signed"
Returns: 32767
As explained above, the answer is 32767 so the method returns 32767.8
"unsigned"
Returns: 255
2
"signed"
Returns: 1
29
"unsigned"
Returns: 536870911