#include <iostream>
using namespace std;
void DtoB(int long n, int bit)
{
static int costante=bit;
try {
if(n>pow(2,costante)-1)
throw exception();
if(--bit>=0)
{
if(((1L<<bit)& n) ==0)
cout<<"0";
else
cout<<"1";
DtoB(n,bit);
}
else
cout<<endl;
} catch (exception e) {
cout<<"Attenzione Bit insufficienti\n";
}
}
int main(int argc, const char * argv[]) {
/*assegno il numero da convertire*/
int long s=43L;
/*uso un for per lo shift dei bit*/
for(int i=31;i>=0;i--)
{
/*creo la maschera di 0000000000........1*/
int long k=(1L<<i)& s;
if(k==0)
cout<<'0';
else
cout<<'1';
}
cout<<endl;
DtoB(16,9);
return 0;
}
Post Views: 226