将整数转换为二进制 [英] Convert Integer to Binary

查看:127
本文介绍了将整数转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在C?


是否有某个公式?

解决方案

当然。一般的想法通过使用余数来工作。例如,假设输入值为9.


9/2 ^ 4 = 0余数9

9/2 ^ 3 = 1余数1

1/2 ^ 2 = 0余数1

1/2 ^ 1 = 0余数1

1/2 ^ 0 = 1余数0


[一般等式是(余数/ 2 ^ i)]


因此返回的输出将是:01001 - 相当于9

在代码中执行此操作的最直接的方式(但不是最有效)(对于0< = input< 256)是:

展开 | 选择 | Wrap | 行号


总是可以用任何语言进行转换,只需要进行数学运算。

我?猜测整数是在10基数


所以说你有23号要转换成二进制


23-(2 ^ 4)= 7

1


7-(2 ^ 3)=投入零

0

7-(2 ^ 2)= 3

1


3-(2 ^ 1)= 1

1


1-(2 ^ 0)= 0

1


所以23基10 = 10111二进制


并且有人可以检查我的工作以确保其正确。


为什么我设置y = pow(x,1/3 ),结果总是1.000000?


例如


y = pow(8,1 / 3)--- > Y = 1.000000

Is it possible in C?

Is there a certain formula?

解决方案

Sure. The general idea works by using remainders. For example, suppose the input value is 9.

9 / 2^4 = 0 remainder 9
9 / 2^3 = 1 remainder 1
1 / 2^2 = 0 remainder 1
1 / 2^1 = 0 remainder 1
1 / 2^0 = 1 remainder 0

[the general equation is (remainder / 2^i)]

So the returned output would be: "01001" -- which is equivalent to 9

The most straight-forward way (but not most efficient) to do this in code (for 0 <= input < 256) is:

Expand|Select|Wrap|Line Numbers


It is always possible to convert in any language you just have to do the math.

I?m guessing the integer is in base 10

So say you have the number 23 that you want to convert into binary

23-(2^4)=7
1

7-(2^3) =put in a zero
0

7-(2^2) =3
1

3-(2^1)=1
1

1-(2^0)=0
1

so 23 base 10 = 10111 binary

And can someone check my work to make sure its correct.


Why do i set y=pow(x,1/3), the result always be "1.000000"?

For example

y=pow(8,1/3) ---> y=1.000000


这篇关于将整数转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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