S
sugaray
Hi, can somebody help me out with a better version of the following
functions which only convert decimal integer to it's corresponding
binary form. the
problem i'm having now is that I can't figure out how to handle when 0
is
passed as parameter in only one function, my code below have to add
one more function to handle this situation, any help is appreciated,
thanx.
char *Dec2Bin(const int decimal) {
char *binary=new char[64];
int dividend,i;
dividend=decimal;
for(i=0;dividend!=0;++i) {
binary=(dividend&1)+'0'; // (a % b) == (a & (b-1))
dividend>>=1;
}
binary='\0';
return strrev(binary);
}
char *Decimal2Binary(const int decimal) {
return (decimal==0)?"0"ec2Bin(decimal);
}
functions which only convert decimal integer to it's corresponding
binary form. the
problem i'm having now is that I can't figure out how to handle when 0
is
passed as parameter in only one function, my code below have to add
one more function to handle this situation, any help is appreciated,
thanx.
char *Dec2Bin(const int decimal) {
char *binary=new char[64];
int dividend,i;
dividend=decimal;
for(i=0;dividend!=0;++i) {
binary=(dividend&1)+'0'; // (a % b) == (a & (b-1))
dividend>>=1;
}
binary='\0';
return strrev(binary);
}
char *Decimal2Binary(const int decimal) {
return (decimal==0)?"0"ec2Bin(decimal);
}