Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.c > #387309

Re: What is your opinion about unsigned int u = -2 ?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: What is your opinion about unsigned int u = -2 ?
Date 2024-08-03 16:10 -0700
Organization None to speak of
Message-ID <87v80hdwyx.fsf@nosuchdomain.example.com> (permalink)
References (9 earlier) <v8k055$33fcl$1@dont-email.me> <87cymqfl3m.fsf@nosuchdomain.example.com> <v8k2ck$33nca$2@dont-email.me> <874j82fiew.fsf@nosuchdomain.example.com> <v8l887$3edtr$1@dont-email.me>

Show all headers | View raw


You must have done something odd when posting your followup.  The
attributions are messed up.

Thiago Adams <thiago.adams@gmail.com> writes:
> -------- Mensagem encaminhada --------
> Assunto: Re: What is your opinion about unsigned int u = -2 ?
> Data: Fri, 02 Aug 2024 19:29:27 -0700
> De: Keith Thompson <Keith.S.Thompson+u@gmail.com>
> Organização: None to speak of
> Grupos de notícias: comp.lang.c
> Referências: <v8dfo9$1k7cg$1@dont-email.me>
> <pan$d2c8a$8c54ac9f$29a202e0$12c6ce86@invalid.invalid>
> <87bk2cecan.fsf@bsb.me.uk> <v8inds$2qpqh$1@dont-email.me>
> <v8iqnr$7l3c$1@news.xmission.com> <v8irje$2rolg$1@dont-email.me>
> <87r0b6g3qx.fsf@nosuchdomain.example.com>
> <v8jbj5$2us0r$4@dont-email.me> <v8jvln$33atp$1@dont-email.me>
> <v8k055$33fcl$1@dont-email.me>
> <87cymqfl3m.fsf@nosuchdomain.example.com>
> <v8k2ck$33nca$2@dont-email.me>
>
> Thiago Adams <thiago.adams@gmail.com> writes:
>> Em 8/2/2024 10:31 PM, Keith Thompson escreveu:
>>> Thiago Adams <thiago.adams@gmail.com> writes:
>>> [...]
>>>> It is interesting to compare constexpr with the existing constant
>>>> expression in C that works with integers.Compilers extend to work with
>>>> unsigned long long.
>>>> constexpr works with the sizes as defined , for instance char.
>>> I'm not sure what you mean by "Compilers extend to work with
>>> unsigned long long.".
>>
>> enum {C = 18446744073709551615 -1 };
>> //        ~~~~~~~~~~~~~~~~~~~~
>> //        ^ warning: integer constant is so large that it is unsigned
>>
>> https://godbolt.org/z/K7hzczETP
>
> Since 18446744073709551615 is 2**64-1, it's outside the range of any
> signed integer type in typical implementations.  Since unsuffixed
> integer constants are of some signed type (since C99), that constant is
> likely to cause problems.  You could write 18446744073709551615ull.
>
> That's a rather odd warning.  In C90, an unsuffixed integer constant's
> type was the first of (int, long int, unsigned long int) in which its
> value would fit.  In C99 and later, an unsuffixed integer constant is of
> type int, long int, or long long int, *never* of any unsigned type.
> Since 18446744073709551615 exceeds ULLONG_MAX (assuming 64 bits),
> apparently gcc treats it as having an unsigned type as an extension.
>
KT> (My quick experiment indicates that, at least on my system,
KT> 18446744073709551615 is of type __int128 and, bizarrely,
KT> 18446744073709551616 is of type int with the value 0.  This seems
KT> like a bug.)

[...]

TA> I checked and 18446744073709551615 is not unsigned long long
TA>
TA> static_assert(TYPE_IS(18446744073709551615, unsigned long long));
TA>
TA> https://godbolt.org/z/vnzWWxvjr

Right.  As I said, gcc gives it type __int128.  This is to be expected
if __int128 is an *extended integer type*, but in fact it's an
extension, and gcc's behavior appears reasonable on that basis.  In the
absence of integer types wider than 64 bits, 18446744073709551615
(2**64-1) has no type.  If you need that particular value in your code,
I suggest finding a different way to express it.  18446744073709551615u
or 0xffffffffffffffff are possibilities; so is ULLONG_MAX if long long
is 64 bits and that expresses your intent more clearly.

>>"In C99 and later, an unsuffixed integer constant is of
>>type int, long int, or long long int, *never* of any unsigned type."
>
> I think this should be reviewed before they constexpr was added in C.

That's not going to happen, and I'm not sure why it should.

> I am fixing my constant expression evaluation in cake and I will share
> the code of "old" constant expressions and new constexpr. So constexpr
> does not have restriction on signed types only.

constexpr has never been restricted to signed types.  Unsuffixed integer
constants are.  That's why there are suffixed integer constants.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-07-31 10:55 -0300
  Re: What is your opinion about unsigned int u = -2 ? Ben Bacarisse <ben@bsb.me.uk> - 2024-07-31 20:29 +0100
    Re: What is your opinion about unsigned int u = -2 ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 12:33 -0700
      Re: What is your opinion about unsigned int u = -2 ? Vir Campestris <vir.campestris@invalid.invalid> - 2024-08-11 21:08 +0100
        Re: What is your opinion about unsigned int u = -2 ? Richard Damon <richard@damon-family.org> - 2024-08-11 16:45 -0400
        Re: What is your opinion about unsigned int u = -2 ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-11 16:53 -0400
          Re: What is your opinion about unsigned int u = -2 ? Vir Campestris <vir.campestris@invalid.invalid> - 2024-08-12 11:47 +0100
        Re: What is your opinion about unsigned int u = -2 ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 23:07 -0700
  Re: What is your opinion about unsigned int u = -2 ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-07-31 17:17 -0400
  Re: What is your opinion about unsigned int u = -2 ? Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-01 06:34 +0000
    Re: What is your opinion about unsigned int u = -2 ? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-01 12:02 +0100
      Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 10:36 -0300
        Re: What is your opinion about unsigned int u = -2 ? gazelle@shell.xmission.com (Kenny McCormack) - 2024-08-02 14:33 +0000
          Re: What is your opinion about unsigned int u = -2 ? Bart <bc@freeuk.com> - 2024-08-02 15:48 +0100
            Re: What is your opinion about unsigned int u = -2 ? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-02 16:17 +0100
            Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 15:39 -0300
              Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 15:58 -0300
            Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 11:48 -0700
              Re: What is your opinion about unsigned int u = -2 ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-02 15:21 -0400
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:03 -0300
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:12 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 18:31 -0700
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:50 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 19:29 -0700
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 09:36 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:10 -0700
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 22:46 -0300
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 23:04 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 18:25 -0700
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:44 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 19:04 -0700
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 23:27 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 19:40 -0700
                Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-03 19:43 +0200
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 14:46 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:52 -0700
                Re: What is your opinion about unsigned int u = -2 ? dave_thompson_2@comcast.net - 2024-08-25 16:52 -0400
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-25 14:28 -0700
                Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-03 19:34 +0200
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 14:51 -0300
                Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 15:30 -0300
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:46 -0700
                Re: What is your opinion about unsigned int u = -2 ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 13:57 -0700
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 14:53 -0700
                Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:19 -0700
              Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-09 20:10 +0200
            Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-03 19:17 +0200
          Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-04 20:29 +0200
            Re: What is your opinion about unsigned int u = -2 ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-07 23:48 +0000
              Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-08 19:47 +0200
                Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-09 20:08 +0200
                Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-09 20:19 +0200
                Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-09 21:18 +0200
                Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-09 21:40 +0200
                Re: What is your opinion about unsigned int u = -2 ? Richard Damon <richard@damon-family.org> - 2024-08-09 21:16 -0400
    Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-04 18:16 +0200

csiph-web