Velocity Reviews > unsigned short addition/subtraction overflow

Andy
Guest
Posts: n/a

 12-21-2003
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
Guest
Posts: n/a

 12-21-2003
On 2003-12-21, Andy <(E-Mail Removed)> 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
Guest
Posts: n/a

 12-21-2003
James Hu wrote:
>
> On 2003-12-21, Andy <(E-Mail Removed)> 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
Guest
Posts: n/a

 12-21-2003

"pete" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> James Hu wrote:
> >
> > On 2003-12-21, Andy <(E-Mail Removed)> 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.2 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
Guest
Posts: n/a

 12-21-2003
pete wrote:
> James Hu wrote:
> > On 2003-12-21, Andy <(E-Mail Removed)> 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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

pete
Guest
Posts: n/a

 12-21-2003
Robert Stankowic wrote:
>
> "pete" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > James Hu wrote:
> > >
> > > On 2003-12-21, Andy <(E-Mail Removed)> 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.2
> 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.

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
Guest
Posts: n/a

 12-21-2003
CBFalconer wrote:
>
> pete wrote:
> > James Hu wrote:
> > > On 2003-12-21, Andy <(E-Mail Removed)> 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
Guest
Posts: n/a

 12-21-2003
pete wrote:
>
> Robert Stankowic wrote:
> >
> > "pete" <(E-Mail Removed)> schrieb im Newsbeitrag
> > news:(E-Mail Removed)...
> > > James Hu wrote:
> > > >
> > > > On 2003-12-21, Andy <(E-Mail Removed)> 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.2
> > 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.

>
> 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
Guest
Posts: n/a

 12-21-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Andy) wrote in message news:<(E-Mail Removed). 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
Guest
Posts: n/a

 12-21-2003

"pete" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> pete wrote:
> >
> > Robert Stankowic wrote:
> > >
> > > "pete" <(E-Mail Removed)> 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.

Thanks
Robert