如何以2³²为基数表示数字? [英] How to represent a number in base 2³²?

查看:143
本文介绍了如何以2³²为基数表示数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些以10为底数或以16为底数的数字,如何将其更改为以2为底数的 32 ?

If I have some base 10 or base 16 number, how do I change it into base 232?

我尝试这样做的原因是,按照此处其他成员的建议实施BigInt.

The reason I'm trying to do this, is for implementing BigInt as suggested by other members here Why to use higher base for implementing BigInt?

在2 32 之前,它是否与整数(以10为底)相同?之后会发生什么?

Will it be the same as integer (base 10) till 232? What will happen after it?

推荐答案

您正试图找到某种形式的

You are trying to find something of the form

a0 + a1 * (2^32) + a2 * (2^32)^2 + a3 * (2^32)^3 + ...

确切地说是<2> base-2 32 系统的定义,所以请忽略所有告诉您您的问题没有道理的人!

which is exactly the definition of a base-232 system, so ignore all the people that told you that your question doesn't make sense!

无论如何,您所描述的称为 基本转换 .有快速的方法,也有简单的方法来解决此问题.快速方法非常复杂(存在

Anyway, what you are describing is known as base conversion. There are quick ways and there are easy ways to solve this. The quick ways are very complicated (there are entire chapters of books dedicated to the subject), and I'm not going to attempt to address them here (not least because I've never attempted to use them).

一种简单的方法是首先在您的数字系统中实现两个功能,即乘法和加法. (即实施BigInt add(BigInt a, BigInt b)BigInt mul(BigInt a, BigInt b)).解决该问题后,您会注意到以10为底的数字可以表示为:

One easy way is to first implement two functions in your number system, multiplication and addition. (i.e. implement BigInt add(BigInt a, BigInt b) and BigInt mul(BigInt a, BigInt b)). Once you've solved that, you will notice that a base-10 number can be expressed as:

b0 + b1 * 10 + b2 * 10^2 + b3 * 10^3 + ...

也可以写成:

b0 + 10 * (b1 + 10 * (b2 + 10 * (b3 + ...

因此,如果您在输入字符串中从左到右移动,则可以一次剥离一个以10为底的数字,并使用addmul函数累加到您的BigInt中:

so if you move left-to-right in your input string, you can peel off one base-10 digit at a time, and use your add and mul functions to accumulate into your BigInt:

BigInt a = 0;
for each digit b {
    a = add(mul(a, 10), b);
}

免责声明::该方法在计算上不是高效,但是至少可以帮助您入门.

Disclaimer: This method is not computationally efficient, but it will at least get you started.

注意:从base-16进行转换要简单得多,因为2 32 是16的精确倍数.所以转换基本上是直到连接位.

Note: Converting from base-16 is much simpler, because 232 is an exact multiple of 16. So the conversion basically comes down to concatenating bits.

这篇关于如何以2³²为基数表示数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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