1

我目前正在编写一个 C 函数,该函数从用户那里获取一个数字并将其转换为二进制输出。首先,这是代码:

void Convert_Number_To_Binary(const int num,char *binary) {
    double remainder = num;

    //Start from binary[0] and check if num is divisible by 2^ith power by shifting
    for(int i = 0; i < 33; i++) {
        int shift = num >> i; //shift the current bit value of remainder i bits
        printf("i: %d, %d \n", i,  (shift) );

        //If shift is greater than 0, then remainder is divisible by 2^i
        if( (shift & 1) > 0) {
                binary[32-i] = '1';
        }
        else
                binary[32-i] = '0';
        //printf("i:%d, 32-i:%d\n", i, (32-i));
    }

    //printf("%c, %c", binary[0], binary[31]);

    binary[33] = '\0';
}

该代码在大多数情况下都可以正常工作,除了当我输入一个奇数(例如:17)时,我在最重要的位置得到一个:

num = 17    binary = 100000000000000000000000000010001

偶数不出现前导“1”:

num = 16    binary = 000000000000000000000000000010000

我在远程 32 位 linux 机器上运行它,这可能是原因吗?

4

2 回答 2

2

您正在创建一个 33 位而不是 32 位的二进制字符串:

for(int i = 0; i < 33; i++) {
    int shift = num >> i; //shift the current bit value of remainder i bits

假设 anint是 32 位宽,在循环的最后一次迭代中,您移动的量与您正在移动的变量的大小相同。这样做会调用未定义的行为这在C 标准的第 6.5.7p3 节中记录了有关按位移位运算符的内容:

对每个操作数执行整数提升。结果的类型是提升的左操作数的类型。如果右操作数的值为负数或大于或等于提升的左操作数的宽度,则行为未定义。

将循环的停止点更改为 32,并相应地调整减法和空终止字节的设置。

void Convert_Number_To_Binary(const int num,char *binary) {
    //Start from binary[0] and check if num is divisible by 2^ith power by shifting
    for(int i = 0; i < 32; i++) {
        int shift = num >> i; //shift the current bit value of remainder i bits
        printf("i: %d, %d \n", i,  (shift) );

        //If shift is greater than 0, then remainder is divisible by 2^i
        if( (shift & 1) > 0) {
                binary[32-i-1] = '1';
        }
        else
                binary[32-i-1] = '0';
        //printf("i:%d, 32-i-1:%d\n", i, (32-i-1));
    }

    //printf("%c, %c", binary[0], binary[31]);

    binary[32] = '\0';
}
于 2020-06-22T02:23:30.830 回答
0

您应该首先将intto强制转换unsigned int为 0,这将强制 MSB 填充 0。

像这样的东西:

unsigned int shift = (unsigned int)num >> i;
于 2020-06-22T01:53:58.763 回答