(unsigned)-INT_MIN

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • viza

    (unsigned)-INT_MIN

    Hi all

    int i= INT_MIN;
    unsigned int u= -i;

    Is u guaranteed to have the absolute value of INT_MIN?

    Why it might not: -i has type (int), and -INT_MIN might be more than
    INT_MAX.

    A cast might not work either, because in (unsigned)-i; the cast is too
    late, and in -(unsigned)i then i cannot be represented as unsigned.

    If long is longer than int, I can do -(long)i, but what if we are talking
    about the longest integer type available?

    TIA
    viza
  • vippstar@gmail.com

    #2
    Re: (unsigned)-INT_MIN

    On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviousc hange.invalid>
    wrote:
    Hi all
    >
    int i= INT_MIN;
    unsigned int u= -i;
    >
    Is u guaranteed to have the absolute value of INT_MIN?
    >
    Why it might not: -i has type (int), and -INT_MIN might be more than
    INT_MAX.
    So what? corresponding integer types have the same range of values; If
    int has N values, unsigned int has N values.
    It is guaranteed.
    <snip>

    Comment

    • rahul

      #3
      Re: (unsigned)-INT_MIN

      On Jul 21, 1:53 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
      wrote:
      Hi all
      >
      int i= INT_MIN;
      unsigned int u= -i;
      >
      Is u guaranteed to have the absolute value of INT_MIN?
      On my system, u becomes 0 as INT_MIN is -2147483648 and INT_MAX is
      2147483647. So, -INT_MIN wraps around to 0.
      Why it might not: -i has type (int), and -INT_MIN might be more than
      INT_MAX.
      -INT_MIN is greater than INT_MAX on most of the systems.

      If long is longer than int, I can do -(long)i, but what if we are talking
      about the longest integer type available?
      What are we looking at? Is it about getting the absolute value of a
      signed int in an unsigned int?


      Comment

      • vippstar@gmail.com

        #4
        Re: (unsigned)-INT_MIN

        On Jul 21, 12:21 pm, vipps...@gmail. com wrote:
        On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviousc hange.invalid>
        wrote:Hi all
        >
        int i= INT_MIN;
        unsigned int u= -i;
        >
        Is u guaranteed to have the absolute value of INT_MIN?
        >
        Why it might not: -i has type (int), and -INT_MIN might be more than
        INT_MAX.
        >
        So what? corresponding integer types have the same range of values; If
        int has N values, unsigned int has N values.
        It is guaranteed.
        <snip>

        I'm sorry, I misunderstood you. You're interested in the case that
        INT_MIN < -INT_MAX.
        Well, in that case, computing -INT_MIN invokes undefined behavior;
        (overflow)
        Try this

        if(i == INT_MIN)
        u += INT_MAX + (unsigned)-(i + INT_MAX);
        u = -i;

        Comment

        • Old Wolf

          #5
          Re: (unsigned)-INT_MIN

          On Jul 21, 8:53 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
          wrote:
          int i= INT_MIN;
          unsigned int u= -i;
          >
          Is u guaranteed to have the absolute value of INT_MIN?
          No. On a normal (2's complement) system, the
          code causes undefined behaviour because the
          operation "-i" causes an integer overflow.

          Not sure what 'vipps' was on about..
          A cast might not work either, because in (unsigned)-i; the cast is too
          late, and in -(unsigned)i then i cannot be represented as unsigned.
          Signed values can be converted to unsigned ;
          (unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
          which is INT_MAX on the normal machine.

          Comment

          • vippstar@gmail.com

            #6
            Re: (unsigned)-INT_MIN

            On Jul 21, 12:38 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
            On Jul 21, 8:53 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
            wrote:
            >
            int i= INT_MIN;
            unsigned int u= -i;
            >
            Is u guaranteed to have the absolute value of INT_MIN?
            >
            No. On a normal (2's complement) system, the
            code causes undefined behaviour because the
            operation "-i" causes an integer overflow.
            >
            Not sure what 'vipps' was on about..
            Which one of my two (without counting this one) messages are you
            refering to?
            I posted a follow-up quoting my first message saying I misunderstood,
            and then I gave an answer which I believe to be correct. (and
            relevant)
            A cast might not work either, because in (unsigned)-i; the cast is too
            late, and in -(unsigned)i then i cannot be represented as unsigned.
            >
            Signed values can be converted to unsigned ;
            (unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
            which is INT_MAX on the normal machine.
            Wrong.
            Assuming i is -N with N >= 0, (unsigned)i is UINT_MAX - N + 1.
            So (unsigned)INT_M IN would be UINT_MAX + INT_MIN + 1.

            Comment

            • vippstar@gmail.com

              #7
              Re: (unsigned)-INT_MIN

              On Jul 21, 12:36 pm, vipps...@gmail. com wrote:
              <snip>
              Try this
              >
              if(i == INT_MIN)
              u += INT_MAX + (unsigned)-(i + INT_MAX);
              u = -i;
              ^
              insert 'else' there... :-( I should be looking twice at my messages
              before posting them.

              Comment

              • viza

                #8
                Re: (unsigned)-INT_MIN

                Hi

                On Mon, 21 Jul 2008 08:53:51 +0000, viza wrote:
                int i= INT_MIN;
                unsigned int u= -i;
                >
                Is u guaranteed to have the absolute value of INT_MIN?
                If long is longer than int, I can do -(long)i, but what if we are
                talking about the longest integer type available?
                Thanks for the answers so far, but none of them help. Perhaps I can
                explain it a bit better:

                What I would like is a constant expression of unsigned type for the
                absolute value of an constant negative expression of the corresponding
                signed type.

                Ie:

                1) I know i is negative
                2) I want (unsigned)abs(i )

                The answer simply: ( (unsigned)-i ) for any i except INT_MIN. What is
                the answer for INT_MIN? or for any i?

                Thanks,

                viza

                Comment

                • viza

                  #9
                  Re: (unsigned)-INT_MIN

                  Hi

                  On Mon, 21 Jul 2008 10:02:32 +0000, viza wrote:
                  What I would like is a constant expression of unsigned type for the
                  absolute value of an constant negative expression of the corresponding
                  signed type.
                  Thanks for the answers so far, but none of them help
                  I could use a conditional operation based on vippstars second post, but
                  is there a neater way?

                  viza

                  Comment

                  • Old Wolf

                    #10
                    Re: (unsigned)-INT_MIN

                    On Jul 21, 9:51 pm, vipps...@gmail. com wrote:
                    On Jul 21, 12:38 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
                    Not sure what 'vipps' was on about..
                    >
                    Which one of my two (without counting this one) messages are you
                    refering to?
                    I was referring to your first post on this thread
                    (and I posted before you posted your correction).
                    Signed values can be converted to unsigned ;
                    (unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
                    which is INT_MAX on the normal machine.
                    >
                    Wrong.
                    Assuming i is -N with N >= 0, (unsigned)i is UINT_MAX - N + 1.
                    So (unsigned)INT_M IN would be UINT_MAX + INT_MIN + 1.
                    Knew I should have checked that more before
                    posting .. the baby was crying though :)

                    Comment

                    • Old Wolf

                      #11
                      Re: (unsigned)-INT_MIN

                      On Jul 21, 10:02 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
                      wrote:
                      What I would like is a constant expression of unsigned type for the
                      absolute value of an constant negative expression of the corresponding
                      signed type.
                      -(unsigned)i

                      Assuming i is negative, then:
                      (unsigned)i is UINT_MAX + 1 - abs(i)
                      so the negative of that is
                      UINT_MAX + 1 - (UINT_MAX + 1 - abs(i))
                      which is abs(i).

                      Comment

                      • pete

                        #12
                        Re: (unsigned)-INT_MIN

                        viza wrote:
                        Hi
                        >
                        On Mon, 21 Jul 2008 08:53:51 +0000, viza wrote:
                        >
                        >int i= INT_MIN;
                        >unsigned int u= -i;
                        >>
                        >Is u guaranteed to have the absolute value of INT_MIN?
                        >
                        >If long is longer than int, I can do -(long)i, but what if we are
                        >talking about the longest integer type available?
                        >
                        Thanks for the answers so far, but none of them help. Perhaps I can
                        explain it a bit better:
                        >
                        What I would like is a constant expression of unsigned type for the
                        absolute value of an constant negative expression of the corresponding
                        signed type.
                        >
                        Ie:
                        >
                        1) I know i is negative
                        2) I want (unsigned)abs(i )
                        >
                        The answer simply: ( (unsigned)-i ) for any i except INT_MIN. What is
                        the answer for INT_MIN? or for any i?
                        There is no integer type which is guaranteed
                        to be able to represent the absolute value of INT_MIN.

                        --
                        pete

                        Comment

                        • vippstar@gmail.com

                          #13
                          Re: (unsigned)-INT_MIN

                          On Jul 21, 5:56 pm, pete <pfil...@mindsp ring.comwrote:
                          <snip>
                          There is no integer type which is guaranteed
                          to be able to represent the absolute value of INT_MIN.
                          That's incorrect. Do you mean no signed integer type? If so, that is
                          correct.
                          unsigned int, unsigned long int, unsigned long long int, all can
                          represent the absolute value of INT_MIN.

                          Comment

                          • Keith Thompson

                            #14
                            Re: (unsigned)-INT_MIN

                            vippstar@gmail. com writes:
                            On Jul 21, 5:56 pm, pete <pfil...@mindsp ring.comwrote:
                            <snip>
                            >There is no integer type which is guaranteed
                            >to be able to represent the absolute value of INT_MIN.
                            >
                            That's incorrect. Do you mean no signed integer type? If so, that is
                            correct.
                            unsigned int, unsigned long int, unsigned long long int, all can
                            represent the absolute value of INT_MIN.
                            That's true on most systems, but it's not guaranteed by the standard.

                            A conforming system could have padding bits in unsigned int, so that
                            UINT_MAX == INT_MAX. And unsigned long int and unsigned long long int
                            could both have the same size and representation as unsigned int
                            (assuming UINT_MAX >= 2**64-1).

                            Such a system is unlikely to exist in real life. int and unsigned int
                            would have to be at least 65 bits (33 bits if you'll settle for C90
                            conformance).

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • pete

                              #15
                              Re: (unsigned)-INT_MIN

                              vippstar@gmail. com wrote:
                              On Jul 21, 5:56 pm, pete <pfil...@mindsp ring.comwrote:
                              <snip>
                              >There is no integer type which is guaranteed
                              >to be able to represent the absolute value of INT_MIN.
                              >
                              That's incorrect. Do you mean no signed integer type? If so, that is
                              correct.
                              unsigned int, unsigned long int, unsigned long long int, all can
                              represent the absolute value of INT_MIN.
                              No.
                              ULONG_MAX can be 4294967295
                              while INT_MIN can be (-4294967296)
                              on the same implementation.

                              --
                              pete

                              Comment

                              Working...