Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: sizeof Data types |
| Date | 2016-08-09 11:23 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87k2fqgs0e.fsf@bsb.me.uk> (permalink) |
| References | (3 earlier) <no9dgf$a05$1@dont-email.me> <450a4877-bb68-48d5-b24e-123ca59569a1@googlegroups.com> <noae50$l9r$1@dont-email.me> <433a7e7e-5f86-4a08-864f-f6074bf53cc8@googlegroups.com> <noc19a$d5h$1@dont-email.me> |
David Brown <david.brown@hesbynett.no> writes:
> On 08/08/16 23:09, supercat@casperkitty.com wrote:
>> On Monday, August 8, 2016 at 12:05:44 PM UTC-5, David Brown wrote:
>>> If you don't want implementation dependent behaviour in your programs,
>>> don't write code that depends on the implementation.
>>>
>>> int diffcomp(uint16_t a, uint16_b, uint16_t c) {
>>> return (unsigned int) a - b > c;
>>> }
>>
>> The effect of that will still vary depending upon the implementation.
>
> Perhaps I have got the details wrong, but my understanding here is:
>
> uint16_t is either "unsigned short", "unsigned int", or an unsigned
> extended integer type of lower rank than "unsigned int".
>
> "a" is cast to an unsigned int. This either involves no change at all,
> or a safe conversion since the range of unsigned int is at least as big
> as that of uint16_t.
>
> Before the "-" operation, the two operands need to be the same type.
> Since "unsigned int" has higher or equal rank to "uint16_t", and both
> are unsigned types, b is converted/promoted to "unsigned int".
>
> The "-" operation is carried out with unsigned int arithmetic (modular
> overflow handling).
>
> Similarly, "c" is converted/promoted as necessary to unsigned int, and
> the comparison is done with unsigned int arithmetic.
>
> The result of the comparison is an int, with value 0 or 1.
>
> The legal values for the arguments to the function are all 0..65535, as
> that is how uint16_t is defined.
>
> I see no scope for implementation-dependent behaviour here. But if you
> or anyone else sees something that I've missed, please say.
Take, for example, a=0, b=1, c=65535. On a machine with a 32-bit int
your code gives 1, but if the machine has a 16 bit int the result will
be 0.
>> Handling this particular expression in portable fashion requires casting
>> the arithmetic result, e.g.
>>
>> int diffcomp(uint16_t a, uint16_b, uint16_t c) {
>> return (uint16_t)(a - b) > c;
>
> That would work too, as far as I can see.
This version is just the zero function. Handling this in a portable
fashion really means writing
int diffcomp(uint16_t a, uint16_b, uint16_t c) { return 0; }
or, more likely, throwing the function away.
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sizeof Data types Real Troll <real.troll@trolls.com> - 2016-08-05 18:45 -0400
sizeof Data types jameskuyper@verizon.net - 2016-08-05 17:10 -0700
Re: sizeof Data types raltbos@xs4all.nl (Richard Bos) - 2016-08-06 10:35 +0000
Re: sizeof Data types raltbos@xs4all.nl (Richard Bos) - 2016-08-06 10:36 +0000
Re: sizeof Data types Jean-Marc Bourguet <jm@bourguet.org> - 2016-08-06 22:29 +0200
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-09 12:55 -0400
Re: sizeof Data types BartC <bc@freeuk.com> - 2016-08-06 21:43 +0100
Re: sizeof Data types jameskuyper@verizon.net - 2016-08-06 15:25 -0700
Re: sizeof Data types David Brown <david.brown@hesbynett.no> - 2016-08-08 09:48 +0200
Re: sizeof Data types James Kuyper <jameskuyper@verizon.net> - 2016-08-08 11:05 -0400
Re: sizeof Data types David Brown <david.brown@hesbynett.no> - 2016-08-08 17:57 +0200
Re: sizeof Data types supercat@casperkitty.com - 2016-08-08 08:36 -0700
Re: sizeof Data types David Brown <david.brown@hesbynett.no> - 2016-08-08 19:05 +0200
Re: sizeof Data types supercat@casperkitty.com - 2016-08-08 14:09 -0700
Re: sizeof Data types David Brown <david.brown@hesbynett.no> - 2016-08-09 09:38 +0200
Re: sizeof Data types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-09 11:23 +0100
Re: sizeof Data types Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-12 07:42 -0700
Re: sizeof Data types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-12 21:38 +0100
Re: sizeof Data types Keith Thompson <kst-u@mib.org> - 2016-08-09 08:12 -0700
Re: sizeof Data types supercat@casperkitty.com - 2016-08-10 10:36 -0700
Re: sizeof Data types Keith Thompson <kst-u@mib.org> - 2016-08-10 11:42 -0700
Re: sizeof Data types Philip Lantz <prl@canterey.us> - 2016-08-11 10:20 -0700
Re: sizeof Data types Keith Thompson <kst-u@mib.org> - 2016-08-11 12:04 -0700
Re: sizeof Data types Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-12 08:22 -0700
Re: sizeof Data types Nobody <nobody@nowhere.invalid> - 2016-08-07 01:32 +0100
Re: sizeof Data types Keith Thompson <kst-u@mib.org> - 2016-08-06 18:10 -0700
Re: sizeof Data types Siri Cruise <chine.bleu@yahoo.com> - 2016-08-06 20:22 -0700
Re: sizeof Data types Ian Collins <ian-news@hotmail.com> - 2016-08-07 15:40 +1200
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-09 13:06 -0400
Re: sizeof Data types Siri Cruise <chine.bleu@yahoo.com> - 2016-08-09 12:37 -0700
Re: sizeof Data types Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-11 09:18 -0700
Re: sizeof Data types Siri Cruise <chine.bleu@yahoo.com> - 2016-08-11 11:11 -0700
Re: sizeof Data types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-08-11 12:26 -0600
Re: sizeof Data types David Brown <david.brown@hesbynett.no> - 2016-08-11 23:31 +0200
Re: sizeof Data types supercat@casperkitty.com - 2016-08-11 14:59 -0700
Re: sizeof Data types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-08-12 00:15 -0700
Re: sizeof Data types Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-12 08:05 -0700
Re: sizeof Data types John Bode <jfbode1029@gmail.com> - 2016-08-08 12:38 -0700
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-09 13:08 -0400
Re: sizeof Data types Keith Thompson <kst-u@mib.org> - 2016-08-09 10:43 -0700
Re: sizeof Data types "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-09 13:41 -0400
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-10 11:51 -0400
Re: sizeof Data types "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-10 12:35 -0400
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-11 11:53 -0400
Re: sizeof Data types "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-13 11:29 -0400
Re: sizeof Data types supercat@casperkitty.com - 2016-08-13 09:18 -0700
Re: sizeof Data types Keith Thompson <kst-u@mib.org> - 2016-08-10 10:13 -0700
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-11 11:56 -0400
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-09 12:59 -0400
Re: sizeof Data types "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-09 13:49 -0400
Re: sizeof Data types Ken Brody <kenbrody@spamcop.net> - 2016-08-10 12:08 -0400
Re: sizeof Data types Keith Thompson <kst-u@mib.org> - 2016-08-09 11:35 -0700
csiph-web