签名vs无签名 [英] signed vs unsigned

查看:87
本文介绍了签名vs无签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这本身并不是一个C ++问题...而是,我发布这个bcs我想要从C ++语言的角度来看答案。希望这是有道理的。


我正在阅读Peter van der Linden的专家C编程:深度C

秘密并且发现了以下声明:


通过最大限度地减少使用无符号类型来避免不必要的复杂性。

具体来说,不要使用无符号类型表示数量只是

因为它永远不会是负数(例如,年龄或national_debt)。


不可否认,我最小化我的代码中的注释。我在必要时使用它们

但是尝试将它们限制为一两行。我只是不喜欢他们在源头上的美学影响。我发现代码更难以阅读

散落着各种解释。


我发现代码在使用适当的换行符分解时更容易阅读

以及作为标题的小而简短的评论。


所以 - 这实际上意味着我尝试了最好的写作

自描述代码。更短的函数,

函数和变量名等的不言自明的名称。有时候,评论的范围是必要的,但是作为一个整体,我倾向于避免使用它。 />

我真的很享受彼得的书,但我发现这个评论很难

吞咽,考虑到如果一个年龄或数组索引永远不会是负面的

- 我想用适当的选择来说明 -

即unsigned int。


我在这里的少数人?我的偏好被认为是不好的风格吗?


我想,从编译器的角度来看......到处使用int是

更便携...对于unsigned int的比较可以在K& R和ANSI C之间变化



我不会因此而遭受某种类型的性能损失决定

我是吗?


提前致谢,


-Luther

This isn''t a C++ question per se ... but rather, I''m posting this bcs I
want the answer from a C++ language perspective. Hope that makes sense.

I was reading Peter van der Linden''s "Expert C Programming: Deep C
Secrets" and came across the following statement:

"Avoid unnecessary complexity by minimizing your use of unsigned types.
Specifically, don''t use an unsigned type to represent a quantity just
because it will never be negative (e.g., "age" or "national_debt")."

Admittedly, I minimize comments in my code. I use them when necessary
but try to limit them to one or two lines. I just don''t like their
aesthetic affect in the source. I find the code much harder to read
littered with paragraphs of explanations.

I find the code easier to read when broken up with appropriate newlines
and small, short comments acting as headings.

So - that effectually means that I try my darndest to write
self-describing code. Shorter functions, self-explanatory names for
functions and variable names, etc. Sometimes exceessive commenting is
necessary, but as a whole, I tend to avoid it.

I''m really enjoying Peter''s book, but I find this comment hard to
swallow considering that if an age or array index can never be negative
- I would want to illustrate that with an apporpriate choice of type -
namely, unsigned int.

Am I in the minority here? Is my predilection considered poor style?

I guess, from the compiler''s standpoint ... using int everywhere is
more portable ... since comparison''s against unsigned int can vary
between K&R and ANSI C.

I''m not incurring some type of performance penalty for such decisions
am I?

Thanks in advance,

-Luther

推荐答案

2006年2月21日08:58:23 -0800,LuB <卢********* @ yahoo.com>写道:
On 21 Feb 2006 08:58:23 -0800, "LuB" <lu*********@yahoo.com> wrote:
我真的很享受彼得的书,但我发现这个评论难以吞下,因为如果一个年龄或数组索引永远不会是负面的
- 我想用适当的选择来说明 -
即unsigned int。

我在这里是少数人吗?


希望不是!

我的偏好被认为是不好的风格吗?
I''m really enjoying Peter''s book, but I find this comment hard to
swallow considering that if an age or array index can never be negative
- I would want to illustrate that with an apporpriate choice of type -
namely, unsigned int.

Am I in the minority here?
Hopefully not!
Is my predilection considered poor style?




绝对不是!


处理已签名与未签名的关键不是混合它们,如果

可能,在比较中。特别警惕在减少计数器时将未签名的

计数器值与0进行比较。


-

Bob Hairgrove
No**********@Home.com


就像你说的那样,将一个类型标记为unsigned。如果没有别的,那么对于其他开发者而言,它将永远不会消极的文件。

我看不懂他的东西,但我无法想象他是一个复杂的问题。

讨论。


性能差异在很大程度上取决于您的架构。在AMD64上,使用无符号类型时,
除法/模数更快。使用签名类型转换为

浮点更快。加/减$

相同。

Like you said, marking a type as "unsigned" if for nothing else is an
easy documentation for other developers that it will never be negative.
I havn''t read his stuff but I can''t imagine what complexities he is
talking about.

Performance differences depend largly on your architecture. On AMD64,
division/modulus are faster when using unsigned types. Conversion to
floating point is faster with signed types. Addition/subtraction are
the same.




LuB写道:

LuB wrote:
我真的很享受彼得的书,但我觉得这个评论难以吞下,因为如果一个年龄或数组索引永远不会是负面的
- 我想说明一下有一个适当的类型选择 -
即unsigned int。
I''m really enjoying Peter''s book, but I find this comment hard to
swallow considering that if an age or array index can never be negative
- I would want to illustrate that with an apporpriate choice of type -
namely, unsigned int.




我能想到的唯一原因是你可能不想使用

''unsigned''如果假设它永远不会是负面的,那么可能会改变
。但是,大多数时候你最好假设和

保护无符号类型,然后如果你需要b / b
则更改它。最大限度地减少对类型的依赖性,以便更容易做到。


无符号的好处是,如果值不能为负,那么

你使用签名然后你总是要检查它。简单地将

类型定义为unsigned,除去所有内容以及记录您的

域名。



The only reason that I can think of that you might want to not use
''unsigned'' is if the assumption that it will never be negative might
change. However, most times you are better off with the assumption and
the protection of the unsigned type and then change it later if you
have to. Minimize dependencies on the type so that it is easier to do.

The great thing about unsigned is if the value can''t be negative and
you use signed then you always have to check it. Simply defining the
type as unsigned gets rid of all that as well as documenting your
domain.


这篇关于签名vs无签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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