473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Zero always == 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>
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
Mar 5 '06 #1
15 2092
Tomás wrote:

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.

Mar 6 '06 #2
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:du*************@news.t-online.com...
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.


Really? Can you tell us where that guarantee is?
Mar 6 '06 #3
Andrew Koenig wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:du*************@news.t-online.com...

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.

Really? Can you tell us where that guarantee is?


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
Mar 6 '06 #4
Victor Bazarov wrote:
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.

Really? Can you tell us where that guarantee is?


Ok, maybe I forgot padding bits.
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.


He didn't ask about signed types either, but rather only about unsigned
ones.
Mar 6 '06 #5
Rolf Magnus wrote:
Victor Bazarov wrote:

>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.
Really? Can you tell us where that guarantee is?

Ok, maybe I forgot padding bits.


Padding bits? Do they exist outside the context of _bit fields_?

V
--
Please remove capital As from my address when replying by mail
Mar 6 '06 #6
Victor Bazarov wrote:
Rolf Magnus wrote:
Victor Bazarov wrote:

> > > 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.
>
>
> Really? Can you tell us where that guarantee is?

Ok, maybe I forgot padding bits.


Padding bits? Do they exist outside the context of _bit fields_?


"Padding bits" would mean bits in an object that are used in the
representation of values. I believe C added something to the standard
that prohibits that sort of thing. Up until then, there was talk about
whether there could be unused bits that could form a trap
representation. Then 0 would not necessarily be all-bits-zero. This was
always pretty academic, as no one knew of any such implementation.

This is all my recollection, so any parts of it may be incorrect.

Brian
Mar 6 '06 #7
Victor Bazarov wrote:
Rolf Magnus wrote:
Victor Bazarov wrote:

>>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.
Really? Can you tell us where that guarantee is?

Ok, maybe I forgot padding bits.


Padding bits? Do they exist outside the context of _bit fields_?


AFAIK, in integer types other than char and signed/unsigned char, not all
bits need to participate in its value representation. So in theory, there
could be a machine where an unsigned int with all bits (including the
padding ones) 0 could be a trap representation. That's the only thing I can
think of that Andrew could have been after in his answer to my posting. If
there is any other issue, maybe he could elaborate, because clearly, an
integer with all value-bits 0 does have a zero value.

Mar 6 '06 #8
> 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.


Well, I can't find any place in the standard that prohibits sign-magnitude
notation in which 0 represents negative and 1 represents positive. In such
a notation, all bits 0 means -0, which is presumably distinguishable from 0.
Mar 7 '06 #9
Tomás posted:

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

Anywho, what got me thinking of this is how you can't use this method
with pointers. For instance, take :

1) int* p = 0;

2) int* p;
memset(p,0,sizeof(int*) );
The 1st method sets the pointer to a "null pointer", which may or may not
represent "all bits zero" in memory.

The 2nd methos sets the pointer to "all bits zero" in memory, which may
or may not represent "all bits zero" in memory.

Actually come to think of it, if we've no guarantee that an unsigned
numeric type stores its zero value as "all bits zero", then we've not
guarantee that when we pass a zero literal (ie. 0) to memset, that it
will make the memory all bits zero...
-Tomás
Mar 7 '06 #10
The 2nd methos sets the pointer to "all bits zero" in memory, which may
or may not represent "all bits zero" in memory.


Typo:

The 2nd method sets the pointer to "all bits zero" in memory, which may
or may not represent a "null pointer".
Mar 7 '06 #11
Andrew Koenig wrote:
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.


Well, I can't find any place in the standard that prohibits sign-magnitude
notation in which 0 represents negative and 1 represents positive. In
such a notation, all bits 0 means -0, which is presumably distinguishable
from 0.


However, this is of no relevance, since the OP didn't ask about signed, but
rather only about unsigned types.

Mar 7 '06 #12

"Tomás" <NU**@NULL.NULL> skrev i meddelandet
news:y2******************@news.indigo.ie...

2) int* p;
memset(p,0,sizeof(int*) );

Actually come to think of it, if we've no guarantee that an unsigned
numeric type stores its zero value as "all bits zero", then we've
not
guarantee that when we pass a zero literal (ie. 0) to memset, that
it
will make the memory all bits zero...


First, the 0 isn't unsigned, but a signed int. :-)

Also, it is converted to an unsigned char before it is stored. That
presumably takes care of any pad bits, as an unsigned char cannot have
any.
Bo Persson
Mar 7 '06 #13
Andrew Koenig wrote:
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.


Well, I can't find any place in the standard that prohibits sign-magnitude
notation in which 0 represents negative and 1 represents positive.


How about:

"The range of nonnegative values of a signed integer type is a subrange of
the corresponding unsigned integer type, and the value representation of
each corresponding signed/unsigned type shall be the same."

I don't see why this shouldn't include the zero value. So an unsigned 0 must
have the same value representation as a signed 0.

Mar 8 '06 #14
are there implementations where null pointers aren't zeros?
which?

Mar 8 '06 #15
Diego Martins posted:
are there implementations where null pointers aren't zeros?
which?


I don't know any off hand. What I do know is:

A) The Standard permits that a pointer not be all bits zero.

And from that, I'd speculate that they allowed this because there is in
fact a system where pointers aren't all bits zeros.

-Tomás
Mar 8 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
1
by: zahni31 | last post by:
I had trouble to get the hmon work in 8.x FP 8 and FP 9 . The server runnig Windows 2003 SP1 on a member server. The db2 services are running under an domain admin account. Ok, no problem so...
25
by: Mantorok Redgormor | last post by:
Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. beyond this...
4
by: Christopher Benson-Manica | last post by:
How does one determine whether a double is equal to -0.0000...? Will fabs() from math.h return 0.000... on -0.0000...? -- Christopher Benson-Manica | I *should* know what I'm talking about -...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
1
by: hireagenius | last post by:
Hi heros, I have a text field that stores numbers like 06-1234. I created a combo box to look up values on a form based on the number. However, I can't get it formatted correctly. It is...
33
by: Zytan | last post by:
I want to make a zero element array. I know that Nothing is not the same as a zero element array, since I can't get the length of, or iterate through, an array = Nothing. I could make a zero...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.