Thread: unsingned int problem

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    15

    Exclamation unsingned int problem

    I was going through the following statement in a c book:

    "interpreting -1 as unsigned int leads to a big +ve number"

    -1 is already unsigned …. then what does the above statement mean ??

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    unsigned integral types can only represent values between 0 and their maximum (which is always positive).

    -1 is NOT unsigned. It is a value of type int (signed). When that value is converted from int to a unsigned integer type, modulo arithmetic is used, so the value obtained is the maximum value that unsigned integer type can represent. That is the way that the standard specifies unsigned integral types work (because, on most real hardware, that's how machine instructions work with unsigned values)
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by grumpy View Post
    unsigned integral types can only represent values between 0 and their maximum (which is always positive).

    -1 is NOT unsigned. It is a value of type int (signed). When that value is converted from int to a unsigned integer type, modulo arithmetic is used, so the value obtained is the maximum value that unsigned integer type can represent. That is the way that the standard specifies unsigned integral types work (because, on most real hardware, that's how machine instructions work with unsigned values)
    On most (If not all) systems, signed integer types are stored using two's compliment arithmetic. At least on gcc (my compiler) when you copy a signed integer to an unsigned integer of the same size, the bit pattern is copied.

    For example if a signed short value, -3, is copied to an unsigned short, the unsigned short value is 65533.

    The bit pattern (Little Endian on my system) of the signed and unsigned values after the assignment are the same: 11111101 11111111.

    Bottom line, don't mix negative signed and unsigned values and variables.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rstanley View Post
    On most (If not all) systems, signed integer types are stored using two's compliment arithmetic.
    There are some older systems that used ones-complement.

    Yes, twos-complement is most common at present. However, the C standard deliberately avoids making it necessary for compilers to do that. If, for some reason, hardware evolution drives toward some other representation, it would be imprudent for a compiler to use something different.

    Quote Originally Posted by rstanley View Post
    For example if a signed short value, -3, is copied to an unsigned short, the unsigned short value is 65533.
    Assuming an unsigned short with maximum value 65535, that is consistent with modulo arithmetic that I described. Whether or not the representation is twos-complement.

    Quote Originally Posted by rstanley View Post
    The bit pattern (Little Endian on my system) of the signed and unsigned values after the assignment are the same: 11111101 11111111.
    Yes, but there is no requirement that the bit patterns be the same. The requirement in the C standard is in terms of the relationship between values. Not bit patterns.

    Quote Originally Posted by rstanley View Post
    Bottom line, don't mix negative signed and unsigned values and variables.
    The more general version of that rule is to understand your code, and how it behaves.

    The "don't mix them" rule is unfortunately the rule for a significant majority of programmers - who can't (more or less understandable) or don't bother (a sign of laziness) to understand what is involved when converting a negative value to a unsigned representation.
    Last edited by grumpy; 10-30-2014 at 04:24 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  4. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM