Reputation: 102
My Problem Instructions: Create a method which determines if an integer argument is power of 2. It should have the following heading:
private static int powerOf2(int aNumber)
If the number passed as the argument to your method is an integral power of 2, then return the power. If the number passed in aNumber is not an integral power of 2 (or not a positive number) then return -1.
Do not use any methods from Java's Math class.
My Question: I dont understand what the problem is asking for, return the power of 2 of a integer then return the power converted? Im not even sure whats being asked.
My Code:
private static int powerOf2(int aNumber)
{
if(aNumber % 2 != 0)
{
return -1;
}
else
{
System.out.println(aNumber + " is 2 raised to ");
while (((aNumber % 2) == 0) && aNumber > 0)
{
// While aNumber is even and > 0
aNumber /= 2;
}
System.out.print(+ aNumber);
return aNumber;
}
}
Conclusion: Really in depth explanation of what the problem is asking me to do, and what does it mean to check the power of 2?
Upvotes: 0
Views: 1062
Reputation: 42009
Such a method can use the Integer.numberOfTrailingZeros(...) method. This gives the power of 2 if the number is an integral power of 2, and we can simply test to see if we're right.
public static int powerOf2(int aNumber) {
int trailingZeros = Integer.numberOfTrailingZeros(aNumber);
if (aNumber == (1 << trailingZeros)) {
return trailingZeros;
} else {
return -1;
}
}
Upvotes: 1
Reputation: 273860
What the question means is basically how many 2s multiplied together will get you the number passed in.
If the number passed in is 8, then you return 3, because three 2s multiplied together, 2x2x2 is 8.
If not for the restriction of not using anything from the Math
class, you could do this just by doing something like:
if (aNumber < 0) return -1; int power = (int)(Math.log(aNumber) / Math.log(2)); return (int)Math.pow(2, power) == aNumber ? power : -1;
With the restrictions, you could do some bhtshifting, in addition to what other answerers have said.
private static int powerOf2(int aNumber)
{
if (aNumber == 0) return 0;
if (aNumber < 0) return -1;
int count = 0;
while (aNumber != 0) {
int shifted = aNumber >> 1;
if (shifted * 2 != aNumber) return -1;
aNumber = shifted;
count++;
if (aNumber == 1) {
return count;
}
}
return -1;
}
Upvotes: 0
Reputation: 11
Your method should return a number that represents an exponent.
If you input 8, the program should output 3 (since 2 x 2 x 2 = 8). If you input either a number that isn’t a power of 2, or is negative, it should output -1. Hope that makes sense!
Upvotes: 0
Reputation: 2795
What you are asked for is to check if the number passed as an argument to the function is a power of 2, and if it is, what is the power of 2 that sums up to aNumber
. (2^x = aNumber
) - you want to find x
.
For example if you pass 8 to the function, you should return 3, since 2^3 = 8
. But if the number isn't the power of 2, you should return -1 - for example if the parameter is 9, there is no integral power of 2 that can result in 9.
Regarding your program, you can make it work with some minor modifications:
What you want to do is make a loop and in every iteration check if the input number is dividable by 2 (aNumber % 2 == 0
), and if it is divide it by two (aNumber = aNumber / 2
). If you can get to 1 in this way, that means that your number is a power of two, and you just have to count the iterations (number of times you divided aNumber
by 2). So your function might look like this:
private static int powerOf2(int aNumber)
{
int power = 0;
if(aNumber % 2 != 0)
{
return -1;
}
else
{
System.out.print(aNumber + " is 2 raised to ");
while (true)
{
if(aNumber % 2 == 0){
aNumber /= 2;
power++;
if(aNumber == 1) return power;
}else{
return -1;
}
}
}
}
Upvotes: 2
Reputation: 638
In maths base^exponent=term
Eg:2^3=8 here base=2,exponent=3 and term=8
What the problem will give you is a term and what it need is the exponent with base fixed as 2.It needs to have the case that term may not be possible with integer values for exponent
Eg:2^4.473=20(approx).For such cases it needs you to return -1 and for cases with integer exponents that is perfect powers of 2 it needs the exponent
Have a look at the code to understand
private static int powerOf2(int aNumber) {
int cnt = 0;
System.out.println(aNumber + " is 2 raised to ");
while ((((aNumber % 2) == 0) && aNumber > 0)) {
// While aNumber is even and > 0
aNumber /= 2;
cnt++;
}
if (aNumber == 1)
return cnt;
else
return -1;
}
Upvotes: 1
Reputation: 28289
Power of 2 means you can get the number by multiplying digit 2, for example:
If the input is 1, then you should return 0 since 20 = 1;
If the input is 2, then you should return 1 since 21 = 2 * 1 = 2;
If the input is 4, then you should return 2 since 22 = 2 * 2 = 4;
If the input is 8, then you should return 2 since 23 = 2 * 2 * 2 = 8;
...
If the input is 0 or negative, you should return -1;
If the input is not a power of 2(for example 3, 5, 7, 10), you should return -1;
Upvotes: 2