473,797 Members | 3,199 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 UnsignedNumeric Type, std::size_t const i>
void SetAllElementsT oZero( UnsignedNumeric Type (&array)[i] )
{
memset( array, 0, i * sizeof(Unsigned NumericType) );

//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 2135
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 UnsignedNumeric Type, std::size_t const i>
No need for const. A template parameter is a compile-time constant anyway.
void SetAllElementsT oZero( UnsignedNumeric Type (&array)[i] )
{
memset( array, 0, i * sizeof(Unsigned NumericType) );

//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 UnsignedNumeric Type, std::size_t const i>
void SetAllElementsT oZero( UnsignedNumeric Type (&array)[i] )
{
memset( array, 0, i * sizeof(Unsigned NumericType) );

//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,size of(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

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

Similar topics

4
4740
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 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
1
2403
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 far (HMON working) . Now i had made a special local group a the member server (contains both local and domain admin accounts and some local accounts without local
25
15638
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 paragraph from the standard, I can't determine if this macro will always be zero. It would surely be convenient if it is but it never states this directly. the "zero or" part, leads me to believe that the macro
4
5858
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 - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
10
4592
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 zero-length string (always returns a zero-length string when used in a query expression)" **** How many records are there in FirstTable in which Product Is Null. SELECT COUNT(*) AS CountofNullProdcut
1
1294
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 formatted @@-@@@@. The results are 0-61234 in the combo box. Can you assist me? Thank you Charlie -- Message posted via http://www.accessmonster.com
33
8196
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 element array like this, but it seems like overkill: Dim emptyArray as Byte() = System.Text.Encoding.Default.GetBytes("") Is there a better way? Zytan
23
4378
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 non-two's-complement machine. Just wondering.) -- Hallvard
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10023
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.