使用有符号整数类型 [英] Use of signed integer types

查看:91
本文介绍了使用有符号整数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想讨论使用带符号的整数类型,其中无符号整数

类型就足够了。


一个常见的例子是:


#include< cassert>

#include< cstddef>


int CountOccurrences(unsigned char const val,

unsigned char const p [],

std :: size_t const len)

{

断言(len> = 2);


unsigned char const * const p_over = p + len;

int i = 0 ; / *使用签名的整数类型* /


do

{

if(val == * p ++)++ i ;

}

while(p!= p_over);


返回i;

}

首先,我倾向于尽可能回避签名的整数类型

主要是因为溢出会导致未定义的行为。


另一个方面我想讨论的是效率。如果机器使用

两个补码,那么添加正数
数和负数应该没有区别。


但是,对于使用除了两个补码以外的东西的机器,是否会增加有符号整数的开销?例如:


int i = 5;

int j = -4;


if( j> = 0)

{

/ *使用特定技术添加* /

}

else

{

/ *使用特定技术添加* /

}

添加无符号整数时,会没有必要。


我认为在机器上使用有符号整数类型的

可能存在效率问题。我会使用两个'的补码,或者

我会离开标记吗?

-


Frederick Gotham


I''d like to discuss the use of signed integers types where unsigned integers
types would suffice.

A common example would be:

#include <cassert>
#include <cstddef>

int CountOccurrences(unsigned char const val,
unsigned char const p[],
std::size_t const len)
{
assert(len >= 2);

unsigned char const * const p_over = p + len;
int i = 0; /* USE OF SIGNED INTEGER TYPE */

do
{
if(val == *p++) ++i;
}
while(p != p_over);

return i;
}
Firstly, I tend to shy away from signed integer types wherever possible
primarily because overflow results in undefined behaviour.

Another aspect I''d like to discuss though is efficiency. If a machine uses
two''s complement, then there should be no difference in adding positive
numbers and negative numbers.

However, for machines which use something other than two''s complement, would
there be overhead in adding signed integers? For instance something like:

int i = 5;
int j = -4;

if( j >= 0 )
{
/* Add using a particular technique */
}
else
{
/* Add using a particular technique */
}
When adding unsigned integers, there would be no need for that.

Would I be right in thinking that there could be an efficieny issue with
using signed integer types on machines which don''t use two''s complement, or
would I be way off the mark?
--

Frederick Gotham

推荐答案



" Frederick Gotham" < fg ******* @ SPAM.comskrev i meddelandet

news:za ******************* @ news.indigo .ie ...

"Frederick Gotham" <fg*******@SPAM.comskrev i meddelandet
news:za*******************@news.indigo.ie...

>

我想讨论使用未签名的有符号整数类型

整数

类型就足够了。


一个常见的例子是:


#include< ; cassert>

#include< cstddef>

int CountOccurrences(unsigned char const val,

unsigned char const p [ ],

std :: size_t const len)

{

断言(len> = 2);

unsigned char const * const p_over = p + len;


int i = 0; / *使用签名的整数类型* /


do

{

if(val == * p ++)++ i ;

}

while(p!= p_over);


返回i;

}


首先,我倾向于回避有符号的整数类型,因为溢出会导致未定义的行为,因为这可能是
可能


>
I''d like to discuss the use of signed integers types where unsigned
integers
types would suffice.

A common example would be:

#include <cassert>
#include <cstddef>

int CountOccurrences(unsigned char const val,
unsigned char const p[],
std::size_t const len)
{
assert(len >= 2);

unsigned char const * const p_over = p + len;
int i = 0; /* USE OF SIGNED INTEGER TYPE */

do
{
if(val == *p++) ++i;
}
while(p != p_over);

return i;
}
Firstly, I tend to shy away from signed integer types wherever
possible
primarily because overflow results in undefined behaviour.



另一方面,定义了无符号溢出,但通常非常好。实际上,
无用。锄头你用的是定义明确的吗?结果?

On the other hand, unsigned overflow is defined but usually pretty
useless in practice. Hoe do you use the "well defined" result?


>

我想讨论的另一个方面是效率。如果

机器使用

两个补码,那么添加

肯定没有区别

数字和负数。


但是,对于使用两个以外的东西的b / b
补充的机器,会是

添加有符号整数会有开销吗?
>
Another aspect I''d like to discuss though is efficiency. If a
machine uses
two''s complement, then there should be no difference in adding
positive
numbers and negative numbers.

However, for machines which use something other than two''s
complement, would
there be overhead in adding signed integers?



不,那就是硬件。相信芯片设计师和编译器

实现者来解决这个问题。

No, that''s hardware anyway. Trust the chip designer and compiler
implementor to fix that.


>

我是否正确认为可能存在效率问题



在机器上使用有符号整数类型,这些机器不使用两个'

补充,或者

我会离开标记吗?
>
Would I be right in thinking that there could be an efficieny issue
with
using signed integer types on machines which don''t use two''s
complement, or
would I be way off the mark?



离开! :-)

混合有符号和无符号值时会出现真正的问题。选择

其中一个,并坚持下去。

Bo Persson

Way off! :-)
The real problem comes when you mix signed and unsigned values. Pick
one of them, and stay with it.
Bo Persson


文章< za ** *****************@news.indigo.ie>, fg *** **** @ SPAM.com

说...
In article <za*******************@news.indigo.ie>, fg*******@SPAM.com
says...

>

我想讨论使用有符号整数类型,其中无符号整数

类型就足够了。
>
I''d like to discuss the use of signed integers types where unsigned integers
types would suffice.



[...]

[ ... ]


但是,对于使用两个以外的东西的机器补充,是否会增加有符号整数的开销?
However, for machines which use something other than two''s complement, would
there be overhead in adding signed integers?



一般来说,没有 - 开销(当有任何时候)通常比较时会发生
。例如,在补码中,你可以用两种不同的方式表示零,所以比较相等

需要的不仅仅是检查所有位是

完全相同。


对此的惩罚因分钟而异。如果

硬件自动处理它,通常不会受到惩罚。如果你需要在软件中处理它,你通常会在适当的时候将

数加0。这通常是一个单一周期

在相对罕见的场合浪费。


[...]

Generally speaking, no -- the overhead (when there is any) generally
happens when you compare them. For example, in ones complement, you
can represent zero in two different ways, so comparing for equality
requires a little more than just checking for all bits being
identical.

The penalty for this varies from minute to nonexistent. If the
hardware handles it automatically, there''ll usually be no penalty. If
you have to handle it in software, you usually do it by adding 0 to a
number when appropriate. This is typically going to be a single cycle
wasted on relatively rare occasion.

[ ... ]


我认为在没有使用两个补码的机器上使用有符号整数类型可能存在效率问题

,或者

我会离开标记吗?
Would I be right in thinking that there could be an efficieny issue with
using signed integer types on machines which don''t use two''s complement, or
would I be way off the mark?



大部分都没有了。充其量只是在真正微观/ b $ b优化的范围内。通常它完全没有意义。至少IMO,

使用签名或未签名的原因是因为它反映了你的意图

更好,不是因为在某些罕见的情况下,可能会是

在显微镜下比另一个快。


-

后来,

杰里。


宇宙是自己想象的虚构。

Mostly off the mark. At best it''s in the realm of truly micro-
optimizations. Usually it''s completely meaningless. At least IMO, the
reason to use signed or unsigned is because it reflects your intent
better, not because one might, on some rare occasion, be
microscopically faster than the other.

--
Later,
Jerry.

The universe is a figment of its own imagination.


Jerry Coffin发布:

Jerry Coffin posted:


大部分都没有了。充其量只是在真正微观/ b $ b优化的范围内。通常它完全没有意义。至少IMO,

使用签名或未签名的原因是因为它反映了你的意图

更好,不是因为在某些罕见的情况下,可能会是

在显微镜下比另一个快。
Mostly off the mark. At best it''s in the realm of truly micro-
optimizations. Usually it''s completely meaningless. At least IMO, the
reason to use signed or unsigned is because it reflects your intent
better, not because one might, on some rare occasion, be
microscopically faster than the other.



是的,但是有很多专业程序员使用int为了各种目的,他们也一直使用b ++,并且他们也倾向于使用i ++。一直以来。


这是一个我可能写的循环的例子:


for(unsigned i = 0; i!= some_value; ++ i)


而不是:


for(int i = 0; i< some_value; i ++)

-


Frederick Gotham


Yes but there are MANY professional programmers who use "int" consistently
for all purpose, and they also tend to use "i++" consistently aswell.

Here''s an example of a loop I might write:

for(unsigned i = 0; i != some_value; ++i)

instead of:

for(int i = 0; i < some_value; i++)
--

Frederick Gotham


这篇关于使用有符号整数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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