gpt4 book ai didi

c - 二进制打印在 C 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:38 27 4
gpt4 key购买 nike

我正在尝试使用 c 中的 32 位位掩码打印二进制,但二进制表示没有在 if 语句中打印。

unsigned int bit_mask = 2147483648;
int decimal = 2;
printf("\nBinary representation of 2: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}

decimal = 255;
printf("\n\nBinary representation of 255: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}

decimal = 32;
printf("\n\nBinary representation of 32: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}

decimal = -1;
printf("\n\nBinary representation of -1: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}

decimal = -255;
printf("\n\nBinary representation of -255: \n");
while(bit_mask > 0){
if((decimal & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}

int random_number = (rand() % INT_MAX) + (rand() % INT_MIN);
printf("\n\nBinary representation of %d: \n", random_number);
while(bit_mask > 0){
if((random_number & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask = bit_mask >> 1;
}

PS:该程序现在仅适用于 2,但仍未打印其他值(255、32、-1、-255)

最佳答案

如果按照 BLUEPIXY 和我自己的建议进行修正,代码将生成位序列正确的答案。

#include <stdio.h>

int main(void)
{
for (unsigned value = 2; value < 1024; value = value * 3 + 1)
{
unsigned bit_mask = 0x80000000; // 2147483648

printf("Binary representation of %3u: ", value);
while (bit_mask > 0)
{
if ((value & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask >>= 1;
}
putchar('\n');
}
return 0;
}

输出:

Binary representation of   2: 00000000000000000000000000000010
Binary representation of 7: 00000000000000000000000000000111
Binary representation of 22: 00000000000000000000000000010110
Binary representation of 67: 00000000000000000000000001000011
Binary representation of 202: 00000000000000000000000011001010
Binary representation of 607: 00000000000000000000001001011111

不管BLUEPIXY如何,输出都没有反转在 comments 中说.


循环体可以被转换成一个函数,例如 void print_binary(unsigned value) 以最小的努力,然后从循环中调用它。带有或多或少相同循环的编辑问题被写了六次(!) 是一种讽刺——不要写那样的代码。当您像那样复制'n'粘贴代码时,就会有一个函数等待编写。

#include <stdio.h>

void print_binary(unsigned value)
{
unsigned bit_mask = 0x80000000; // 2147483648

while (bit_mask > 0)
{
if ((value & bit_mask) == 0)
printf("0");
else
printf("1");
bit_mask >>= 1;
}
}

此函数可用于打印 unsigned int 的二进制表示而不添加任何修饰。一般都能合理使用。在这个特定的上下文中,您可以编写一个包装函数处理其他格式:

void fmt_binary(int value)
{
printf("Binary representation of %3d: ", value);
print_binary((unsigned)value);
putchar('\n');
}

只要范围内有 print_binary() 的原型(prototype),就不需要强制转换。从 C99 开始,您必须有一个函数声明,但不一定是原型(prototype)。然而,在没有原型(prototype)的情况下进行编译是愚蠢的。你应该找到确保你的编译器在你试图跳过时提示的选项。对于 GCC,您可以使用:

gcc -std=c11 -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes …

您可能会或可能不会添加 -Wold-style-declaration-Wold-style-definition 取决于您处理的代码库和您正在使用 GCC(以及您在编写代码时有多粗心)。您还可以考虑其他选项,例如 -Wshadow,但如果您的代码可以按照显示的内容进行干净地编译,则不太可能遇到许多不是逻辑问题的问题。

定义了fmt_binary(),你可以这样写main():

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// … declarations or definitions of fmt_binary and print_binary

int main(void)
{
for (int value = 2; value < 1024; value = value * 3 + 1)
fmt_binary(value);
fmt_binary(2);
fmt_binary(255);
fmt_binary(32);
fmt_binary(-1);
fmt_binary(-255);
srand(time(0)); // Better than no call to srand()
int random_number = (rand() % INT_MAX) + (rand() % INT_MIN);
fmt_binary(random_number);
return 0;
}

示例输出可能是:

Binary representation of   2: 00000000000000000000000000000010
Binary representation of 7: 00000000000000000000000000000111
Binary representation of 22: 00000000000000000000000000010110
Binary representation of 67: 00000000000000000000000001000011
Binary representation of 202: 00000000000000000000000011001010
Binary representation of 607: 00000000000000000000001001011111
Binary representation of 2: 00000000000000000000000000000010
Binary representation of 255: 00000000000000000000000011111111
Binary representation of 32: 00000000000000000000000000100000
Binary representation of -1: 11111111111111111111111111111111
Binary representation of -255: 11111111111111111111111100000001
Binary representation of -1758826555: 10010111001010100110111111000101

关于c - 二进制打印在 C 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46780484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com