top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find if Given number is power of 2 using C/C++ program. Number is a very very big number?

0 votes
792 views
How to find if Given number is power of 2 using C/C++ program. Number is a very very big number?
posted Dec 6, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
#include <iostream>
using namespace std;

int main() {
    // your code goes here
    unsigned long long int num=2;
    num<<=62; // use  for( i =1 to 62 to check
    cout<<num;
    return 0;
}
i checked for num <= 9223372036854775808

1 Answer

+1 vote

Hi,

very simple , if this condition satisfies then it is power of 2 if ( ! ( num & ( num - 1 ) ) )

answer Dec 7, 2016 by Jaganathan
Condition satisfies or condition fails?
ab0ve IF statement true means it is power of two
can you explain this
awsome
Similar Questions
0 votes

C program to find and print second largest digit in the given number? (without using arrays, functions and using only one loop).

–1 vote

Write a C program to check if the given string is repeated substring or not.
ex
1: abcabcabc - yes (as abc is repeated.)
2: abcdabababababab - no

...