# how unsigned integer store negative numbers

Discussion in 'C' started by phanihup, Jun 29, 2012.

1. ### phanihupNew Member

Joined:
Jun 29, 2012
Messages:
1
0
Trophy Points:
0
hi! my doubt is how unsigned integers store negative numbers?
if i write ,int i=-9 then how -9 is stored in i(8 bit).
if we again print the value in i we won't get -9.but we get some larger number.why it so?

2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
They don't. Unsigned integers are unsigned, by definition, and therefore always positive.
If you try then what will happen is that the bit pattern of -9 will be stored in the integer and interpreted as a positive number. Negative numbers are stored in two's complement, which is like 1's complement (where you flip all the bits) but then you add 1.

00001001=9
11110110=1's complement of 9
11110111=2's complement of 9

So if this is put into unsigned 8-bit storage, then the result will be interpreted as 247.

3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
By the way, int i=-9 defines a signed integer by default (unless your compiler has a flag that changes that default behaviour). For unsigned integers you need to specify the unsigned keyword, i.e.
Code:
```unsigned int i=-9;
```