如何正确地加/减到一个128位的数目(作为两个u_int64_t) [英] How to properly add/subtract to a 128-bit number (as two u_int64_t)

查看:304
本文介绍了如何正确地加/减到一个128位的数目(作为两个u_int64_t)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C工作,需要加减一个64位的号码和一个128位的数字。结果将在128位的数量来保持。我使用的是整数数组来存储128位数字(即 u_int64_t BIGNUM [2] ,其中 BIGNUM [0的上半部和下半部] 是最显著)。

I'm working in C and need to add and subtract a 64-bit number and a 128-bit number. The result will be held in the 128-bit number. I am using an integer array to store the upper and lower halves of the 128-bit number (i.e. u_int64_t bigNum[2], where bigNum[0] is the least significant).

任何人都可以用一个加减功能,帮助那些可以在BIGNUM和加/减一个 u_int64_t 呢?

Can anybody help with an addition and subtraction function that can take in bigNum and add/subtract a u_int64_t to it?

我看到网络上很多不正确的例子,所以考虑这个问题:

I have seen many incorrect examples on the web, so consider this:

bigNum[0] = 0;  
bigNum[1] = 1;  
subtract(&bigNum, 1);

此时 BIGNUM [0] 应该都位设置,而 BIGNUM [1] 应该没有位设置。

At this point bigNum[0] should have all bits set, while bigNum[1] should have no bits set.

推荐答案

在1级或2级,你应该有learn't如何打破加入1和10分成几部分,通过拆分为多个单独添加十位和个。当与大数字处理,相同的主体可以被应用于计算上任意大数算术运算,通过实现的单元是现在的2 ^比特为单位,你的几十是2 ^位较大等

In grade 1 or 2, you should have learn't how to break down the addition of 1 and 10 into parts, by splitting it into multiple separate additions of tens and units. When dealing with big numbers, the same principals can be applied to compute arithmetic operations on arbitrarily large numbers, by realizing your units are now units of 2^bits, your "tens" are 2^bits larger and so on.

这篇关于如何正确地加/减到一个128位的数目(作为两个u_int64_t)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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