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


Groups > comp.lang.c > #387502

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

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: What is your opinion about unsigned int u = -2 ?
Date 2024-08-11 23:07 -0700
Organization A noiseless patient Spider
Message-ID <86le12ff4r.fsf@linuxsc.com> (permalink)
References <v8dfo9$1k7cg$1@dont-email.me> <87mslxe4wj.fsf@bsb.me.uk> <86y152n9c8.fsf@linuxsc.com> <v9b5od$2rta7$1@dont-email.me>

Show all headers | View raw


Vir Campestris <vir.campestris@invalid.invalid> writes:

> On 11/08/2024 20:33, Tim Rentsch wrote:
>
>> Ick.  That choice is exactly backwards IMO.  Converting -1 to
>> an unsigned type always sets all the bits.  Converting -1u to
>> an unsigned type can easily do the wrong thing, depending
>> on the target type.
>
> "Converting -1 to an unsigned type always sets all the bits"
>
> In any normal twos complement architecture that's the case.  But there
> are a few oddballs out there where -1 is +1, except that the dedicated
> sign bit is set.

What you say is right if the transformation occurs by means of
type punning, as for example if we had a union with both a
signed int member and an unsigned int member.  Reading the
unsigned int member after having assigned to the signed int
member depends on whether signed int uses ones' complement,
two's complement, or signed magnitude.

My comment though is about conversion, not about type punning.
The rules for conversion (both explicit conversion, when a cast
operator is used, and implicit conversion, such as when an
assignment is performed (and lots of other places)) are defined
in terms of values, not representations.  Thus, in this code

    signed char minus_one = -1;
    unsigned u = minus_one;
    unsigned long bigu = minus_one;
    unsigned long long biggeru = minus_one;
    printf( " printing unsigned : %u\n", (unsigned) minus_one );

in every case we get unsigned values (of several lengths) with
all value bits set, because of the rules for how values are
converted between a signed type (such as signed char) and an
unsigned type.

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