0
In c++ i want to make a program in which, output is the difference between N(input) and the next highest number in power of 2
for example if an input 5 is given then, output =8-5=3 if input 19 is given then, output=32-19=13 please help me with the code i should use
4 ответов
+ 4
// n is input
int x=n, y=1;
while(x>>=1) y<<=1;
return y-n;
+ 1
// If you enter a number which is already power of 2 then I go for next power of two.
#include <iostream>
#include <cmath>
using namespace std;
int getCount(int n){
int i = 0;
while(n){
n>>=1;
i++;
}
return i;
}
int main() {
int n;
std::cin >> n;
int ans = pow(2,getCount(n));
std::cout << ans << "-" << n << "=" << ans-n;
return 0;
}
+ 1
thank you sir