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


Groups > comp.lang.c > #164438

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

From Meredith Montgomery <mmontgomery@levado.to>
Newsgroups comp.lang.c
Subject Re: does a double cast to unsigned makes sense in any circumstance?
Date 2022-01-17 09:41 -0300
Organization Aioe.org NNTP Server
Message-ID <86fspmr0u1.fsf@levado.to> (permalink)
References <86czl8kaew.fsf@levado.to> <sqvfed$rps$1@dont-email.me> <8635lotrby.fsf@levado.to> <ss06le$h5n$1@dont-email.me>

Show all headers | View raw


James Kuyper <jameskuyper@alumni.caltech.edu> writes:

> On 1/15/22 8:14 PM, Meredith Montgomery wrote:
>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>> 
>>> Am 03.01.2022 um 18:15 schrieb Meredith Montgomery:
>>>> I've seen this somewhere or I wrote this some time in the past for some
>>>> reason and I can't see how this make sense any longer.  In a procedure
>>>> to read a numeric string and turn it into a number, we need to convert
>>>> each char-digit to some kind of integer:
>>>>    (uint64_t) (unsigned char) (s[pos] - '0');
>>>
>>> (s[pos] - '0') might me signed and without the cast to unsigned
>>> char it might be sign-extended to int64_4 before it is converted
>>> to uint64_t. ...
>
> He's right about it being signed - that's normally the case. It normally
> has the type `int`, except in the extremely unlikely case that CHAR_MAX
>> INT_MAX, in which case it will have the type `unsigned int`. However,
> int64_t comes into play only if it's typedef for `int`.
>
>>> ... But there are no guarantees when converting negative
>>> values to unsigneds.
>> 
>> Really, no guarantees?  So a procedure that does
>
> No, Bonita is mistaken. The standard provides a strong explicit
> guarantee of what the behavior is when converting negative values to
> unsigned type:
>
> "... the value is converted by repeatedly adding ... one more than the
> maximum value that can be represented in the new type until the value is
> in the range of the new type." (6.3.1.3p2).
>
> It's the other direction that's dangerous: conversion of an integer
> value to a signed integer type that is not representable in that type
> has undefined behavior.

That makes perfect sense.  Thanks for the info.  Very appreciated.

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