unsigned short addition/subtraction overflow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy

    #31
    Re: unsigned short addition/subtraction overflow

    Richard Heathfield <dontmail@addre ss.co.uk.invali d> wrote in message news:<bsocf3$oo 1$1@sparta.btin ternet.com>...[color=blue]
    > Andy wrote:
    >[color=green]
    > >
    > > Can unsigned short be more than 2 bytes long?[/color]
    >
    > Yes. On a Cray, for example, it might easily be eight bytes long.
    >[/color]

    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

    Comment

    • Ben Pfaff

      #32
      Re: unsigned short addition/subtraction overflow

      bikejog@hotmail .com (Andy) writes:
      [color=blue]
      > Richard Heathfield <dontmail@addre ss.co.uk.invali d> wrote in message news:<bsocf3$oo 1$1@sparta.btin ternet.com>...[color=green]
      > > Andy wrote:
      > >[color=darkred]
      > > >
      > > > Can unsigned short be more than 2 bytes long?[/color]
      > >
      > > Yes. On a Cray, for example, it might easily be eight bytes long.[/color]
      >
      > 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.[/color]

      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;}

      Comment

      • Richard Heathfield

        #33
        Re: unsigned short addition/subtraction overflow

        Andy wrote:[color=blue]
        > Richard Heathfield wrote:[color=green]
        >> Andy wrote:[color=darkred]
        >> > Can unsigned short be more than 2 bytes long?[/color]
        >>
        >> Yes. On a Cray, for example, it might easily be eight bytes long.[/color]
        >
        > 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.[/color]

        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 : binary@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

        Comment

        • Chris Torek

          #34
          Re: unsigned short addition/subtraction overflow

          In article <news:aed59298. 0312302033.1943 d7c6@posting.go ogle.com>
          Andy <bikejog@hotmai l.com> writes:[color=blue]
          >... 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?[/color]

          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.

          Comment

          • Kelsey Bjarnason

            #35
            Re: unsigned short addition/subtraction overflow

            On Tue, 30 Dec 2003 20:37:08 -0800, Andy wrote:
            [color=blue]
            > Richard Heathfield <dontmail@addre ss.co.uk.invali d> wrote in message news:<bsocf3$oo 1$1@sparta.btin ternet.com>...[color=green]
            >> Andy wrote:
            >>[color=darkred]
            >> >
            >> > Can unsigned short be more than 2 bytes long?[/color]
            >>
            >> Yes. On a Cray, for example, it might easily be eight bytes long.
            >>[/color]
            >
            > 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.[/color]

            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.


            Comment

            Working...