Bloomberg LP Interview Question
Software Engineer / Developers Financial Software DevelopersHow abt
if((num%2)!=0)
num is not power of 2
else
num is power of 2
As 2 to the power x is divisible by 2 and reminder is 0
Correct me if m wrong ....
@Phenom
the if condition should be:
if(num && (num & (num-1))==0) // to handle the case where input is 0
Mandar, you're so fucking dumb. a 1 at the doesn't hold for any powers of two other than 1. what about 4, is there a 0 in MSB?
What you need to check is the following as one of the posts above mentioned.
if(num != 0 && (num & (num - 1) == 0)
return true;
else
return false;
// Check no of bits set because in a no which is a power of 2 only the MSB is set.
void check ( int no ) {
int temp = 0x00000001, cnt ;
if ( no ) {
for ( i = 0; i < 32; i++ ) {
temp = temp << i;
if ( temp & no != 0 )
cnt++ ;
}
}
if( cnt == 1 )
printf (" No. is a multiple \n");
else
printf("not a multiple\n");
}
<pre lang="java" line="1" title="CodeMonkey42535" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
</pre><pre title="CodeMonkey42535" input="yes">1
2
10
42
11
</pre>
static boolean isPower2(int number){
if(number == number - (number & (number - 1)) && number != 1){
return true;
}
else return false;
}
As has been stated numerous times already, simply &ing the number and the number minus one will get you the correct answer (as long as you also check for the zero case).
The following code also works. It is less elegant, but it more exhaustively goes through the byte without being too cumbersome.
int count = 0;
for(int i = 0; i<sizeof(aInt); i++){
count+=aInt & 1;
if(count > 1)
return false;
aInt>>=1;
}
printf("%d\n", count);
return true;
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
cin >> n;
double resF= log10(n)/log10(2);
int resI = log10(n)/log10(2);
double resF2 = (double) resI;
if (resF == resF2)
{
cout << "n is of power 2" << endl;
}
else
{
cout << "n is NOT of power 2" << endl;
}
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
cin >> n;
double resF= log10(n)/log10(2);
int resI = log10(n)/log10(2);
double resF2 = (double) resI;
if (resF == resF2)
{
cout << "n is of power 2" << endl;
}
else
{
cout << "n is NOT of power 2" << endl;
}
return 0;
}
{{
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
cin >> n;
double resF= log10(n)/log10(2);
int resI = log10(n)/log10(2);
double resF2 = (double) resI;
if (resF == resF2)
{
cout << "n is of power 2" << endl;
}
else
{
cout << "n is NOT of power 2" << endl;
}
return 0;
}
}}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
cin >> n;
double resF= log10(n)/log10(2);
int resI = log10(n)/log10(2);
double resF2 = (double) resI;
if (resF == resF2)
{
cout << "n is of power 2" << endl;
}
else
{
cout << "n is NOT of power 2" << endl;
}
return 0;
}
One of the most efficient method to check whether a given number is a power of two or not we will check for the & of the number and the number before itself and then for fulfilling the condition of 0 we have to check for && operation.
Implementation:
#include<bits/stdc++.h>
using namepace std;
bool returnpower(int n){
return n && (!(n & (n - 1)))
}
"Find if a num is a power of 2 as fast as possible"
- Phenom October 08, 2009{
if((num & (num-1))==0)
num is a power of 2
else
num is nota a power of 2
}