无需携带 [英] add without carry
问题描述
我想在没有携带两个整数的情况下执行添加...
我不知道如何在python中执行此操作,尽管我一直在使用它
for web / cgi / db工作几年了。
任何帮助都会很棒。
休
在< 11 ********************* *@i42g2000cwa.googlegroups .com> ;, Hugh写道:
我想在没有携带两个整数的情况下执行添加...
我不知道如何在python中做到这一点,虽然我已经使用它了几年来用于web / cgi / db工作的
。
您的描述有点模糊。什么是没有携带?你想要
将结果限制为给定的比特长度,并使用环绕。行为如果
这个数字太大而不适合这些位?然后这应该做
把戏:
a =(b + c)& (2 **位 - 1)
Ciao,
Marc''BlackJack''Rintsch
< blockquote>
休写道:
我想在没有携带两个整数的情况下执行添加...
我不知道如何在python中做到这一点,虽然我已经使用它了
for web / cgi / db工作几年了。
在汇编语言的多字添加中,对最低位字对和ADC(或ADDC或
指令/>
无论如何)即添加* with * carry,它为每个后续词对的
前一个操作添加进位。 IOW,另外
*没有*携带是正常的,我不记得之前听到过表达
......
>
你可能意味着:添加两个32位整数,这样结果适合于
32位,即丢弃生成的进位,保持低位
32位?
如果是这样,那就是(a + b)& 0xFFFFFFFF(在Python中以及一些
其他语言)。
如果你的位数不同,那么8位的答案就是
将是(a + b)& 0xFF。
否则,你需要提供两个整数的例子以及
添加而不携带生产。
HTH,
John
对不起,这里是'一个例子......
5 + 7 = 12
无需携带,5 + 7 = 2
>
即结果总是少于10
我一直在想更多关于这一点,我的大脑开始了
工作出来的东西...我只是想知道在python中是否有一个函数
数学自动执行此操作...
Hugh
I would like to perform an addition without carrying of two integers...
I''ve got no idea how to do this in python, although I''ve been using it
for web/cgi/db work for a few years now.
Any help would be great.
Hugh
In <11**********************@i42g2000cwa.googlegroups .com>, Hugh wrote:
I would like to perform an addition without carrying of two integers...
I''ve got no idea how to do this in python, although I''ve been using it
for web/cgi/db work for a few years now.Your description is a bit vague. What is `without carrying`? Do you want
to limit the result to a given bit length with a "wrap around" behavior if
the number gets too large to fit into those bits? Then this should do the
trick:
a = (b + c) & (2**bits - 1)
Ciao,
Marc ''BlackJack'' Rintsch
Hugh wrote:I would like to perform an addition without carrying of two integers...
I''ve got no idea how to do this in python, although I''ve been using it
for web/cgi/db work for a few years now.
In multiword addition in assembly language, one uses a normal ADD
instruction on the lowest-order pair of words, and an ADC (or ADDC or
whatever) i.e add *with* carry which adds in the carry bit from the
previous operation for each subsequent pair of words. IOW, addition
*without* carrying is normal, and I don''t recall hearing the expression
before ......
Do you possibly mean: add two 32-bit integers such the result fits in
32 bits i.e. discard the generated carry bit, keep the low-order
32-bits?
If so, that''s simply (a + b) & 0xFFFFFFFF (in Python as well as a few
other languages).
If you meant a different number of bits, well the answer for 8 bits
would be (a + b) & 0xFF.
Otherwise, you need to provide some examples of two integers and what
"addition without carrying" produces.
HTH,
John
Sorry, here''s an example...
5+7=12
added without carrying, 5+7=2
i.e the result is always less than 10
I''ve been thinking some more about this and my brain is starting to
work something out... I just wondered if there was a function in python
math to do this automatically...
Hugh
这篇关于无需携带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!