大整数类的位操作?

发布于 2024-10-08 00:31:16 字数 378 浏览 0 评论 0原文

我在为 C++ 中的大整数类提出算法时遇到问题。我最初的想法是使用数组/列表,但效率非常低。然后我发现了类似以下课程的内容: http://www.codeproject.com/KB/cpp/CppIntegerClass.aspx

然而,我发现这种方法确实令人困惑。我不知道如何使用位操作,而且我几乎看不懂代码。有人请向我解释如何利用位操作,它是如何工作的,等等。最终我想创建我自己的大整数类,但我几乎不是一个新手程序员,我刚刚学会了如何使用类。

基本上我的问题是: 如何使用位操作来创建大整数类?它是如何工作的?

谢谢!

I'm having a problem coming up with an algorithm for a big integer class in C++. My initial idea was using arrays/lists, but it's very inefficient. I then discovered about things like the following class:
http://www.codeproject.com/KB/cpp/CppIntegerClass.aspx

However, I find that approach really confusing. I don't know how to work with bit manipulations, and I barely understood the code. Someone please explain to me how to utilise bit manipulation, how it works, etc. Eventually I would like to create my own big integer class, but I'm barely a novice programmer and I just learned how to use classes.

Basically my question is:
How do I use bit manipulation to create a big integer class? How does it work??

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-10-15 00:31:16

首先阅读一般的二进制数。该页面显示了常见算术运算(加法、减法等)如何对二进制数进行操作,即如何对数字进行逐位操作以获得所需的结果。

一旦您知道为什么使用位操作操作,将其映射到 C++ 等编程语言应该非常简单。

根据我的经验,在实现类似的东西时,最明显的面向位的事情是位测试,以检查溢出。假设您将大二进制数表示为 uint16_t 数组,即 16 位块。实现加法时,您将从两个数字的最低有效端开始,然后将它们相加。如果总和大于 65,535,则需要将 1“进位”到下一个 uint16_t,就像一次一位一位地添加十进制数字一样。

这可以通过如下测试来实现:

const uint16_t *number1;
const uint16_t *number2;

/* assume code goes here to set up the number1 and number2 pointers. */

/* Compute sum of 16 bits. */
uint16_t carry = 0;
uint32_t sum = number1[0] + number2[0];

/* One way of testing for overflow: */
if (sum & (1 << 16))
 carry = 1;

这里, 1 << 16 表达式通过将 1 向左移动十六步来创建掩码。 & 按位与运算符根据掩码测试总和;如果 sum 中设置了第 16 位,则结果将为非零(即,在 C++ 中为 true)。

Start by reading up on binary numbers in general. That page shows how the common arithmetic operations (addition, subtraction etc) work on binary numbers, i.e. how the numbers are manipulated bit by bit to get the desired result.

Mapping that into a programming language such as C++ should be pretty straight-forward once you know why there are bit-manipulating operations being used.

In my experience, the most obvious bit-oriented thing needed when implementing something like this is bit testing, to check for overflow. Let's say you represent your big binary number as an array of uint16_t, i.e. chunks of 16 bits. When implementing addition, you will start at the least significant end of both numbers, and add those. If the sum is larger than 65,535, you need to "carry" one to the next uint16_t, just as when you add decimal numbers one digit at a time.

This can be implemented with a test like so:

const uint16_t *number1;
const uint16_t *number2;

/* assume code goes here to set up the number1 and number2 pointers. */

/* Compute sum of 16 bits. */
uint16_t carry = 0;
uint32_t sum = number1[0] + number2[0];

/* One way of testing for overflow: */
if (sum & (1 << 16))
 carry = 1;

Here, the 1 << 16 expressions creates a mask by shifting a 1 sixteen steps to the left. The & bitwise and operator tests the sum against the mask; the result will be non-zero (i.e. true, in C++) if bit 16 is set in sum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文