Velocity Reviews > unsigned short addition/subtraction overflow

# unsigned short addition/subtraction overflow

Andy
Guest
Posts: n/a

 12-31-2003
Richard Heathfield <(E-Mail Removed)> wrote in message news:<bsocf3\$oo1\$(E-Mail Removed)>...
> Andy wrote:
>
> >
> > Can unsigned short be more than 2 bytes long?

>
> Yes. On a Cray, for example, it might easily be eight bytes long.
>

Really? From my 1st day at work, I've been taught that
unsigned short is always 2 bytes long while int is variable in length.
You learn something everyday.

Thanks
Andy

Ben Pfaff
Guest
Posts: n/a

 12-31-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Andy) writes:

> Richard Heathfield <(E-Mail Removed)> wrote in message news:<bsocf3\$oo1\$(E-Mail Removed)>...
> > Andy wrote:
> >
> > >
> > > Can unsigned short be more than 2 bytes long?

> >
> > Yes. On a Cray, for example, it might easily be eight bytes long.

>
> Really? From my 1st day at work, I've been taught that
> unsigned short is always 2 bytes long while int is variable in length.

Interesting. `unsigned short' is commonly 2 bytes in size, and 2
bytes is commonly 16 bits, but neither is required. 16 bits is
the minimum size of an `unsigned short', though.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Richard Heathfield
Guest
Posts: n/a

 12-31-2003
Andy wrote:
> Richard Heathfield wrote:
>> Andy wrote:
>> > Can unsigned short be more than 2 bytes long?

>>
>> Yes. On a Cray, for example, it might easily be eight bytes long.

>
> Really? From my 1st day at work, I've been taught that
> unsigned short is always 2 bytes long while int is variable in length.
> You learn something everyday.

Here's another one for your collection of strange facts. On a 32-bit DSP,
you might easily find that unsigned short is just *one* byte long (that
byte being 32 bits wide, of course).

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Chris Torek
Guest
Posts: n/a

 12-31-2003
In article <news:(E-Mail Removed). com>
Andy <(E-Mail Removed)> writes:
>... I guess, then I can say that unsigned short
>addition/subtraction on a machine where sizeof(unsigned short) ==
>sizeof(int) will always yield valid modulus 2^n results. Correct?

Well, C99 allows for "padding bits" in various representations, in
which case it is possible (though bizarre) to have, e.g.,
CHAR_BIT = 9, sizeof(short) = sizeof(unsigned short) = 2, and
sizeof(int) = 2; yet INT_MAX could be 131071 (no padding bits)
while USHRT_MAX is 65535 (two padding bits). In this case, since
USHRT_MAX < INT_MAX, "unsigned short" promotes to (signed) int
under the ANSI/ISO widening rules!

Such an implementation is certainly bizarre, but it appears to meet
the Standard's requirements.

Note that unless an implementation is *really* strange, we either
have USHRT_MAX > INT_MAX (e.g., 16-bit PDP-11), or 2*USHRT_MAX <=
INT_MAX (e.g., 16-bit "short", 32-bit "int" systems). If the first
is true -- if USHRT_MAX exceeds INT_MAX -- then unsigned short
promotes to unsigned int and arithmetic works mod 2**n. Otherwise,
double USHRT_MAX is still a valid "int" value, so the sum of any
two unsigned short values after they are promoted to signed int
values is a valid, nonnegative "int" value. For instance,
65535 + 65535 is 131070, and on 18-bit-or-more machines this should
be strictly less than INT_MAX (131071 or more). Of course, the
difference (subtraction) of two such values can be a negative "int",
and the sum of three or more such values can overflow.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

Kelsey Bjarnason
Guest
Posts: n/a

 01-01-2004
On Tue, 30 Dec 2003 20:37:08 -0800, Andy wrote:

> Richard Heathfield <(E-Mail Removed)> wrote in message news:<bsocf3\$oo1\$(E-Mail Removed)>...
>> Andy wrote:
>>
>> >
>> > Can unsigned short be more than 2 bytes long?

>>
>> Yes. On a Cray, for example, it might easily be eight bytes long.
>>

>
> Really? From my 1st day at work, I've been taught that
> unsigned short is always 2 bytes long while int is variable in length.
> You learn something everyday.

Y'all been taught wrong. Types in C and C++ (apart from the new
specified-size types in C99) are defined by minimum required ranges (and
the caveat that some types must be as large or larger than others).

Net result: an unsigned can be one byte, or 128 bytes, as long as it
handles _at least_ the minimum required range.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pozz C Programming 12 03-20-2011 11:32 PM myx C Programming 2 05-26-2009 06:22 PM Ioannis Vranos C Programming 5 03-05-2008 01:25 AM fancyerii Java 21 11-05-2007 09:33 PM slougheed@gmail.com C++ 4 10-16-2006 11:25 PM

Advertisments