二进制打印在C中不起作用 [英] Binary print not working in C

查看:65
本文介绍了二进制打印在C中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c中32位的位掩码打印二进制文件,但是在if语句中不会显示二进制表示形式.

I'm trying to print binary using bit mask of 32 bit in c but the binary representation is not getting printed in the if statement.

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)

PS: the program is now only working for 2 but still not printing the other values (255, 32, -1, -255)

推荐答案

如果进行了BLUEPIXY和我本人建议的修正,则代码将以正确的顺序产生带有答案的位.

If the fixups suggested by BLUEPIXY and myself are made, the code will produce the answer with the bits in the correct sequence.

#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

尽管可以轻松地将循环体转换为void print_binary(unsigned value)之类的函数,然后从循环中调用该函数.被编辑的问题几乎具有相同的循环,被写出六次(!)次,这是一种愚蠢的行为-不要编写这样的代码.当您像这样复制'n'粘贴代码时,会有一个函数等待编写.

The body of the loop could be converted into a function such as void print_binary(unsigned value) with minimal effort, and that would then be called from a loop. The edited question with more or less the same loop written out six(!) times is a travesty — do not write code like that. When you copy'n'paste code like that, there is a function waiting to be written.

#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的二进制表示形式,而无需添加任何修饰.通常可以合理地使用它.在这种特定情况下,您可以编写包装函数 处理其他格式:

This function can be used to print the binary representation of an unsigned int without adding any decoration. It can be used reasonably generally. In this specific context, you might write a wrapper function to handle the other formatting:

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

只要范围内具有print_binary()的原型,就不需要强制转换.从C99开始,必须存在一个函数声明,但这不必是原型.但是,在没有原型的情况下进行编译是很愚蠢的.并且您应该找到确保您的编译器在尝试跳过时抱怨的选项.对于GCC,您可以使用:

The cast is not necessary as long as you have a prototype for print_binary() in scope. From C99 onwards, you must have a function declaration present, but that doesn't have to be a prototype. However, compiling without prototypes present is silly. And you should find the options that ensure your compiler complains if you try to skimp. For GCC, you might use:

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

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

You might or might not add -Wold-style-declaration or -Wold-style-definition depending on the code base you're dealing with and the version of GCC you're using (and on how careless you are in writing code). There are other options you'd consider, like -Wshadow, but if your code compiles cleanly with what's shown, it is unlikely to run into many problems that aren't logic problems.

定义了fmt_binary()后,您可以编写main():

With fmt_binary() defined, you can write 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中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆