零总是== 0000 0000 [英] Zero always == 0000 0000

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

问题描述



对于所有未签名的

类型,以下是否完全合法且完全可移植?该函数的目的是通过引用获取数组并将每个元素的值设置为零。


#include< ...


模板< class UnsignedNumericType,std :: size_t const i>

void SetAllElementsToZero(UnsignedNumericType(& array)[i])

{

memset(数组,0,i * sizeof(UnsignedNumericType));


//或:


memset(数组,0,sizeof(数组));

}

我正在编写一个函数,当时正在操作一个

通过引用传递给它。它需要将一定数量的

元素设置为零。它只会被赋予无符号类型,例如:


unsigned

unsigned char

unsigned short

unsigned long ...

代码是否完全可移植且定义明确?在

标准中是否有保证所有上述类型的内存中的位模式

将全部为零,即。 0000 0000?

-Tomás


Is the following fully legal and fully portable for all the unsigned
types? The aim of the function is to take an array by reference and set
each element''s value to zero.

#include <...

template<class UnsignedNumericType, std::size_t const i>
void SetAllElementsToZero( UnsignedNumericType (&array)[i] )
{
memset( array, 0, i * sizeof(UnsignedNumericType) );

//or:

memset( array, 0, sizeof (array) );
}
I writing a function at the moment that''s manipulating an array which is
passed to it by reference. It needs to set a certain amount of the
elements to zero. It will only ever be given the unsigned types, e.g.:

unsigned
unsigned char
unsigned short
unsigned long...
Is the code fully portable and well defined? Is there a guarantee in the
Standard that the bit pattern in memory for all the aforementioned types
will be all zeros, ie. 0000 0000?
-Tomás

推荐答案

Tomás写道:
Tomás wrote:
<对于所有未签名的
类型,以下是完全合法且完全可移植的吗?该函数的目的是通过引用获取数组并将每个元素的值设置为零。

#include< ...

template< class UnsignedNumericType,std :: size_t const i>


不需要const。模板参数无论如何都是编译时常量。

void SetAllElementsToZero(UnsignedNumericType(& array)[i])
{memset(array,0,i * sizeof) (UnsignedNumericType));

//或:

memset(数组,0,sizeof(数组));
}

我正在编写一个函数,它正在操作一个通过引用传递给它的数组。它需要将一定数量的
元素设置为零。它只会被赋予无符号类型,例如:

unsigned
unsigned char
unsigned short
unsigned long ...

代码是否完全可移植且定义明确?在
标准中是否有保证所有上述类型的存储器中的位模式将全部为零,即。 0000 0000?

Is the following fully legal and fully portable for all the unsigned
types? The aim of the function is to take an array by reference and set
each element''s value to zero.

#include <...

template<class UnsignedNumericType, std::size_t const i>
No need for const. A template parameter is a compile-time constant anyway.
void SetAllElementsToZero( UnsignedNumericType (&array)[i] )
{
memset( array, 0, i * sizeof(UnsignedNumericType) );

//or:

memset( array, 0, sizeof (array) );
}
I writing a function at the moment that''s manipulating an array which is
passed to it by reference. It needs to set a certain amount of the
elements to zero. It will only ever be given the unsigned types, e.g.:

unsigned
unsigned char
unsigned short
unsigned long...
Is the code fully portable and well defined? Is there a guarantee in the
Standard that the bit pattern in memory for all the aforementioned types
will be all zeros, ie. 0000 0000?




是。



Yes.


" Rolf Magnus" < RA ****** @ t-online.de>在消息中写道

news:du ************* @ news.t-online.com ...
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:du*************@news.t-online.com...
代码是否完全可移植且定义良好?在
标准中是否有保证所有上述类型的存储器中的位模式将全部为零,即。 0000 0000?
Is the code fully portable and well defined? Is there a guarantee in the
Standard that the bit pattern in memory for all the aforementioned types
will be all zeros, ie. 0000 0000?


是。




真的吗?你能告诉我们保证在哪里吗?



Really? Can you tell us where that guarantee is?


Andrew Koenig写道:
Andrew Koenig wrote:
" Rolf Magnus" < RA ****** @ t-online.de>在消息中写道
新闻:du ************* @ news.t-online.com ...

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:du*************@news.t-online.com...

代码是否完全可移植且定义良好?在
标准中是否有保证所有上述类型的存储器中的位模式将全部为零,即。 0000 0000?
Is the code fully portable and well defined? Is there a guarantee in the
Standard that the bit pattern in memory for all the aforementioned types
will be all zeros, ie. 0000 0000?





是。



真的吗?你能告诉我们保证在哪里吗?


Really? Can you tell us where that guarantee is?




如果我们直接谈论0,不是只有三个陈述的事实

被接受(两个补码,一个补码,签名量级)服务

作为担保?有条件的幅度和一个补码有一种方法来表示-b
,但这不是OP所要求的。


V

-

请在邮寄回复时从我的地址中删除资金



If we talk of straight 0, doesn''t the fact that only three representations
are accepted (two''s complement, one''s complement, signed magnitude) serve
as the guarantee? Signed magnitude and one''s complement have a way to
represent -0, but that''s not what the OP asked.

V
--
Please remove capital As from my address when replying by mail


这篇关于零总是== 0000 0000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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