union SignedChoice{
long with_sign;
unsigned long without_sign;
};
int main()
{
SignedChoice data;
data.with_sign = -1;
//Right now, does the C++ Standard guarantee that
//data.without_si gn == MAX_ULONGINT?
//Similarly:
data.without_si gn = MAX_ULONGINT;
//Is data.with_sign now definitely == -1?
}
Is there any other interesting facts yous can give me?
-JKop 38 8444
"JKop" <NU**@NULL.NULL > wrote in message
news:PF******** *********@news. indigo.ie... union SignedChoice{ long with_sign; unsigned long without_sign; };
int main() { SignedChoice data;
data.with_sign = -1;
//Right now, does the C++ Standard guarantee that //data.without_si gn == MAX_ULONGINT?
//Similarly:
data.without_si gn = MAX_ULONGINT;
//Is data.with_sign now definitely == -1?
}
I'm pretty sure that it doesn't. From what I understand, the standard does
not dictate the specific representation of a signed integer type (i.e,
whehter it's two's-complement, one's-complement, etc.). What is wel-defined
is the overflow/underflow behavior of an unsigned integer value, not its
direct equivelance to a signed integer value. So, adding x to a value to
casue an overflow can always be reversed by then subtracting x from that
result. Likewise, you can undo an underfolw by adding back the value
subtracted. But that doesn't neccessarily mean that s signed value of -1 is
the same bit representation as an unsigned value of max_whatever.
-Howard
Geez, how many typos can I make in one post??? I'll have to get my fingers
checked. :-)
-Howard
On Mon, 28 Jun 2004 16:27:27 GMT, JKop <NU**@NULL.NULL > wrote: union SignedChoice{ long with_sign; unsigned long without_sign; };
int main() { SignedChoice data;
data.with_sign = -1;
//Right now, does the C++ Standard guarantee that //data.without_si gn == MAX_ULONGINT?
No, you can't write to one member of a union and read from another, at
least not like that (you can with structs with identical initial
sequences), so the point is moot.
If you are asking whether the bit pattern of -1 is guaranteed to be
all 1s, then the answer is no. 1's complement CPUs will give (for a
16-bit int):
111111111111111 0
Sign magnitude CPUs will give:
100000000000000 1
However, most CPUs these days do use 2s complement for signed numbers.
Is there any other interesting facts yous can give me?
Depending on your interests: http://www.lingolex.com/ants.htm
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Imagine a class that is absolutely massive in memory, maybe even a MB. You
want to swap two of these classes, and so obviously you don't want a
temporary. What do yous think of the following? I ain't got a compiler at
the moment so it's untested, and may be syntactically incorrect in places.
template<class Class> void Swap(Class& x, Class& y)
{
if ( (&x == &y) ) return;
unsigned char* const &pArrayX = reintepret_cast <unsigned char* const
&>(&x);
unsigned char* const &pArrayY = reinterpret_cas t<unsigned char*
const &>(&y);
for ( unsigned char i = 0 ; i < sizeof(Class); ++i)
{
pArrayX[i] += pArrayY[i];
pArrayY[i] = pArrayX[i] - pArrayY[i];
pArrayX[i] -= pArrayY[i];
}
};
The only reason I define those references is for enhanced readability.
-JKop
"JKop" <NU**@NULL.NULL > wrote in message
news:Jk******** *********@news. indigo.ie... Imagine a class that is absolutely massive in memory, maybe even a MB. You want to swap two of these classes, and so obviously you don't want a temporary. What do yous think of the following? I ain't got a compiler at the moment so it's untested, and may be syntactically incorrect in places.
template<class Class> void Swap(Class& x, Class& y) { if ( (&x == &y) ) return;
unsigned char* const &pArrayX = reintepret_cast <unsigned char*
const &>(&x); unsigned char* const &pArrayY = reinterpret_cas t<unsigned char* const &>(&y);
for ( unsigned char i = 0 ; i < sizeof(Class); ++i) { pArrayX[i] += pArrayY[i];
pArrayY[i] = pArrayX[i] - pArrayY[i];
pArrayX[i] -= pArrayY[i]; } };
The only reason I define those references is for enhanced readability.
-JKop
Without looking at the code close enough to check the details, I see what
you're trying to do, and it won't always work. You can't always treat a
class as an array of unsigned char. If it contains non-POD types, I think
you get undefined behavior when writing to the array.
-Howard
JKop wrote: Imagine a class that is absolutely massive in memory, maybe even a MB. You want to swap two of these classes, and so obviously you don't want a temporary. What do yous think of the following? I ain't got a compiler at the moment so it's untested, and may be syntactically incorrect in places.
Generally, when classes, objects, instances or data is massive
in memory, you want to avoid swapping the beasts. Most programmers
use pointers to massive data and just swap the pointers.
If you don't want to use a temporary variable, then try swapping
individual members of the classes. One would say that less
memory is used swapping a class' member than the whole class.
This could even trickle down for aggregate and sub-classes.
Again, ask yourself why you are swapping the monstrosities
rather than pointers to them.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
Thomas Matthews posted: JKop wrote: Imagine a class that is absolutely massive in memory, maybe even a MB. You want to swap two of these classes, and so obviously you don't want a temporary. What do yous think of the following? I ain't got a compiler at the moment so it's untested, and may be syntactically incorrect in places.
Generally, when classes, objects, instances or data is massive in memory, you want to avoid swapping the beasts. Most programmers use pointers to massive data and just swap the pointers.
If you don't want to use a temporary variable, then try swapping individual members of the classes. One would say that less memory is used swapping a class' member than the whole class. This could even trickle down for aggregate and sub-classes.
Again, ask yourself why you are swapping the monstrosities rather than pointers to them.
Very good point, but I'm just doing this for fun. If I had to formulate a
bullshit reason for wanting to swap the values of variables I'd say
something like so:
A certain piece of hardware, Hardware A, is accessing a certain piece of
memory.
A certain piece of hardware, Hardware B, is accessing another certain piece
of memory.
You cannot change the address of the memory they're accessing so you've to
physically swap them around in memory.
-JKop
JKop wrote: union SignedChoice{ long with_sign; unsigned long without_sign; };
int main() { SignedChoice data;
data.with_sign = -1;
//Right now, does the C++ Standard guarantee that //data.without_si gn == MAX_ULONGINT?
No since long and unsigned long are different types and they can be of
different size. Also their representation implementation may be
different, for example for one type it may be from left to right and for
the other from right to left (my mind has stuck and I do not recall the
exact terminology right now). //Similarly:
data.without_si gn = MAX_ULONGINT;
//Is data.with_sign now definitely == -1?
The same with what I told above. }
Is there any other interesting facts yous can give me?
Have a slow thorough read of a good, up to date ISO C++ book like "The
C++ Programming Language" 3rd Edition or Special Edition by Bjarne
Stroustrup, the creator of C++.
However the last one is not for newcomers, and if you have not read any
other C++ introduction book *from cover to cover*, may be you had better
check "Accelerate d C++" by Andrew Koenig, Barbara Moo.
Regards,
Ioannis Vranos
JKop wrote: Imagine a class that is absolutely massive in memory, maybe even a MB.
Types do not occupy space, objects do. For example:
class A
{
int array[512];
};
The above definition does not occupy any space. However
A a;
does.
Objects occupying massive amounts of memory usually mean that there is
very bad design.
If you have container objects for example, the object itself is a
representation and when you swap you do not copy everything, but
exchange the internal structures (e.g. pointer variables pointing to
sequences on the free store).
In any case, stick with std::swap and provide specialisations of it for
your classes, in case you *can* perform some hand-written optimisations.
Otherwise the default one will be used (usually implemented to use a
temporary object).
Regards,
Ioannis Vranos This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Oplec |
last post by:
Hello,
How can underflow and overflow of the basic arithmetic types be tested
for using standard C++?
For instance, if two long doubles are multiplied together, test whether
or not the result is too large to fit in a long double?
Thank you for your time, Oplec.
|
by: aling |
last post by:
Given: signed a, b;
How to judge overflow of the sum of these two operands? Just use one
*if* statement.
Is this statement right?
if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0))
// overflow
else
// not overflow
|
by: Paul Emmons |
last post by:
Can anyone suggest example code, or how to proceed, in adding or
subtracting two long long integers (I'm working in gcc with Linux,
where a long long is 64 bits) and determining whether an overflow or
underflow occurs, invalidating the result?
I have written a solution in assembler. It's simple, thanks to the
overflow flag. But without access to this part of the hardware in the
C language, how would one do it?
Thank you for your...
|
by: Ian Pilcher |
last post by:
I'm trying to figure out if an increment to a variable of an integer
type, followed by a decrement, (or vice versa) is guaranteed to restore
the variable to its initial value, even if the first operation causes
the variable to overflow/underflow.
In other words, if foo_t is an integer type, are the following two
functions guaranteed to *always* return a non-zero value?
int check_overflow(foo_t f)
{
|
by: glenn |
last post by:
I have a COM Server I've written in C#. I have a client app I've written in
Delphi that is calling the C# COM Server. However, one of the functions in
the COM Server creates a form and during the creation process it receives
the Overflow or Underflow in arithmetic operation error. I have traced it
down to the section of the for where its registering the components...
this.Controls.Add(this.cmd_exit);
it doesn't matter which control...
| |
by: Tom |
last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's
workstation. It works fine on EVERY machine except for one - on this one
machine it generates a 'Overflow or underflow in the arithmetic operation'
when I attempt to instantiate my first form, as so:
Dim frm As New frmMyForm()
Based on some things I saw here, I thought it might be that this workstation
didn't have a MS Sans Seriff font on it (since that is the...
|
by: Raymond |
last post by:
Source:
http://moryton.blogspot.com/2007/08/detecting-overflowunderflow-when.html
Example from source:
char unsigned augend (255);
char unsigned const addend (255);
char unsigned const sum (augend + addend);
if (sum < augend)
|
by: jou00jou |
last post by:
Hi,
I have trouble using sscanf and fgets to check for overflow.
I will post the assignment specification so I could help whoever would kindly like to offer his/her help.
____________________________________________________________________________________
1) The program should expect 2 decimal integers per line. Let's call these n1 and n2.
2) For each such input line it should output the value of n1 + 2 * n2 (followed by a newline...
|
by: pereges |
last post by:
Hi, I have written a small function that can (I think) deal with
overflow/underflow while adding integers and divide by zero problem.
Can some one please tell me if I have done it correctly ? Would it be
safe to extend it to double precision floating point numbers since we
know that there is always some margin for error while adding floating
point numbers ? What other functions should I look to implement for my
project ?
#include...
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |