[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

inc unsigned (whatever) beyond limit.

.rhavin grobert

10/21/2008 3:47:00 PM

when i do a.....
________________________________________________
unsigned int iTest; // lets asume 'int' = 32bit
iTest = 0xFFFFFFFF;
iTest++;
________________________________________________

does the std say something what shall happen now?
May i safely assume iTest is now 0 or is it CPU or sys-dependent?


TIA, ~.rhavin;)
13 Answers

Zeppe

10/21/2008 3:58:00 PM

0

..rhavin grobert wrote:
> when i do a.....
> ________________________________________________
> unsigned int iTest; // lets asume 'int' = 32bit
> iTest = 0xFFFFFFFF;
> iTest++;
> ________________________________________________
>
> does the std say something what shall happen now?
> May i safely assume iTest is now 0 or is it CPU or sys-dependent?
>
>
> TIA, ~.rhavin;)

the second one.

best, zeppe

.rhavin grobert

10/21/2008 4:00:00 PM

0

On 21 Okt., 17:57, Zeppe <ze...@remove.all.this.long.comment.yahoo.it>
wrote:
> .rhavin grobert wrote:
> > when i do a.....
> > ________________________________________________
> > unsigned int iTest;    // lets asume 'int' = 32bit
> > iTest = 0xFFFFFFFF;
> > iTest++;
> > ________________________________________________
>
> > does the std say something what shall happen now?
> > May i safely assume iTest is now 0 or is it CPU or sys-dependent?
>
> > TIA, ~.rhavin;)
>
> the second one.
>
> best, zeppe

CPU-dependent? damn, that's the worst case!

REH

10/21/2008 4:21:00 PM

0

On Oct 21, 11:57 am, Zeppe
<ze...@remove.all.this.long.comment.yahoo.it> wrote:
> .rhavin grobert wrote:
> > when i do a.....
> > ________________________________________________
> > unsigned int iTest;    // lets asume 'int' = 32bit
> > iTest = 0xFFFFFFFF;
> > iTest++;
> > ________________________________________________
>
> > does the std say something what shall happen now?
> > May i safely assume iTest is now 0 or is it CPU or sys-dependent?
>
> > TIA, ~.rhavin;)
>
> the second one.
>
> best, zeppe

That is incorrect. Assuming unsigned int is 32-bits, result will
always be 0.

REH

Maxim Yegorushkin

10/21/2008 4:28:00 PM

0

On Oct 21, 4:57 pm, Zeppe
<ze...@remove.all.this.long.comment.yahoo.it> wrote:
> .rhavin grobert wrote:
> > when i do a.....
> > ________________________________________________
> > unsigned int iTest;    // lets asume 'int' = 32bit
> > iTest = 0xFFFFFFFF;
> > iTest++;
> > ________________________________________________
>
> > does the std say something what shall happen now?
> > May i safely assume iTest is now 0 or is it CPU or sys-dependent?
>
> > TIA, ~.rhavin;)
>
> the second one.

Wrong answer.

The C and C++ standards guarantee the wrapping for unsigned integer
types.

Here is a relevant quote from C99 6.2.5:

A computation involving unsigned operands can never overflow, because
a result that cannot be represented by the resulting unsigned integer
type is reduced modulo the number that is one greater than the largest
value that can be represented by the resulting type.

--
Max

Victor Bazarov

10/21/2008 4:50:00 PM

0

Maxim Yegorushkin wrote:
> [..]
> The C and C++ standards guarantee the wrapping for unsigned integer
> types.
>
> Here is a relevant quote from C99 6.2.5:
> [..]

Why C99? C++ has its own Standard, does it not?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Obnoxious User

10/21/2008 4:59:00 PM

0

On Tue, 21 Oct 2008 09:27:47 -0700, Maxim Yegorushkin wrote:
>> .rhavin grobert wrote:
>> > when i do a.....
>> > ________________________________________________
>> > unsigned int iTest;    // lets asume 'int' = 32bit
>> > iTest = 0xFFFFFFFF;
>> > iTest++;
>> > ________________________________________________
>>
>> > does the std say something what shall happen now?
>> > May i safely assume iTest is now 0 or is it CPU or sys-dependent?
>>
>> > TIA, ~.rhavin;)
>>
>> the second one.
>
>Wrong answer.
>
>The C and C++ standards guarantee the wrapping for unsigned integer
>types.
>
>Here is a relevant quote from C99 6.2.5:
>
>A computation involving unsigned operands can never overflow, because
>a result that cannot be represented by the resulting unsigned integer
>type is reduced modulo the number that is one greater than the largest
>value that can be represented by the resulting type.

Sure C++ has a C legacy, but quoting the C standard
in a C++ context just looks wrong, even if the actual
text is almost the same.

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

Erik Wikström

10/21/2008 5:34:00 PM

0

On 2008-10-21 18:49, Victor Bazarov wrote:
> Maxim Yegorushkin wrote:
>> [..]
>> The C and C++ standards guarantee the wrapping for unsigned integer
>> types.
>>
>> Here is a relevant quote from C99 6.2.5:
>> [..]
>
> Why C99? C++ has its own Standard, does it not?

For your pleasure:

3.9.1/4:
"Unsigned integers, declared unsigned, shall obey the laws of arithmetic
modulo 2^n where n is the number of bits in the value representation of
that particular size of integer."

--
Erik Wikström

Zeppe

10/21/2008 5:44:00 PM

0

Maxim Yegorushkin wrote:
>>> ________________________________________________
>>> unsigned int iTest; // lets asume 'int' = 32bit
>>> iTest = 0xFFFFFFFF;
>>> iTest++;
>>> ________________________________________________
>>> does the std say something what shall happen now?
>>> May i safely assume iTest is now 0 or is it CPU or sys-dependent?
>>> TIA, ~.rhavin;)
>> the second one.
>
> Wrong answer.
>
> The C and C++ standards guarantee the wrapping for unsigned integer
> types.

My bad. Actually, I know that piece, I just ignored the comment as a
compiler would do. So, the result IS sys-dependent, BUT if you assume
that all the systems in which that code will be compiled are going to be
32bit, then is safe to assume that iTest is going to be zero after the
increment.

Sorry about that.

Best wishes,

Zeppe

blargg.h4g

10/21/2008 6:23:00 PM

0

In article <gdl4cv$28s$1@aioe.org>, Zeppe
<zep_p@remove.all.this.long.comment.yahoo.it> wrote:

> Maxim Yegorushkin wrote:
> >>> ________________________________________________
> >>> unsigned int iTest; // lets asume 'int' = 32bit
> >>> iTest = 0xFFFFFFFF;
> >>> iTest++;
> >>> ________________________________________________
> >>> does the std say something what shall happen now?
> >>> May i safely assume iTest is now 0 or is it CPU or sys-dependent?
> >>> TIA, ~.rhavin;)
> >> the second one.
> >
> > Wrong answer.
> >
> > The C and C++ standards guarantee the wrapping for unsigned integer
> > types.
>
> My bad. Actually, I know that piece, I just ignored the comment as a
> compiler would do. So, the result IS sys-dependent, BUT if you assume
> that all the systems in which that code will be compiled are going to be
> 32bit, then is safe to assume that iTest is going to be zero after the
> increment.

How about this:

unsigned i = UINT_MAX;
i++;
assert( i == 0 );

.rhavin grobert

10/21/2008 7:23:00 PM

0

On 21 Okt., 20:23, blargg....@gishpuppy.com (blargg) wrote:
> In article <gdl4cv$28...@aioe.org>, Zeppe
>
>
>
> <ze...@remove.all.this.long.comment.yahoo.it> wrote:
> > Maxim Yegorushkin wrote:
> > >>> ________________________________________________
> > >>> unsigned int iTest;    // lets asume 'int' = 32bit
> > >>> iTest = 0xFFFFFFFF;
> > >>> iTest++;
> > >>> ________________________________________________
> > >>> does the std say something what shall happen now?
> > >>> May i safely assume iTest is now 0 or is it CPU or sys-dependent?
> > >>> TIA, ~.rhavin;)
> > >> the second one.
>
> > > Wrong answer.
>
> > > The C and C++ standards guarantee the wrapping for unsigned integer
> > > types.
>
> > My bad. Actually, I know that piece, I just ignored the comment as a
> > compiler would do. So, the result IS sys-dependent, BUT if you assume
> > that all the systems in which that code will be compiled are going to be
> > 32bit, then is safe to assume that iTest is going to be zero after the
> > increment.
>
> How about this:
>
>     unsigned i = UINT_MAX;
>     i++;
>     assert( i == 0 );

Actually it's that one:
________________________________

typedef unsigned __int64 QUAD;

class foo {
foo(): m_qSel(0) {};
public:
inline QUAD NextSelector() {return m_qSel++;};
private:
QUAD m_qSel;
};