How To Depository Fiscal Establishment Jibe If Integer Expose Is Ability Of 2 Inwards Coffee - Iii Examples
How to depository fiscal establishment jibe if an integer lay out is a ability of 2 inwards Java is ane of the pop programming interview question in addition to has been asked inwards many interviews. Surprisingly, this occupation which looks uncomplicated plenty to answer, doesn't plow out that uncomplicated if for many developers. Many Java programmers, both freshers in addition to less experienced, struggle to write code for a function, which tin depository fiscal establishment jibe if lay out is ability of 2 or not. There could hold upwards many dissimilar reasons for that, simply it’s expected to at to the lowest degree come upwards up amongst beast strength solution. For those who are familiar amongst bitwise operators inwards Java , how positive in addition to negative numbers are represented inwards binary format, this do is quite easy. Since negative numbers are represented equally 2's complement value inwards Java, yous tin easily abide by if whatever lay out is ability of 2 or non yesteryear looking at its flake pattern. Remember checking for ability of ii is dissimilar than checking if lay out is fifty-fifty or odd, that’s about other affair to note. Influenza A virus subtype H5N1 lay out tin hold upwards even, simply it’s non necessary to hold upwards a ability of two, e.g. half dozen is fifty-fifty simply it’s non a ability of two.
3 ways to depository fiscal establishment jibe if lay out is ability of ii or not
In this article nosotros volition run across 3 uncomplicated examples to depository fiscal establishment jibe if whatever integer lay out is ability of 2 or not. We convey 3 methods, which uses bitwise operator, beast strength agency in addition to flake shit operators to solve this problem. Method which uses bitwise operators tin non depository fiscal establishment jibe if nada is ability of 2 or not, in addition to solely piece of work for integer lay out which is greater than or equal to 1. hither are code instance of 3 uncomplicated method to abide by out if a lay out is ability of 2 or non :
public degree PowerOf2Test {
public static void main(String args[]) {
int[] numbers = {0,1,2,6,8};
for(int num: numbers){
System.out.println("isPowerOfTwo()-- is " + num + " ability of ii inwards Java :" + isPowerOfTwo(num));
System.out.println("powerOfTwo()-- is " + num + " ability of ii inwards Java :" + powerOfTwo(num));
System.out.println("checkPowerOfTwo()-- is " + num + " ability of ii inwards Java :" + checkPowerOfTwo(num));
System.out.println("-----------------------------------------------------------");
}
}
/*
* checking if lay out is ability of 2 using flake shift operator inwards java
* e.g. iv inwards binary format is "0000 0000 0000 0000 0000 0000 0000 0100";
* in addition to -4 is "1111 1111 1111 1111 1111 1111 1111 1100";
* in addition to 4&-4 volition be "0000 0000 0000 0000 0000 0000 0000 0100"
*/
private static boolean isPowerOfTwo(int number) {
if(number <0){
throw new IllegalArgumentException("number: " + number);
}
if ((number & -number) == number) {
return true;
}
return false;
}
/*
* checking if lay out is ability of 2 using beast force
* starts amongst 1, multiplying amongst 2 it volition eventually hold upwards same equally master number
*/
private static boolean powerOfTwo(int number){
int foursquare = 1;
while(number >= square){
if(number == square){
return true;
}
square = square*2;
}
return false;
}
/*
* abide by if an integer lay out is ability of 2 or non using flake shift operator
*/
private static boolean checkPowerOfTwo(int number){
if(number <0){
throw new IllegalArgumentException("number: " + number);
}
return ((number & (number -1)) == 0);
}
}
Output:
isPowerOfTwo()-- is 0 ability of ii inwards Java :true
powerOfTwo()-- is 0 ability of ii inwards Java :false
checkPowerOfTwo()-- is 0 ability of ii inwards Java :true
-----------------------------------------------------------
isPowerOfTwo()-- is 1 ability of ii inwards Java :true
powerOfTwo()-- is 1 ability of ii inwards Java :true
checkPowerOfTwo()-- is 1 ability of ii inwards Java :true
-----------------------------------------------------------
isPowerOfTwo()-- is 2 ability of ii inwards Java :true
powerOfTwo()-- is 2 ability of ii inwards Java :true
checkPowerOfTwo()-- is 2 ability of ii inwards Java :true
-----------------------------------------------------------
isPowerOfTwo()-- is 6 ability of ii inwards Java :false
powerOfTwo()-- is 6 ability of ii inwards Java :false
checkPowerOfTwo()-- is 6 ability of ii inwards Java :false
-----------------------------------------------------------
isPowerOfTwo()-- is 8 ability of ii inwards Java :true
powerOfTwo()-- is 8 ability of ii inwards Java :true
checkPowerOfTwo()-- is 8 ability of ii inwards Java :true
-----------------------------------------------------------
That’s all on this article virtually checking if a lay out I ability of ii or not. Let us know if yous abide by about other agency to verify if lay out is ability of two.
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures in addition to Algorithms: Deep Dive Using Java
How to depository fiscal establishment jibe if linked listing contains loop or not
0 Response to "How To Depository Fiscal Establishment Jibe If Integer Expose Is Ability Of 2 Inwards Coffee - Iii Examples"
Post a Comment