signed/unsigned int

Discussion in 'C Programming' started by Aire, Jan 22, 2004.

  1. Aire

    Aire Guest

    1. If a function defined as:

    void test_function(unsigned int a)
    {
    }

    Is "test_function(-256);" going to cause undefined behavior?

    2. What's "a negative signed value wrapping"?

    3. Is bit-shifting on a signed int undefined behavior? Why?

    Thanks!
     
    Aire, Jan 22, 2004
    #1
    1. Advertising

  2. Aire

    Eric Sosman Guest

    Aire wrote:
    >
    > 1. If a function defined as:
    >
    > void test_function(unsigned int a)
    > {
    > }
    >
    > Is "test_function(-256);" going to cause undefined behavior?


    No.

    > 2. What's "a negative signed value wrapping"?


    An informal description of one common consequence
    of the result of trying to compute or store a value
    smaller than the smallest value representable in the
    given signed integer type.

    > 3. Is bit-shifting on a signed int undefined behavior? Why?


    Yes under some circumstances; because the Standard
    doesn't define it.


    --
     
    Eric Sosman, Jan 22, 2004
    #2
    1. Advertising

  3. Aire

    Jack Klein Guest

    On Thu, 22 Jan 2004 17:36:02 GMT, "Aire" <> wrote in
    comp.lang.c:

    > 1. If a function defined as:
    >
    > void test_function(unsigned int a)
    > {
    > }
    >
    > Is "test_function(-256);" going to cause undefined behavior?


    No, but it might cause unexpected errors in the function if such a
    value is not valid and the function does not check for it. The value
    -256 will be converted, as if by assignment, to an unsigned int to
    pass to the function.

    Assigning a negative value to a unsigned integer type can never
    overflow and the result is always well-defined. In this case, the
    unsigned int value passed will be UINT_MAX + 1 - 256. That's 65280
    for typical 16 bit ints, or 4294967040 for typical 32 bit ints, but
    other values are possible.

    > 2. What's "a negative signed value wrapping"?


    No such thing in C. Overflowing or underflowing a signed integer type
    produces undefined behavior.

    > 3. Is bit-shifting on a signed int undefined behavior? Why?


    It is possible that some bit patterns, when interpreted as a signed
    integer type, do not compose a valid value for that type. This is
    called a trap representation, and dealing with such produces undefined
    behavior. When shifting a signed integer, it is possible that a trap
    representation may be produced.

    Aside from that, it is implementation-defined whether right shifting a
    signed integer type preserves the sign bit.

    > Thanks!


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 23, 2004
    #3
    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. Siemel Naran

    convert signed int to unsigned int

    Siemel Naran, Nov 28, 2004, in forum: C++
    Replies:
    3
    Views:
    34,143
    Ioannis Vranos
    Nov 29, 2004
  2. G Fernandes
    Replies:
    2
    Views:
    502
    Lawrence Kirby
    Feb 16, 2005
  3. Alex
    Replies:
    3
    Views:
    650
    Michael Mair
    Apr 26, 2006
  4. Fore
    Replies:
    29
    Views:
    15,394
    Rashad
    Sep 21, 2008
  5. pozz
    Replies:
    12
    Views:
    760
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page