如何将十进制基数(10)转换为负基数(-2)? [英] How to convert a decimal base (10) to a negabinary base (-2)?

查看:154
本文介绍了如何将十进制基数(10)转换为负基数(-2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个从十进制转换为负数的程序。

I want to write a program to convert from decimal to negabinary.

我不知道如何从十进制转换为负数。

I cannot figure out how to convert from decimal to negabinary.

我不知道如何查找规则及其运行方式。

I have no idea about how to find the rule and how it works.

示例: 7(base10) -> 11011(base-2)

我只知道它是 7 =(-2)^ 0 * 1 +(-2)^ 1 * 1 +(-2)^ 2 * 0 +(-2)^ 3 * 1 +(-2)^ 4 * 1

推荐答案

该算法在 http://en.wikipedia.org/wiki/Negative_base#Calculation 。基本上,您只需要选择余数作为正的基本情况,并确保余数是非负且最小即可。

The algorithm is described in http://en.wikipedia.org/wiki/Negative_base#Calculation. Basically, you just pick the remainder as the positive base case and make sure the remainder is nonnegative and minimal.

 7 = -3*-2 + 1  (least significant digit)
-3 =  2*-2 + 1
 2 = -1*-2 + 0
-1 =  1*-2 + 1
 1 =  0*-2 + 1  (most significant digit)

这篇关于如何将十进制基数(10)转换为负基数(-2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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