Code to do binary inversion of a number in C++
#include <iostream>
using namespace std;
int main() {
unsigned int ip_num,op_num,i=0,tmp;
cin>>ip_num;
tmp = ip_num;
while(tmp!=0)
{
tmp = tmp>>1;
i++;
}
//i = 8-i;
//i = 16 -i;
i = 32-i;
op_num = ~ip_num;
op_num = op_num<<i;
/* Any bit value right shifted should be re-initialised to zero */
//op_num = op_num&255;
//op_num = op_num&65535;
op_num = op_num&4294967295;
op_num = op_num>>i;
cout<<op_num;
return 0;
}
Code to display Binary value of a number in C++ upto 16 binary digit.
void toBin(unsigned int n){
int i=0;
//while(n && i!=16){
// int res = n&32768;
if(res)
cout<<1;
else
cout<<0;
n = n<<1;
i++;
}
cout<<endl;
}
#include <iostream>
using namespace std;
int main() {
unsigned int ip_num,op_num,i=0,tmp;
cin>>ip_num;
tmp = ip_num;
while(tmp!=0)
{
tmp = tmp>>1;
i++;
}
//i = 8-i;
//i = 16 -i;
i = 32-i;
op_num = ~ip_num;
op_num = op_num<<i;
/* Any bit value right shifted should be re-initialised to zero */
//op_num = op_num&255;
//op_num = op_num&65535;
op_num = op_num&4294967295;
op_num = op_num>>i;
cout<<op_num;
return 0;
}
Code to display Binary value of a number in C++ upto 16 binary digit.
void toBin(unsigned int n){
int i=0;
//while(n && i!=16){
// int res = n&32768;
if(res)
cout<<1;
else
cout<<0;
n = n<<1;
i++;
}
cout<<endl;
}