Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   unsigned short addition/subtraction overflow (http://www.velocityreviews.com/forums/t316658-unsigned-short-addition-subtraction-overflow.html)

Andy 12-21-2003 06:31 AM

unsigned short addition/subtraction overflow
 
Hi,
Are 1 through 4 defined behaviors in C?

unsigned short i;
unsigned long li; /* 32-bit wide */

1. i = 65535 + 3;
2. i = 1 - 3;
3. li = (unsigned long)0xFFFFFFFF + 3;
4. li = 1 - 3;

TIA
Andy

James Hu 12-21-2003 09:15 AM

Re: unsigned short addition/subtraction overflow
 
On 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> Are 1 through 4 defined behaviors in C?
>
> unsigned short i;
> unsigned long li; /* 32-bit wide */
>
> 1. i = 65535 + 3;
> 2. i = 1 - 3;
> 3. li = (unsigned long)0xFFFFFFFF + 3;
> 4. li = 1 - 3;


Yes.

-- James

pete 12-21-2003 11:14 AM

Re: unsigned short addition/subtraction overflow
 
James Hu wrote:
>
> On 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> > Are 1 through 4 defined behaviors in C?
> >
> > unsigned short i;
> > unsigned long li; /* 32-bit wide */
> >
> > 1. i = 65535 + 3;
> > 2. i = 1 - 3;
> > 3. li = (unsigned long)0xFFFFFFFF + 3;
> > 4. li = 1 - 3;

>
> Yes.


No.
65536 is an allowable value for INT_MAX.
(65535 + 3) would be integer overflow
and undefined behavior in that case.

--
pete

Robert Stankowic 12-21-2003 01:06 PM

Re: unsigned short addition/subtraction overflow
 

"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
news:3FE5809B.2575@mindspring.com...
> James Hu wrote:
> >
> > On 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> > > Are 1 through 4 defined behaviors in C?
> > >
> > > unsigned short i;
> > > unsigned long li; /* 32-bit wide */
> > >
> > > 1. i = 65535 + 3;
> > > 2. i = 1 - 3;
> > > 3. li = (unsigned long)0xFFFFFFFF + 3;
> > > 4. li = 1 - 3;

> >
> > Yes.

>
> No.
> 65536 is an allowable value for INT_MAX.
> (65535 + 3) would be integer overflow
> and undefined behavior in that case.


From N869: 6.2.5
9 The range of nonnegative values of a signed integer type is a subrange of
the
corresponding unsigned integer type, and the representation of the same
value in each
type is the same.28) 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.

However, I am not sure about
i = 1 - 3;
and
li = 1 - 3;
but I think it is defined as far as I understand the integer promotion
rules.

cheers
Robert



CBFalconer 12-21-2003 01:46 PM

Re: unsigned short addition/subtraction overflow
 
pete wrote:
> James Hu wrote:
> > On 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> >
> > > Are 1 through 4 defined behaviors in C?
> > >
> > > unsigned short i;
> > > unsigned long li; /* 32-bit wide */
> > >
> > > 1. i = 65535 + 3;
> > > 2. i = 1 - 3;
> > > 3. li = (unsigned long)0xFFFFFFFF + 3;
> > > 4. li = 1 - 3;

> >
> > Yes.

>
> No.
> 65536 is an allowable value for INT_MAX.
> (65535 + 3) would be integer overflow
> and undefined behavior in that case.


Good catch. However, change "would" to "could".

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!



pete 12-21-2003 02:20 PM

Re: unsigned short addition/subtraction overflow
 
Robert Stankowic wrote:
>
> "pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
> news:3FE5809B.2575@mindspring.com...
> > James Hu wrote:
> > >
> > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> > > > Are 1 through 4 defined behaviors in C?
> > > >
> > > > unsigned short i;
> > > > unsigned long li; /* 32-bit wide */
> > > >
> > > > 1. i = 65535 + 3;
> > > > 2. i = 1 - 3;
> > > > 3. li = (unsigned long)0xFFFFFFFF + 3;
> > > > 4. li = 1 - 3;
> > >
> > > Yes.

> >
> > No.
> > 65536 is an allowable value for INT_MAX.
> > (65535 + 3) would be integer overflow
> > and undefined behavior in that case.

>
> From N869: 6.2.5
> 9 The range of nonnegative values of a
> signed integer type is a subrange of
> the corresponding unsigned integer type,
> and the representation of the same value in each
> type is the same.28)
> 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.


What's your point ?
If INT_MAX equals 65535,
then there are no unsigned operands in (65535 + 3), just two ints.

> However, I am not sure about
> i = 1 - 3;
> and
> li = 1 - 3;
> but I think it is defined as far as I
> understand the integer promotion rules.


The standard defines what happens when any integer
is the right operand of the assignment operator,
and the left operand is an unsigned type.

--
pete

pete 12-21-2003 02:27 PM

Re: unsigned short addition/subtraction overflow
 
CBFalconer wrote:
>
> pete wrote:
> > James Hu wrote:
> > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> > >
> > > > Are 1 through 4 defined behaviors in C?
> > > >
> > > > unsigned short i;
> > > > unsigned long li; /* 32-bit wide */
> > > >
> > > > 1. i = 65535 + 3;
> > > > 2. i = 1 - 3;
> > > > 3. li = (unsigned long)0xFFFFFFFF + 3;
> > > > 4. li = 1 - 3;
> > >
> > > Yes.

> >
> > No.
> > 65536 is an allowable value for INT_MAX.
> > (65535 + 3) would be integer overflow
> > and undefined behavior in that case.

>
> Good catch. However, change "would" to "could".


I think I got that right.
"in that case" meaning the case when 65536 was equal to INT_MAX,
then (65535 + 3) most definitely would overflow,
and definitely be undefined behavior.
The particular form of the manifestation of the undefined behavior,
would be different matter, if that's what you mean.

--
pete

pete 12-21-2003 02:28 PM

Re: unsigned short addition/subtraction overflow
 
pete wrote:
>
> Robert Stankowic wrote:
> >
> > "pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
> > news:3FE5809B.2575@mindspring.com...
> > > James Hu wrote:
> > > >
> > > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> > > > > Are 1 through 4 defined behaviors in C?
> > > > >
> > > > > unsigned short i;
> > > > > unsigned long li; /* 32-bit wide */
> > > > >
> > > > > 1. i = 65535 + 3;
> > > > > 2. i = 1 - 3;
> > > > > 3. li = (unsigned long)0xFFFFFFFF + 3;
> > > > > 4. li = 1 - 3;
> > > >
> > > > Yes.
> > >
> > > No.
> > > 65536 is an allowable value for INT_MAX.
> > > (65535 + 3) would be integer overflow
> > > and undefined behavior in that case.

> >
> > From N869: 6.2.5
> > 9 The range of nonnegative values of a
> > signed integer type is a subrange of
> > the corresponding unsigned integer type,
> > and the representation of the same value in each
> > type is the same.28)
> > 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.

>
> What's your point ?
> If INT_MAX equals 65535,


I meant 65536.

> then there are no unsigned operands in (65535 + 3), just two ints.
>
> > However, I am not sure about
> > i = 1 - 3;
> > and
> > li = 1 - 3;
> > but I think it is defined as far as I
> > understand the integer promotion rules.

>
> The standard defines what happens when any integer
> is the right operand of the assignment operator,
> and the left operand is an unsigned type.


--
pete

Andy 12-21-2003 03:03 PM

Re: unsigned short addition/subtraction overflow
 
bikejog@hotmail.com (Andy) wrote in message news:<aed59298.0312202231.78bb5fae@posting.google. com>...
> Hi,
> Are 1 through 4 defined behaviors in C?
>
> unsigned short i;
> unsigned long li; /* 32-bit wide */
>
> 1. i = 65535 + 3;
> 2. i = 1 - 3;
> 3. li = (unsigned long)0xFFFFFFFF + 3;
> 4. li = 1 - 3;
>
> TIA
> Andy


Actually what I really meant is for unsigned operations.
This is what I want to know. Are the following defined in
C and always guaranteed warped around values?

unsigned short i;
unsigned long li; /* 32-bit wide */

1. i = (unsigned short)65535 + (unsigned short)3;
2. i = (unsigned short)1 - (unsigned short)3;
3. li = (unsigned long)0xFFFFFFFF + (unsigned long)3;
4. li = (unsigned long)1 - (unsigned long)3;

TIA
Andy

Robert Stankowic 12-21-2003 06:31 PM

Re: unsigned short addition/subtraction overflow
 

"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
news:3FE5ADF9.1044@mindspring.com...
> pete wrote:
> >
> > Robert Stankowic wrote:
> > >
> > > "pete" <pfiland@mindspring.com> schrieb im Newsbeitrag

[....]

> > What's your point ?
> > If INT_MAX equals 65535,

>
> I meant 65536.
>
> > then there are no unsigned operands in (65535 + 3), just two ints.


I see your point.
Thanks
Robert




All times are GMT. The time now is 01:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.