将整数转换为二进制

编程入门 行业动态 更新时间:2024-10-28 19:24:03
本文介绍了将整数转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有可能在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

更多推荐

将整数转换为二进制

本文发布于:2023-06-05 01:22:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   转换为

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!