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


Groups > comp.lang.c > #164425

Re: does a double cast to unsigned makes sense in any circumstance?

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: does a double cast to unsigned makes sense in any circumstance?
Date 2022-01-15 23:12 -0500
Organization A noiseless patient Spider
Message-ID <ss0605$e73$1@dont-email.me> (permalink)
References <86czl8kaew.fsf@levado.to> <sqvdud$3tt$1@dont-email.me> <86bl0ctree.fsf@levado.to>

Show all headers | View raw


On 1/15/22 8:12 PM, Meredith Montgomery wrote:
...
> Interesting.  I got another question here.  If s[pos] is always a char
> and '0' is always a char, then s[pos] - '0' will never be greater than a
> char.  It might be negative.  If it's negative and if I do

Note that while members of the basic character set are required to be
represented by non-negative values (6.2.5p3), members of the extended
character set are not. If char is signed, they can be as low as
CHAR_MIN, in which case subtracting '0' necessarily results in undefined
behavior. The following discussion applies only when that isn't the case:

>   (unsigner char) s[pos] - '0',
> 
> can this do any wrapping at all?  I'd say not in this case.  I think you
> are in more general waters than I took your message.  Can you clarify a
> bit?  I'm a bit lost.

Subtraction involves (6.5p6) the usual arithmetic conversions
(6.3.1.8p1), and the first part of that is the integer promotions
(6.3.1.1p2). Both overflow and wrap-around are possible results, but
only on systems where CHAR_BIT >= 16. Such systems do exist - the ones
I've heard of are embedded systems specialized for digital signal
processing.

If all values of type unsigned char can be represented as an int,
(unsigned char)s[pos] will be promoted to int before performing the
subtraction (6.3.1.1p2). In the unlikely event that CHAR_MAX == INT_MAX,
(int)(unsigned char)s[pos] will have the same value as s[pos].
Therefore, if s[pos] is sufficiently close to INT_MIN, subtracting '0'
from it will have undefined behavior.

In the unlikely event that UCHAR_MAX > INT_MAX, s[pos] is promoted to an
unsigned int (6.3.1.1p2). Since '0' has the type 'int', and 'int' and
unsigned int' have the same integer conversion rank, the usual
arithmetic conversions specify that '0' is converted to unsigned int,
and the subtraction is carried out in that type. If s[pos] is less than
'0', then s[pos] - '0' will have a mathematical value that is negative,
so it will have to wrap around.

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


Thread

does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-03 14:15 -0300
  Re: does a double cast to unsigned makes sense in any circumstance? Bart <bc@freeuk.com> - 2022-01-03 17:32 +0000
    Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-15 22:09 -0300
  Re: does a double cast to unsigned makes sense in any circumstance? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-03 10:06 -0800
    Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-15 22:12 -0300
      Re: does a double cast to unsigned makes sense in any circumstance? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-15 23:12 -0500
        Re: does a double cast to unsigned makes sense in any circumstance? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-01-20 19:37 -0800
          Re: does a double cast to unsigned makes sense in any circumstance? "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2022-01-21 09:35 -0800
            Re: does a double cast to unsigned makes sense in any circumstance? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-01-21 17:19 -0800
  Re: does a double cast to unsigned makes sense in any circumstance? Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-03 19:31 +0100
    Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-15 22:14 -0300
      Re: does a double cast to unsigned makes sense in any circumstance? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-15 23:24 -0500
        Re: does a double cast to unsigned makes sense in any circumstance? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-16 13:30 -0800
          Re: does a double cast to unsigned makes sense in any circumstance? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-16 23:25 -0500
          Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-17 09:43 -0300
            Re: does a double cast to unsigned makes sense in any circumstance? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-17 13:59 -0800
              Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 22:13 -0300
                Re: does a double cast to unsigned makes sense in any circumstance? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-28 21:29 -0500
                Re: does a double cast to unsigned makes sense in any circumstance? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-02-03 22:28 -0800
                Re: does a double cast to unsigned makes sense in any circumstance? "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2022-02-04 10:43 -0800
                Re: does a double cast to unsigned makes sense in any circumstance? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-28 19:37 -0800
                Re: does a double cast to unsigned makes sense in any circumstance? Manfred <noname@add.invalid> - 2022-01-30 03:45 +0100
                Re: does a double cast to unsigned makes sense in any circumstance? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-30 00:01 -0800
                Re: does a double cast to unsigned makes sense in any circumstance? Richard Damon <Richard@Damon-Family.org> - 2022-01-30 12:59 -0500
        Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-17 09:41 -0300
  Re: does a double cast to unsigned makes sense in any circumstance? Richard Damon <Richard@Damon-Family.org> - 2022-01-03 14:37 -0500
    Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-15 22:15 -0300
      Re: does a double cast to unsigned makes sense in any circumstance? Richard Damon <Richard@Damon-Family.org> - 2022-01-15 20:55 -0500
        Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-17 09:44 -0300
    Re: does a double cast to unsigned makes sense in any circumstance? Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-16 05:13 +0100
  Re: does a double cast to unsigned makes sense in any circumstance? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-01-15 18:53 -0800
    Re: does a double cast to unsigned makes sense in any circumstance? Meredith Montgomery <mmontgomery@levado.to> - 2022-01-17 09:46 -0300
    Re: does a double cast to unsigned makes sense in any circumstance? Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-29 16:57 +0100
      Re: does a double cast to unsigned makes sense in any circumstance? Öö Tiib <ootiib@hot.ee> - 2022-01-29 09:54 -0800
        Re: does a double cast to unsigned makes sense in any circumstance? Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-30 13:17 +0100
          Re: does a double cast to unsigned makes sense in any circumstance? Öö Tiib <ootiib@hot.ee> - 2022-01-30 23:53 -0800

csiph-web