Basic doubt in datatypes of signed and unsigned

Discussion in 'C Programming' started by hari, Dec 21, 2008.

  1. hari

    hari Guest

    Hi,

    #include <stdio.h>
    #include<conio.h>
    int main()
    {
    int x= -1;
    unsigned int y = x ;
    unsigned int j = y;

    printf("%x %x",x,y)
    return 0;
    }

    When I execute this,I' m getting the output as 0xfffffff
    0xfffffff.My expectation is , y is unsigned int,so on assigning a
    signed variable with value -1.It should go to 0.

    Please correct me if I' m wrong .

    Regards
    Hari
    hari, Dec 21, 2008
    #1
    1. Advertising

  2. On Dec 21, 3:09 pm, hari <> wrote:

    > #include<conio.h>



    Just so you know, conio.h is not a part of the C Standard. For this
    particular program, you don't even need conio.h.


    > int main()
    > {
    >                 int x= -1;
    >                 unsigned int y = x ;



    When you convert a signed integer to an unsigned integer, the value
    you're left with is as follows:

    (WHATEVER_UNSIGNED_INT_TYPE_MAX + 1) + (-the_negative_value)

    So in your case, it's:

    (UINT_MAX + 1) - (1)

    And so that becomes:

    UINT_MAX
    Tomás Ó hÉilidhe, Dec 21, 2008
    #2
    1. Advertising

  3. hari

    James Kuyper Guest

    hari wrote:
    > Hi,
    >
    > #include <stdio.h>
    > #include<conio.h>
    > int main()
    > {
    > int x= -1;
    > unsigned int y = x ;
    > unsigned int j = y;
    >
    > printf("%x %x",x,y)
    > return 0;
    > }
    >
    > When I execute this,I' m getting the output as 0xfffffff


    Are you sure that wasn't 0xffffffff? A value of 0xfffffff is technically
    possible, but extremely unlikely unless you're using a very unusual machine.

    > 0xfffffff.My expectation is , y is unsigned int,so on assigning a
    > signed variable with value -1.It should go to 0.
    >
    > Please correct me if I' m wrong .


    If an integer value is outside the valid range for an unsigned integer
    type, when you convert that value to that type, the result is determined
    using modulus arithmetic. This means that you first determine the number
    that is 1 larger than the largest value that can be stored in in the
    unsigned type; in your case, that would be UINT_MAX+1, which is probably
    0x100000000 (assuming that I was right about you mis-counting the number
    of 'f's). Then you repeatedly either add or subtract this number from
    the value, until you get a result that is in range for the unsigned
    type. In this case, a single addition of UINT_MAX+1 to -1 gives you a
    value of UINT_MAX, which is within range, so that's the result you
    should be getting.
    James Kuyper, Dec 21, 2008
    #3
  4. hari

    Phil Carmody Guest

    James Kuyper <> writes:
    > hari wrote:
    >> Hi,
    >>
    >> #include <stdio.h>
    >> #include<conio.h>


    *ahem*

    >> int main()
    >> {
    >> int x= -1;
    >> unsigned int y = x ;
    >> unsigned int j = y;
    >>
    >> printf("%x %x",x,y)
    >> return 0;
    >> }
    >>
    >> When I execute this,I' m getting the output as 0xfffffff

    >
    > Are you sure that wasn't 0xffffffff?


    Odd that you should expect one character more, I would have expected
    one character fewer.

    Phil
    --
    I tried the Vista speech recognition by running the tutorial. I was
    amazed, it was awesome, recognised every word I said. Then I said the
    wrong word ... and it typed the right one. It was actually just
    detecting a sound and printing the expected word! -- pbhj on /.
    Phil Carmody, Dec 23, 2008
    #4
  5. hari

    James Kuyper Guest

    Phil Carmody wrote:
    > James Kuyper <> writes:
    >> hari wrote:
    >>> Hi,
    >>>
    >>> #include <stdio.h>
    >>> #include<conio.h>

    >
    > *ahem*
    >
    >>> int main()
    >>> {
    >>> int x= -1;
    >>> unsigned int y = x ;
    >>> unsigned int j = y;
    >>>
    >>> printf("%x %x",x,y)
    >>> return 0;
    >>> }
    >>>
    >>> When I execute this,I' m getting the output as 0xfffffff

    >> Are you sure that wasn't 0xffffffff?

    >
    > Odd that you should expect one character more, I would have expected
    > one character fewer.


    Yes, that leading 0 shouldn't appear in his output, and I shouldn't have
    included it in my proposed alternative.
    James Kuyper, Dec 23, 2008
    #5
    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. me2

    hex and unsigned and signed decimal

    me2, Dec 28, 2006, in forum: C Programming
    Replies:
    14
    Views:
    1,575
    Keith Thompson
    Jan 2, 2007
  2. hari

    signed and unsigned Sign bit doubt

    hari, Dec 22, 2008, in forum: C Programming
    Replies:
    4
    Views:
    1,946
    CBFalconer
    Dec 23, 2008
  3. pozz
    Replies:
    12
    Views:
    720
    Tim Rentsch
    Mar 20, 2011
  4. Peter Otten
    Replies:
    2
    Views:
    103
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    106
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page