Question

Write a short C++ function, isTwoPower, that takes an int i and returns true if and...

Write a short C++ function, isTwoPower, that takes an int i and returns true if and only if i is a power of 2. Do not use multiplication or division,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

#include<iostream>
using namespace std;
bool isTwoPower(int i)
{
// use of bitwise AND (&) operator
       if((i&(i-1))==0)
           return true;
       else
           return false;
}

int main()
{
   // declaring the array n
   int i;
   cout<<"Enter integer: ";
   cin>>i;
   if(isTwoPower(i))
   cout<<"True";
   else
   cout<<"False";
  
   return 0;  
}

main.cpp 1 #include<iostream> 2 using namespace std; 3 bool isTwoPower(int i) 4- { 5 // use of bitwise AND (&) operator 6 if(

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a short C++ function, isTwoPower, that takes an int i and returns true if and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT