Base conversion
I know usual conversion from oct_开发者_JAVA技巧to_dec. Some smarter way?
It would help to know WHY you want to do this via bit masking, because sometimes there are better ways to solve your problem globally, rather than this small request.
I have a feeling this is for homework, as Googling this problem found me forums with the same query as homework. If this is homework, then please tag it as homework as you did with the other question you asked recently.
I managed to find this site thanks to Google Perhaps it will help you understand...
void convertBase(int decimal) //Function that convert decimal to base of 8
{
const int mask1 = (7 << 3);
const int mask2 = (7 << 0);
firstDigit = (decimal & mask1) + '0';
secondDigit = (decimal & mask2) + '0';
printf("Octal Representation of Binary Number: %d%d\n", firstDigit, secondDigit);
}
This function reads an octal string and returns its numerical value.
int parse_octal(const char* s) {
int r = 0;
for ( ; *s; s++)
r = (r << 3) | (*s & 7);
return r;
}
It uses a bit mask for extracting the relevant bits of the ASCII value.
精彩评论