3

What is the bit representation of unsigned int x =-1; Can we assign unsigned int with a negative integer?

#include<stdio.h> 
int main(){
    unsigned int x = -1; 
    int y = ~0; 
    if(x == y) 
        printf("same"); 
    else
        printf("not same"); 
    return 0;
}

output :

same

and how is it possible, x being unsigned

#include<stdio.h> 

int  main()     
{

unsigned int x = -4; 
if (x == -4)
   printf("true");
else 
   printf("FALSE");
}

output:

true

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
pranav prashant
  • 307
  • 1
  • 3
  • 13
  • unsigned can not be negative :) when you wrote the word "unsigned" you do not think it means? – Ivan Ivanovich Sep 30 '14 at 18:09
  • The answer of your question is already in your example. – dari Sep 30 '14 at 18:10
  • i mean, how -1 is represented in binary form. What is the logic that -1 and 1 are the same – pranav prashant Sep 30 '14 at 18:12
  • 2
    -1 is always UMAX see [this](http://stackoverflow.com/a/22801135/1708801) and [this](http://stackoverflow.com/questions/22047158/why-is-this-happening-with-the-sizeof-operator-when-comparing-with-a-negative-nu/22047204#22047204) – Shafik Yaghmour Sep 30 '14 at 18:13
  • 4
    @larsmans: The language requires `(unsigned)(-1)` to produce `UNIT_MAX` on all platforms, regardless of whether they use 2's coimplement or not. – AnT stands with Russia Sep 30 '14 at 18:14
  • `~0`, on the other hand, only yields -1 if the implementation uses 2's complement (and for sake of completeness, `~0` may be a trap representation (otherwise, it's negative zero) for 1's complement). – mafso Sep 30 '14 at 20:14

3 Answers3

6

When an unsigned int value is compared to int value, the int value is implicitly converted to unsigned int type. The result of that conversion is congruent to the original value modulo 2N, where N is the number of value-forming bits in unsigned int. This modulo equals to UINT_MAX + 1.

For this reason initialization

unsigned int x = -1;

initializes x with some unsigned value congruent to -1 modulo UINT_MAX + 1. Incidentally, this is nothing else than UINT_MAX. This value has 1 in each value-forming bit of unsigned int object. It works that way with any unsigned type.

Expression ~0 is evaluated in the domain of signed int type, and then y is implicitly converted to unsigned int in x == y comparison. Apparently, on your platform the conversion produces the same unsigned int value with all value-forming bits set to 1. Hence the equality.

Initialization

unsigned int x = -4;

initializes x with some unsigned value congruent to -4 modulo UINT_MAX + 1. In comparison x == -4 the right-hand side is converted to unsigned type by the very same rules. Hence the equality.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    Exactly. “When a value with integer type is converted to […] unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.” (C11, §6.3.1.3). – Stephen Canon Sep 30 '14 at 18:30
2

and how is it possible, x being unsigned

This is the "usual arithmetic conversions" -- the operands on either side of the == have different types, so they must be converted to the same types first. The rule for types of int or larger when the types differ only by signedness is that unsigned wins, so this converts -4 into the unsigned equivalent value, just as it did when you assigned -4 into an unsigned integer.

For more information, see How do promotion rules work when the signedness on either side of a binary operator differ?

Community
  • 1
  • 1
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0

in int negative integers are stored in 2's complement.

suppose we have a data type say of 4 bits.

the first bit 0 represents that the integer is positive otherwise negative, so its max value is 0111 i.e 7

so 1 can be written as 0001

and for -1, it has to be written in its 2's complement, since it is negative ;

-1 = ~(0001) +1 = (1110) + 1 = 1111;

so making this data type unsigned reads -1 as 15.

pranav prashant
  • 307
  • 1
  • 3
  • 13