~ operator returns signed value

Discussion in 'C Programming' started by RezaRob, Sep 23, 2007.

  1. RezaRob

    RezaRob Guest

    gcc is clearly returning -1 when I do this
    ~ (unsigned short) 0

    which is not the case if "unsigned int" is used instead of "unsigned
    short".

    What does the C standard say about that? (reference to the standard
    is appreciated.)

    Thanks.

    Reza.
     
    RezaRob, Sep 23, 2007
    #1
    1. Advertising

  2. RezaRob

    pete Guest

    RezaRob wrote:
    >
    > gcc is clearly returning -1 when I do this
    > ~ (unsigned short) 0
    >
    > which is not the case if "unsigned int" is used instead of "unsigned
    > short".
    >
    > What does the C standard say about that? (reference to the standard
    > is appreciated.)


    If INT_MAX is greater than or equal to USHRT_MAX,
    then (~ (unsigned short) 0) means the same thing as
    (~ (int)(unsigned short) 0).



    ISO/IEC 9899: 1990
    6.2.1 Arithmetic operands
    6.2.1.1 Characters and integers
    A char, a short int, or an int bit-field,
    or their signed or unsigned varieties,
    or an enumeration type, may be used in an expression
    wherever an int or unsigned int may be used.
    If an int can represent all values of the original type,
    the value is converted to an int; otherwise,
    it is converted to an unsigned int.
    These are called the integral romotions.
    All other arithmetic types are unchanged by the integral promotions.

    --
    pete
     
    pete, Sep 23, 2007
    #2
    1. Advertising

  3. RezaRob

    pete Guest

    pete wrote:
    >
    > RezaRob wrote:
    > >
    > > gcc is clearly returning -1 when I do this
    > > ~ (unsigned short) 0
    > >
    > > which is not the case if "unsigned int" is used instead of "unsigned
    > > short".
    > >
    > > What does the C standard say about that? (reference to the standard
    > > is appreciated.)

    >
    > If INT_MAX is greater than or equal to USHRT_MAX,
    > then (~ (unsigned short) 0) means the same thing as
    > (~ (int)(unsigned short) 0).
    >
    > ISO/IEC 9899: 1990
    > 6.2.1 Arithmetic operands
    > 6.2.1.1 Characters and integers
    > A char, a short int, or an int bit-field,
    > or their signed or unsigned varieties,
    > or an enumeration type, may be used in an expression
    > wherever an int or unsigned int may be used.
    > If an int can represent all values of the original type,
    > the value is converted to an int; otherwise,
    > it is converted to an unsigned int.
    > These are called the integral romotions.


    That should be "These are called the integral promotions."

    > All other arithmetic types are unchanged by the integral promotions.


    --
    pete
     
    pete, Sep 23, 2007
    #3
  4. RezaRob

    RezaRob Guest

    On Sep 23, 3:47 pm, pete <> wrote:
    > If INT_MAX is greater than or equal to USHRT_MAX,
    > then (~ (unsigned short) 0) means the same thing as
    > (~ (int)(unsigned short) 0).
    >
    > ISO/IEC 9899: 1990
    > 6.2.1 Arithmetic operands
    > 6.2.1.1 Characters and integers
    > [...]


    Pete, I really appreciate this.

    Reza.
     
    RezaRob, Sep 24, 2007
    #4
  5. RezaRob

    pete Guest

    RezaRob wrote:
    >
    > On Sep 23, 3:47 pm, pete <> wrote:
    > > If INT_MAX is greater than or equal to USHRT_MAX,
    > > then (~ (unsigned short) 0) means the same thing as
    > > (~ (int)(unsigned short) 0).
    > >
    > > ISO/IEC 9899: 1990
    > > 6.2.1 Arithmetic operands
    > > 6.2.1.1 Characters and integers
    > > [...]

    >
    > Pete, I really appreciate this.


    The following three expressions have the same type and value:
    (USHRT_MAX)
    ((unsigned short) ~0u)
    ((unsigned short) -1)

    --
    pete
     
    pete, Sep 24, 2007
    #5
  6. RezaRob

    Martin Wells Guest


    > The following three expressions have the same type and value:
    > (USHRT_MAX)
    > ((unsigned short) ~0u)
    > ((unsigned short) -1)


    Are you sure about the middle one?

    1: 0u is of type unsigned int.

    2: ~0u is of type unsigned int and is equal to UINT_MAX.

    3: (unsigned short)~0u will be UINT_MAX % USHRT_MAX, or at least I
    think it will, so it can't be USHRT_MAX.

    but of course I'm open to correction!

    Martin
     
    Martin Wells, Sep 25, 2007
    #6
  7. RezaRob

    user923005 Guest

    On Sep 24, 4:36 pm, Martin Wells <> wrote:
    > > The following three expressions have the same type and value:
    > > (USHRT_MAX)
    > > ((unsigned short) ~0u)
    > > ((unsigned short) -1)

    >
    > Are you sure about the middle one?


    Sure, there's a cast in it to unsigned short.
    ;-)
    [snip]
     
    user923005, Sep 25, 2007
    #7
  8. Martin Wells said:

    >
    >> The following three expressions have the same type and value:
    >> (USHRT_MAX)
    >> ((unsigned short) ~0u)
    >> ((unsigned short) -1)

    >
    > Are you sure about the middle one?
    >
    > 1: 0u is of type unsigned int.
    >
    > 2: ~0u is of type unsigned int and is equal to UINT_MAX.
    >
    > 3: (unsigned short)~0u will be UINT_MAX % USHRT_MAX, or at least I
    > think it will, so it can't be USHRT_MAX.
    >
    > but of course I'm open to correction!


    Oh good. :) Re your Point 3, a single counter-example should suffice.
    Consider a system with UINT_MAX == USHRT_MAX (e.g. typical 1990s MS-DOS
    systems, or an SIL64 system such as at least one Cray. Clearly, on such a
    system, UINT_MAX % USHRT_MAX will be 0 (for the same reason that x % x is
    0), and yet (unsigned short)~0u will be equivalent to ~0u, which is most
    emphatically NOT 0.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 25, 2007
    #8
  9. RezaRob

    Martin Wells Guest

    Richard:

    > Oh good. :) Re your Point 3, a single counter-example should suffice.
    > Consider a system with UINT_MAX == USHRT_MAX (e.g. typical 1990s MS-DOS
    > systems, or an SIL64 system such as at least one Cray. Clearly, on such a
    > system, UINT_MAX % USHRT_MAX will be 0 (for the same reason that x % x is
    > 0), and yet (unsigned short)~0u will be equivalent to ~0u, which is most
    > emphatically NOT 0.



    Sorry I've red that a few times but I still don't understand what
    you're saying.

    Was pete right or wrong?

    Martin
     
    Martin Wells, Sep 25, 2007
    #9
  10. Martin Wells said:

    > Richard:
    >
    >> Oh good. :) Re your Point 3, a single counter-example should suffice.
    >> Consider a system with UINT_MAX == USHRT_MAX (e.g. typical 1990s MS-DOS
    >> systems, or an SIL64 system such as at least one Cray. Clearly, on such
    >> a system, UINT_MAX % USHRT_MAX will be 0 (for the same reason that x % x
    >> is 0), and yet (unsigned short)~0u will be equivalent to ~0u, which is
    >> most emphatically NOT 0.

    >
    >
    > Sorry I've red that a few times but I still don't understand what
    > you're saying.


    You claimed that "(unsigned short)~0u will be UINT_MAX % USHRT_MAX". I
    disputed that claim, by pointing out a counter-example.

    > Was pete right or wrong?


    Presumably. :)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 25, 2007
    #10
  11. RezaRob

    Martin Wells Guest

    Richard:

    > > Was pete right or wrong?

    >
    > Presumably. :)



    I'm still lost. I'll try think it through using MS-DOS as an example:

    1: 0u == the number 0
    2: ~0u == UINT_MAX == the number 65535
    3: (unsigned short)~0u == the number USHRT_MAX % 65535 == the
    number 65535 % 65535 == the number 0.

    This leads me to believe that pete was wrong... unless I'm missing
    something.

    Martin
     
    Martin Wells, Sep 25, 2007
    #11
  12. Martin Wells said:

    > Richard:
    >
    >> > Was pete right or wrong?

    >>
    >> Presumably. :)

    >
    >
    > I'm still lost.


    <snip>

    > 3: (unsigned short)~0u == the number USHRT_MAX % 65535 == the
    > number 65535 % 65535 == the number 0.


    Please explain why you think this is the case. I can see no justification
    for your assumption that (unsigned short)~0u is equal to USHRT_MAX % 65535
    (and yes, I know we're talking 16-bit systems. You originally wrote that
    it's equal to UINT_MAX % USHRT_MAX, which is also incorrect.

    You might reasonably claim that, on a 16-bit system, (unsigned short)~0u is
    equal to UINT_MAX % (USHRT_MAX + 1), but that would just be equal to
    UINT_MAX (and indeed USHRT_MAX).

    > This leads me to believe that pete was wrong... unless I'm missing
    > something.


    You appear to be missing a + 1. :)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 25, 2007
    #12
  13. RezaRob

    pete Guest

    Martin Wells wrote:
    >
    > > The following three expressions have the same type and value:
    > > (USHRT_MAX)
    > > ((unsigned short) ~0u)
    > > ((unsigned short) -1)

    >
    > Are you sure about the middle one?
    >
    > 1: 0u is of type unsigned int.
    >
    > 2: ~0u is of type unsigned int and is equal to UINT_MAX.
    >
    > 3: (unsigned short)~0u will be UINT_MAX % USHRT_MAX, or at least I
    > think it will, so it can't be USHRT_MAX.
    >
    > but of course I'm open to correction!


    This part of your post: "UINT_MAX % USHRT_MAX"
    is wrong.

    --
    pete
     
    pete, Sep 25, 2007
    #13
  14. RezaRob

    Martin Wells Guest

    pete:

    > The following three expressions have the same type and value:
    > (USHRT_MAX)
    > ((unsigned short) ~0u)
    > ((unsigned short) -1)



    I'm gonna give the middle one another go. . .

    Before I begin though, I'm gonna pretend I'm working with the
    following machine:

    CHAR_BIT == 11
    sizeof(short) == 2 (all 22 bits are value bits)
    sizeof(int) == 3 (31 bits are value bits, 1 is padding)

    Therefore, we have:

    UINT_MAX = 2147483647
    USHRT_MAX = 4194303

    1: 0u

    Expression Type: unsigned int
    Expression Value: 0

    2: ~0u

    Expression Type: unsigned int
    Expression Value: 2147483647 (i.e. UINT_MAX)

    3: (unsigned short)~0u

    == UINT_MAX % (USHRT_MAX + 1)
    == 2147483647 % 4194304
    == 4194303

    Oh, so that worked out OK. I suppose what I was tryna get my head
    around from the start was how you could so blindy make the assumption

    Given: a is a positive integer
    b is a positive integer
    a is greater than or equal to b
    That:

    ( pow(2,a) - 1 ) % pow(2,b)

    is equal to:

    pow(2,b) - 1

    Martin
     
    Martin Wells, Sep 26, 2007
    #14
  15. RezaRob

    Martin Wells Guest


    > sizeof(int) == 3 (31 bits are value bits, 1 is padding)



    Shuda written "2 are padding" instead of "1 is".

    Martin
     
    Martin Wells, Sep 26, 2007
    #15
  16. RezaRob

    pete Guest

    Martin Wells wrote:
    >
    > pete:
    >
    > > The following three expressions have the same type and value:
    > > (USHRT_MAX)
    > > ((unsigned short) ~0u)
    > > ((unsigned short) -1)

    >
    > I'm gonna give the middle one another go. . .
    >
    > Before I begin though, I'm gonna pretend I'm working with the
    > following machine:
    >
    > CHAR_BIT == 11
    > sizeof(short) == 2 (all 22 bits are value bits)
    > sizeof(int) == 3 (31 bits are value bits, 1 is padding)
    >
    > Therefore, we have:
    >
    > UINT_MAX = 2147483647
    > USHRT_MAX = 4194303
    >
    > 1: 0u
    >
    > Expression Type: unsigned int
    > Expression Value: 0
    >
    > 2: ~0u
    >
    > Expression Type: unsigned int
    > Expression Value: 2147483647 (i.e. UINT_MAX)
    >
    > 3: (unsigned short)~0u
    >
    > == UINT_MAX % (USHRT_MAX + 1)
    > == 2147483647 % 4194304
    > == 4194303
    >
    > Oh, so that worked out OK.


    Actually, I got one of those wrong.
    The type of USHRT_MAX isn't unsigned short.

    --
    pete
     
    pete, Sep 26, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vizlab
    Replies:
    3
    Views:
    4,273
    Michael Bar-Sinai
    Oct 17, 2007
  2. Kevin Goodsell
    Replies:
    30
    Views:
    841
    Dan Pop
    Oct 22, 2003
  3. kyrpa83
    Replies:
    1
    Views:
    657
    kyrpa83
    Oct 17, 2007
  4. ++imanshu
    Replies:
    7
    Views:
    499
    ++imanshu
    Aug 23, 2008
  5. Rob1bureau
    Replies:
    1
    Views:
    837
    joris
    Feb 27, 2010
Loading...

Share This Page