十进制到二进制程序向后显示数字?我该如何解决? [英] Decimal To Binary Program Show Numbers Backwards? How Do I Fix This?

查看:93
本文介绍了十进制到二进制程序向后显示数字?我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C做了十进制到二进制转换器。我对C和C ++非常熟悉,但我总是很难理解位。代码给出了十进制的正确二进制数,唯一的问题是它是向后的。我使用了>>运营商和& ;.我不知道如何使用面具,因为我仍然觉得它令人困惑。对此有一些建议会很好,我会很感激。但我也想知道如何扭转这些数字,如果我使用的是面具,那么就很容易使用〜,但我不是。任何建议和评论都会很好。



这是代码:



i made decimal to binary converter with C. I am very familiar with C and C++, but i always had a hard time understanding bits. The code gives the right binary number for the decimal, the only problem is that it is backwards. I used the >> operator and &. I do not know how to use a mask for this as i still find it confusing. Some advice on that would be nice and i would appreciate it. But i also want to find out how to reverse the numbers, if i was using a mask it would be easy to just use ~, but i am not. Any advice and review would be nice.

Here is the code:

#include <stdio.h>
int main(void)
{
int i;
int dec;

scanf("%d" , &dec);

for(dec; dec > 0; dec = dec>>1)//shifting bits to the right by 1 is equivalent to dividing by power of 2.
{
if(dec & 1)//check least significant bit, if odd, then result is 1
printf("1");

else//if even then result is 0
printf("0");
}
}

推荐答案

不要使用 printf 一点一滴。相反,创建一个所需长度的字符串(可以是简单的C字符串, char * )并以相反的顺序填充它,然后立即输出字符串。或者向相反方向移动。只是一点思考,你就完成了。您应该理解,传统的二进制符号在右侧具有最低有效位,与所有其他基础一样;你做了相反的事。



你的代码很短。也许你可以走得更远,但它可以让你更好地理解。在每个迭代步骤中,创建一个掩码, 1<< index ,其中 index 是一个位数。在此掩码中,当前位号的位置只有一个set(1)位。所以,如果你和它与要表示的数字( dec&(1<<<< index)),它会给你0或非零, 0是中的测试位dec 是清除的,如果设置则不为零。



你需要要了解您没有将十进制转换为二进制。您将任何数字表示为显示位表示的字符串。你编号, dec ,尽管你的变量名称不能是十进制或十六进制。这些概念不适用于数字。你可以考虑数字,所有其他对象都是二进制的。



是的,反转是'〜',我没有看到问题。



-SA
Don't use printf bit by bit. Instead, create a string (could be as simple C string, char*) of required length and fill it in reverse order, and then output the string at once. Or shift in opposite direction. Just a bit of thinking and you are done. You should understand that traditional "binary" notation is having least significant bit on right, as with all other bases; and you did the opposite.

Your code is very short. Perhaps you could go a bit longer way, but it can give you better understanding. On each iteration step, create a mask, 1 << index, where index is a bit number. In this mask, you have only one set (1) bit in the position of the current bit number. So, if you AND it with the number to be represented (dec & (1 << index)), it will give you either 0 or non-zero, 0 is the test bit in dec is clear, not-zero if it is set.

And you need to understand that you are not "converting" decimal to "binary". You represent any number as a string showing bit representation. You number, dec, despite your variable name, cannot be "decimal" or "hexadecimal". These concepts are not applicable to numbers. You can consider numbers and all other objects are binary.

Yes, inversion is '~', I don't see a problem.

—SA


这篇关于十进制到二进制程序向后显示数字?我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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