Number Complement The complement of a number is defined here as the number\'s bi
ID: 3841494 • Letter: N
Question
Number Complement The complement of a number is defined here as the number's bitwise inversion from its highest-order 1-bit through its lowest-order bit. For example, the number n 5 is represented as 00000101 in binary. The binary complement of n is 010, which is 2 in decimal notation. Complete the getlntegerComplement function in your editor. It has has one parameter: a base-10 integer, n. This function must return the complement of n as a base-10 integer Input Format Locked stub code in the editor reads a single integer, n from stdin and passes it to the function Constraints o s n s 105 Output Format Return an integer denoting the complement of n. Sample Input 0 50 Sample output 0 13 Explanation 0Explanation / Answer
#include <iostream>
using namespace std;
void binaryconvert(int num)
{
int ans, bin;
if(num<=1)
{
cout<<num;
return;
}
ans = num%2;
binaryconvert(num/2);
cout<<ans;
}
int getintegercomplement(int num)
{
int ans;
if(num<=1)
{
cout<<!num;
return 0;
}
ans = num % 2;
getintegercomplement(num/2);
cout<<!ans;
}
int main()
{
int deci, res,ans;
long bin, dec = 0, rem, base = 1;
cout<<" Enter the Decimal Number:";
cin>>deci;
if(deci<0)
{
cout<<" Enter Number greater than 0";
}
else
{
cout<<" Binary Equivalent of " <<deci <<" is:";
binaryconvert(deci);
cout<<" one's Compliment is:";
getintegercomplement(deci);
}
return 0;
}
output
Enter the Decimal Number:100
Binary Equivalent of 100 is:1100100
one's Compliment is:0011011
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.