Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #164419
| Path | csiph.com!aioe.org!/eRJ076R8qgwl6rQB+gRxA.user.46.165.242.75.POSTED!not-for-mail |
|---|---|
| From | Meredith Montgomery <mmontgomery@levado.to> |
| Newsgroups | comp.lang.c |
| Subject | Re: does a double cast to unsigned makes sense in any circumstance? |
| Date | Sat, 15 Jan 2022 22:12:41 -0300 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <86bl0ctree.fsf@levado.to> (permalink) |
| References | <86czl8kaew.fsf@levado.to> <sqvdud$3tt$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | gioia.aioe.org; logging-data="52949"; posting-host="/eRJ076R8qgwl6rQB+gRxA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; |
| Cancel-Lock | sha1:LPs8WRiQUJDSKO6GtxTVL/dmRhw= |
| X-Notice | Filtered by postfilter v. 0.9.2 |
| Xref | csiph.com comp.lang.c:164419 |
Show key headers only | View raw
Andrey Tarasevich <andreytarasevich@hotmail.com> writes: > On 1/3/2022 9:15 AM, Meredith Montgomery wrote: >> 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'); >> But the double cast here seems odd. I could have written this by >> copying it from somewhere else. Today, what I would write is just >> (uint64_t) (s[pos] - '0'); >> Am I making a mistake now? I can't anything wrong with this. Isn't >> a >> char just an int? I'm turning an int into an unsigned integer (of a >> larger size). > > Yes, it does make sense. For example, if the outer type is wider than > the inner type, as in your example. > > A cast to unsigned type is not just a conceptual type change. It also > might transform (wrap) the value in accordance with the rules of > modulo arithmetic, with modulo equal to 2^(type width). > > So, the first cast is intended to wrap the value. The second cast is > intended to "expand" the result to a desired target type. 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 (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.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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