简体   繁体   English

将十进制数转换为二进制数

[英]converting decimal number to binaric

I wrote a code that converts integer decimal number to binary.我写了一个将十进制整数转换为二进制的代码。 I'm trying to make it print all the number until the number given instead of printing the exact same number each time.我试图让它打印所有数字,直到给出的数字,而不是每次打印完全相同的数字。 for example, for input = 3 it will print 0,1,2,3 in binariy.例如,对于 input = 3,它将以二进制形式打印 0,1,2,3。 this is my code:这是我的代码:

printf("Please enter an integer number\n");
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
    printf("%d ==   ", n);

    for (c = 5; c >= 0; c--)
    {
        k = n >> c;

        if (k & 1)
            printf("1");
        else
            printf("0");
    }

    printf("\n");
}
system ("pause");
return 0;

thanks guys!谢谢你们!

You need to work with i in the loop, not n你需要在循环中使用 i,而不是 n

#include "stdio.h"



int main()
{
    int c,k,n,i = 0;
    printf("Please enter an integer number\n");
    scanf_s("%d", &n);
    for (i = 0; i < n; i++)
    {
        printf("%d ==   ", i);

        for (c = 5; c >= 0; c--)
        {
            k = i >> c;

            if (k & 1)
                printf("1");
            else
                printf("0");
        }

        printf("\n");
    }

    return 0;
}

You can also simply output the single characters with putchar() , no need to call the variadic printf to output a fixed character within the format string (though a good compiler would likely make the optimization for you.您也可以简单地使用putchar()输出单个字符,无需调用可变参数printf来输出格式字符串中的固定字符(尽管一个好的编译器可能会为您进行优化。

You can do something similar to:你可以做类似的事情:

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

int main (void) {

    unsigned n;
    fputs ("enter an integer number: ", stdout);
    if (scanf ("%u", &n) == 1) {
        for (unsigned i = 0; i < n; i++) {
            printf ("%d ==   ", i);
            for (unsigned s = 0; s < sizeof s * CHAR_BIT; s++)
                putchar ((i >> (31 - s)) & 1u ? '1' : '0');
            putchar ('\n');
        }
    }
}

Example Use/Output示例使用/输出

$ ./bin/binslessthann
enter an integer number: 10
0 ==   00000000000000000000000000000000
1 ==   00000000000000000000000000000001
2 ==   00000000000000000000000000000010
3 ==   00000000000000000000000000000011
4 ==   00000000000000000000000000000100
5 ==   00000000000000000000000000000101
6 ==   00000000000000000000000000000110
7 ==   00000000000000000000000000000111
8 ==   00000000000000000000000000001000
9 ==   00000000000000000000000000001001

(of course there is no need for the full 32-bits for small numbers, but they were included for completeness) (当然,对于小数来说,不需要完整的 32 位,但为了完整起见,它们被包含在内)

Unpadded Binary Representation未填充的二进制表示

As noted in the comments, an unpadded version makes the results much easier to read.如评论中所述,未填充的版本使结果更易于阅读。 (the quote was "horrible leading zeros") To output the unpadded version, you can simply check first if the value is zero, if so simply output zero, for values with ones-bits present, you check whether ones bits remain in the value after the shift, if so, continue outputting bits, otherwise don't output anything further, eg (引用是“可怕的前导零”)要输出未填充的版本,您可以简单地先检查值是否为零,如果是,则简单地输出零,对于存在一个位的值,您检查值中是否保留了一个位移位后,如果是,继续输出位,否则不再输出任何内容,例如

int main (void) {

    unsigned n;
    fputs ("enter an integer number: ", stdout);
    if (scanf ("%u", &n) == 1) {
        for (unsigned i = 0; i < n; i++) {
            printf ("%d ==   ", i);
            if (!i)
                putchar ('0');
            else {
                unsigned s = sizeof s * CHAR_BIT;
                while (s--) {
                    unsigned remain = i >> s;
                    if (remain)
                        putchar ((i >> s) & 1u ? '1' : '0');
                }
            }
            putchar ('\n');
        }
    }
}

Example Use/Output示例使用/输出

$ ./bin/binslessthanrem
enter an integer number: 10
0 ==   0
1 ==   1
2 ==   10
3 ==   11
4 ==   100
5 ==   101
6 ==   110
7 ==   111
8 ==   1000
9 ==   1001

Look things over and let me know if you have further questions.仔细检查一下,如果您还有其他问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM