效率关注:什么时候真正使用无符号整数和何时不使用 [英] efficiency concern: when to really use unsigned ints and when not to

查看:68
本文介绍了效率关注:什么时候真正使用无符号整数和何时不使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我常常碰巧处理非负整数,因为我知道我不会在这里需要负数我知道它们作为无符号只是为了使程序

更清晰一点。但实际上,有条件的整数通常也会起作用,因为除非我按照单词长度进行模拟算术模拟,否则我几乎不需要使用高位,因为我处理的整数通常不是那么大,我认为大多数程序都是如此。


所以问题是,当你知道一个整数不会是负数,那就是

足够的理由将它声明为无符号,或者这样做有点慢下来计算机(例如是签名加法,减法等...有点快和

为什么会如此?)?


谢谢,


Neil

Hello,

Often I happen to be dealing with nonnegative integers and since I know I won''t
need negative numbers here I declare them as unsigned simply to make the program
somewhat clearer. Effectively, though, signed integers would often work too since
unless I am doing some modular arithmetic modulo the word length then I almost
never need to use the high bit since the integers I deal with are usually not
that large, and I would assume this is true of most programs in general.

So, the question is, when you know an integer is not going to be negative, is that
good enough reason to declare it as unsigned, or does doing so somewhat slow down
the computer (e.g. are signed addition, subtraction, etc... somewhat faster and
why would that be so?)?

Thanks,

Neil

推荐答案

Neil Zanella写道:
Neil Zanella wrote:
你好,

我常常碰巧处理非负整数,因为我知道我不需要负数,所以我宣称它们是无符号的,只是为了使它成为
程序有点清晰。


我也一样。

但是,有效地,有符号整数通常也会起作用,因为除非我做模拟算术模拟

好​​吧,如果你的模数恰好是N到2,其中N是无符号整数类型中

值位的数量,你''好运。 :-)


但我认为你在谈论,比如说,一周中的一天(模7),

年(模数12) ,或者可能是(整数)角度(以360为单位)。那个

/ sort / of thing。

所以,问题是,当你知道一个整数不会是负数时,
就足够了声明它是未签名的,


恕我直言,是的。

或这样做有时会使计算机变慢(例如签名加法,减法) ,等等......有点快,为什么会如此?)?
Hello,

Often I happen to be dealing with nonnegative integers and since I know I
won''t need negative numbers here I declare them as unsigned simply to make
the program somewhat clearer.
So do I.
Effectively, though, signed integers would
often work too since unless I am doing some modular arithmetic modulo the
word length then I almost never need to use the high bit since the
integers I deal with are usually not that large, and I would assume this
is true of most programs in general.
Well, if your modulus happens to be 2 to the N, where N is the number of
value bits in the unsigned integer type, you''re in luck. :-)

But I presume you''re talking about, say, day of week (modulo 7), month of
year (modulo 12), or perhaps (integral) angle in degrees (modulo 360). That
/sort/ of thing.
So, the question is, when you know an integer is not going to be negative,
is that good enough reason to declare it as unsigned,
IMHO, yes.
or does doing so
somewhat slow down the computer (e.g. are signed addition, subtraction,
etc... somewhat faster and why would that be so?)?




标准不要求无符号整数类型比签名

整数类型。


如果你确定你永远不会溢出该类型的最大值,感觉

如果您愿意,可以免费使用。我更喜欢使用unsigneds。


-

Richard Heathfield: bi **** @ eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton



The Standard doesn''t require unsigned integer types to go slower than signed
integer types.

If you are sure you''ll never overflow the maximum value for the type, feel
free to use signed if you wish. I prefer to use unsigneds.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


nz******@cs.mun.ca (Neil Zanella)在

新闻中写道:b6 ************************** @ posting.google.c om :
nz******@cs.mun.ca (Neil Zanella) wrote in
news:b6**************************@posting.google.c om:
我经常碰巧处理非负整数,因为我知道
我不需要负数我在这里声明它们是无符号的仅仅是为了
使程序更清晰。但实际上,签名整数通常也会起作用,因为除非我做一些模块化算术模数字长度然后我几乎不需要使用高位
因为我处理的整数通常不是那么大,我会认为大多数程序都是如此。

所以,问题是,当你知道一个整数不会是
否定,是否有足够的理由将其声明为无符号,或者这样做会使计算机速度变慢(例如,签名添加,减法等等......有点快,为什么会这样?所以?)?
Often I happen to be dealing with nonnegative integers and since I know
I won''t need negative numbers here I declare them as unsigned simply to
make the program somewhat clearer. Effectively, though, signed integers
would often work too since unless I am doing some modular arithmetic
modulo the word length then I almost never need to use the high bit
since the integers I deal with are usually not that large, and I would
assume this is true of most programs in general.

So, the question is, when you know an integer is not going to be
negative, is that good enough reason to declare it as unsigned, or does
doing so somewhat slow down the computer (e.g. are signed addition,
subtraction, etc... somewhat faster and why would that be so?)?



在没有签名支持的小型微处理器上签名可能比较慢,例如

古老的8051.但是,除非你需要指令级决定论

我不担心。制造变种。当值不是

为负时未签名。例如,为什么你需要将一个人的年龄表示为一个有价值的
年?既然我们知道比将控制与

数据混合更好,我们就不会使用负值来表示错误,所以我当然希望

看到年龄宣称为:


unsigned int ageInYears;





unsigned char ageInYears;


-

- 马克 - >

-



On small micros without sign-support hardware signed maybe slower, e.g.
the venerable 8051. However, unless you need instruction level determinism
I wouldn''t worry about it. Make the vars. unsigned when the value is not
negative. For example, why would you ever need to express someone''s age in
years as a signed value? Since we know better than to mix control with
data we wouldn''t use a negative value for an error so I''d certainly expect
to see age declared as:

unsigned int ageInYears;

or

unsigned char ageInYears;

--
- Mark ->
--


文章< b6 ************************** @ posting.google.com>,
nz ****** @ cs.mun.ca (Neil Zanella)写道:
In article <b6**************************@posting.google.com >,
nz******@cs.mun.ca (Neil Zanella) wrote:
你好,

我常常碰巧处理非负整数,因为我知道我不会在这里需要负数我声明它们是无符号的只是为了使有点清楚。但实际上,签名整数通常也会起作用,因为
除非我正在做一些模数运算模数字长度然后我几乎不需要使用高位,因为我处理的整数通常不是那么大,我认为大多数程序都是如此。

所以,问题是,当你知道一个整数不会是否定的,是否足够的理由将其声明为无符号,或者这样做有点慢......
关闭计算机(例如签名加法,减法等等)。有点快吗

为什么会如此呢?)?
Hello,

Often I happen to be dealing with nonnegative integers and since I know I
won''t
need negative numbers here I declare them as unsigned simply to make the
program
somewhat clearer. Effectively, though, signed integers would often work too
since
unless I am doing some modular arithmetic modulo the word length then I
almost
never need to use the high bit since the integers I deal with are usually not
that large, and I would assume this is true of most programs in general.

So, the question is, when you know an integer is not going to be negative, is
that
good enough reason to declare it as unsigned, or does doing so somewhat slow
down
the computer (e.g. are signed addition, subtraction, etc... somewhat faster
and
why would that be so?)?




对于有符号整数,C运算符+, - ,*生成相同的

作为数学运算符的结果(只要结果不是
太大)。对于无符号整数,C运算符会做一些非常奇怪的事情。一个简单的例子:对于哪些数字是


x> = y - 1


未签名?对于签名号码,你很安全。两个都是未签名的,和

有一个奇怪的特殊情况,y = 0.一个签名,另一个

未签名,你必须学习C标准。


看来你所拥有的是积极的数字。 "无符号"是什么东西

完全不同;无符号数字可以完全表现出意外的方式。在非常常见的情况下,x - 1不比x小1。


(如果他们切换到

,有些人会遇到一些令人讨厌的惊喜unsigned int和unsigned long具有不同大小的编译器。)



For signed integers, the C operators +, -, * produce exactly the same
results as the mathematical operators (as long as the results are not
too large). For unsigned integers, the C operators do some pretty weird
things. A trivial example: For which numbers is

x >= y - 1

true if x and y are both signed, both unsigned, one signed and the other
unsigned? For signed numbers, you are quite safe. Both unsigned, and
there is a strange special case for y = 0. One signed and the other
unsigned, and you have to study the C Standard.

It seems what you have are "positive" numbers. "unsigned" is something
completely different; unsigned numbers can behave in completely
unexpected ways. x - 1 is not one less than x in very common cases.

(And some people will be in for some nasty surprises if they switch to a
compiler where unsigned int and unsigned long have different sizes. )


这篇关于效率关注:什么时候真正使用无符号整数和何时不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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