Int to Binary Conversion explanation

2020-02-16 02:42发布

My question is based on this post: Decimal to Binary and it's chosen solution.

I can get the chosen answer code working, but it only works for 5 bits. How do I modify this code to work for larger numbers, say 8 bits?

I tried just adjusting the character offset in the fist line from 5 to 8, but no success.

void getBin(int num, char *str)
{
  *(str+5) = '\0';
  int mask = 0x10 << 1;
  while(mask >>= 1)
    *str++ = !!(mask & num) + '0';
}

and test with the given code, again adjusting the 6 to 9 to match the function above:

int main()
{
  char str[6];
  getBin(10, str);
  printf("%s\n", str);
  return 0;
}

but the output still only shows the first five bits and then gives random symbols. Can someone please explain what exactly is happening when I adjust those numbers so I can get this to work for an 8 (or any other size) bit conversion?

3条回答
手持菜刀,她持情操
2楼-- · 2020-02-16 03:19

I'm not good at english, sorry. You need to adjust the local variable 'mask' too.

#include <stdio.h>

void getBin(int num, char *str)
{
  *(str+8) = '\0';
  int mask = 0x80 << 1;
  while(mask >>= 1)
    *str++ = !!(mask & num) + '0';
}

int main()
{
  char str[9];
  getBin(10, str);
  printf("%s\n", str);
  return 0;
}

I want to explain why this code is working well. but... I'm not good at english... I just hope it's helpful/

查看更多
聊天终结者
3楼-- · 2020-02-16 03:22

After accept answer.

Too many magic numbers.

In the original, there are constants 5, 0x10, 6 that do not show there relationship to the goal of a 5 binary digit number.

Then when going to 8, the constants 8, 0x10, 9 were used. Since 0x10 was not adjusted, code failed.

Instead approach the problem with a more general point-on-view and use code that eliminates or better expresses these magic numbers.

#define BitWidth 5

void getBin(int num, char *str)
{
  int mask = 1 << (BitWidth - 1);
  // skip the original shift left and then immediately shift right
  do {
    *str++ = !!(mask & num) + '0';
  } while (mask >>= 1);
  *str = '\0';  // by appending at the end, no need for a magic number
}  

int main()
{
  char str[BitWidth + 1];
  getBin(10, str);
  printf("%s\n", str);
  return 0;
}

Note: the above approach (and other answers) have a problem when BitWidth matches the int width. But that is another issue easily solved by using unsigned math.

查看更多
神经病院院长
4楼-- · 2020-02-16 03:42
void getBin(int num, char *str)
{
  *(str+8) = '\0';
  int mask = 0x80 << 1;
  while(mask >>= 1)
    *str++ = !!(mask & num) + '0';
}

main()
{
  char str[9];
  getBin(254, str);
  printf("%s\n", str);
  return 0;
}

For 8-bit number you need array of 9 chars. Also you need to change mask, so it can mask all bits.

The mask for the most significant bit for a 5-bit number like 11111 is 10000 which is equal to 16 decimal or 10 hexadecimal. Same thing for 8-bit number. The mask is 10000000. Since the loop start with mask >>= 1 the mask is shifted one to left int mask = 0x10 << 1; to compensate. Thus to modify it for a x-bit number, define an array of x+1 chars. Put \0 at index x. Find the x-bit number where the most significant bit of it is 1 and others are 0. The number is 2^(x-1) (2 power (x-1)).

查看更多
登录 后发表回答