473,883 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 14 '05
34 16713
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bs******* ***@sparta.btin ternet.com>...
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
Nov 14 '05 #31
bi*****@hotmail .com (Andy) writes:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bs******* ***@sparta.btin ternet.com>...
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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #32
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 : bi****@eton.pow ernet.co.uk
"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
Nov 14 '05 #33
In article <news:ae******* *************** ****@posting.go ogle.com>
Andy <bi*****@hotmai l.com> 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.
Nov 14 '05 #34
On Tue, 30 Dec 2003 20:37:08 -0800, Andy wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bs******* ***@sparta.btin ternet.com>...
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.
Nov 14 '05 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

96
4012
by: John Harrison | last post by:
I knew that unsigned integral data types were the cause of scads of mostly spurious warning messages, but I didn't realise that they were a security risk too (see here http://www.securitytracker.com/alerts/2004/Feb/1009067.html). All for one measly extra bit. So has the time come for C++ to deprecate unsigned integral types? john
49
5930
by: Neil Zanella | last post by:
Hello, Often I happen to be dealing with nonnegative integers and since I know I won't need negative numbers here I declare them as unsigned simply to make the program somewhat clearer. Effectively, though, signed integers would often work too since unless I am doing some modular arithmetic modulo the word length then I almost never need to use the high bit since the integers I deal with are usually not that large, and I would assume...
20
5380
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include <stdlib.h>
3
4597
by: linq936 | last post by:
I have some algorithm dealing with unsigned int calculation a lot, I am trying to add some check for overflow. The initial thinking was very easy, just do something like this, int addition(unsigned int i1, unsigned int i2) { if ( i1 + i2 < i1 || i1 + i2 < i2 ) { ...throw some expception... } return i1+i2; }
0
9944
marktang
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...
0
9797
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,...
0
10762
jinu1996
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...
1
10863
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,...
1
7977
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5807
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...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.