Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23560

Re: Unsinged types

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Unsinged types
Date 2012-07-22 10:37 +0100
Organization A noiseless patient Spider
Message-ID <jughmj$ph$1@dont-email.me> (permalink)
References (1 earlier) <87obnbfjvm.fsf@blp.benpfaff.org> <juchir$ut$1@usenet.stanford.edu> <jucjal$b8e$1@dont-email.me> <juf5mm$mr$1@usenet.stanford.edu> <kfnfw8k1dpf.fsf@x-alumni2.alumni.caltech.edu>

Show all headers | View raw


"Tim Rentsch" <txr@alumni.caltech.edu> wrote in message 
news:kfnfw8k1dpf.fsf@x-alumni2.alumni.caltech.edu...
> Seungbeom Kim <musiphil@bawi.org> writes:
>
>> On 2012-07-20 14:41, Eric Sosman wrote:
> [snip]
>>> And in the cases where naive subtraction doesn't work, you don't
>>> just get a possibly surprising but predictable outcome: You get
>>> undefined behavior.
>>
>> True. But (2U - 5U) yielding 4294967293U is not very useful, no matter
>> how predictable it is, [snip]
>
> That's an overstatement.  It may not be useful in ways that
> you would like it to be, or in all the cases you would like
> it to be, but there still are lots of cases where it is
> useful.  To give one example, if we want to test if
> variable 'i' is in the range [20 .. 30), with signed
> arithmetic that takes two comparisons, but with unsigned
> arithmetic this can be done with one comparison:
>
>    if(  (i-20u) < 10  ) ...

You're replacing two comparisons, with a comparison and a subtraction, and 
now have code that will cause people to scratch their heads (with the 
mysterious introduction of '10' and '20u') instead of just writing:

   if (i>=20 && i<30)

and leaving it to the compiler to sort out, as we're constantly told to do. 
It's possible also that i will be <20, so only one comparison gets done 
anyway.

(I do  sometimes use the idiom, in assembly, but when i needs to be offset 
to start at zero anyway.)

> Unsigned arithmetic doesn't behave in a way that most
> people are used to

'Unintuitive'

-- 
Bartc 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Unsinged types Jase Schick <nospam@nospam.com> - 2012-07-19 20:03 +0000
  Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-19 21:29 +0100
    Re: Unsinged types jacob navia <jacob@spamsink.net> - 2012-07-19 23:15 +0200
  Re: Unsinged types Robert Wessel <robertwessel2@yahoo.com> - 2012-07-19 16:46 -0500
  Re: Unsinged types Ben Pfaff <blp@cs.stanford.edu> - 2012-07-19 15:47 -0700
    Re: Unsinged types Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-19 21:06 -0400
      Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-20 02:32 +0100
        Re: Unsinged types Stephen Sprunk <stephen@sprunk.org> - 2012-07-19 21:08 -0500
          Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-20 04:26 +0100
        Re: Unsinged types Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-19 22:56 -0400
          Re: Unsinged types Kenneth Brody <kenbrody@spamcop.net> - 2012-07-20 14:57 -0400
          Re: Unsinged types Keith Thompson <kst-u@mib.org> - 2012-07-20 12:36 -0700
      Re: Unsinged types Kenneth Brody <kenbrody@spamcop.net> - 2012-07-20 14:56 -0400
    Re: Unsinged types Seungbeom Kim <musiphil@bawi.org> - 2012-07-20 14:11 -0700
      Re: Unsinged types Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-20 17:41 -0400
        Re: Unsinged types Seungbeom Kim <musiphil@bawi.org> - 2012-07-21 14:07 -0700
          Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-21 22:26 +0100
          Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-22 00:01 -0700
            Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-22 10:37 +0100
              Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:34 -0700
            Re: Unsinged types gwowen <gwowen@gmail.com> - 2012-07-23 00:06 -0700
              Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:30 -0700
          Re: Unsinged types Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-22 14:29 +0300
            Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-22 14:43 +0100
              Re: Unsinged types Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-22 19:12 +0300
            Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-22 12:14 -0700
        Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-21 14:57 -0700
  Re: Unsinged types Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-07-20 00:11 -0700
  Re: Unsinged types Andrew Cooper <amc96@cam.ac.uk> - 2012-07-20 10:00 +0100
    Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-20 23:48 -0700
      Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 15:24 +0100
        Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 08:53 -0700
          Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 17:48 +0100
            Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 11:22 -0700
              Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 22:35 +0100
                Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 14:55 -0700
          Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-21 18:27 +0100
            Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 11:37 -0700
              Re: Unsinged types Kenneth Brody <kenbrody@spamcop.net> - 2012-07-23 14:02 -0400
                Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-23 12:21 -0700
                Re: Unsinged types pete <pfiland@mindspring.com> - 2012-07-23 23:57 -0400
  Re: Unsinged types "Bill Leary" <Bill_Leary@msn.com> - 2012-07-20 20:21 -0400
  Re: Unsinged types "io_x" <a@b.c.invalid> - 2012-07-22 10:36 +0200

csiph-web