# C++ Negative value assigning to unsigned int type

An ‘unsigned int’ type does not support negative integer value.In this post we will see what is the output if we assign a negative number to ‘unsigned int’ type variable.

Link : Difference between signed and unsigned int type

Consider the code example given below.

#include <iostream> using namespace std ; int main( ) { unsigned int ui= -134 ; ///ui is assigned negative value cout << “ui=” << ui << endl ; cin.get( ) ; return 0 ; }

Output,

4294967162

Which is a +ve value and it should be because unsigned int type does not support negative value.But the question remains, **how is 4294967162 obtained from -136**?.

To know how **ui( -134)** gives 4294967162 you need to understand the concept of **bit wise complement** and **bit wise addition**.When those two methods are implemented one after the other it is also known as **2’s complement**.So in other words to understand how unsigned variable converts the negative value assigned to positive value we need to know the 2’s complement as the compiler uses this method for the process.You can find the detail explanation of 2’s complement in Wikipedia however,a brief explanation is given below.

To perform 2’s complement first of all,we need the binary format of the absolute value of the negative value,in this case the absolute value of -134 is 134 and the binary format of 134 is 00000000 00000000 00 000000 10000110 .Now perform the two steps given below.

**Step i:** Do bit wise complement of 134.By bit wise complement we mean exchanging the bits so 0 becomes 1 and 1 becomes 0.So,the bit wise complement of 134 is 11111111 11111111 11111111 01111001.

**Step ii:** Add 1 to the bit format obtained in step i .To do bit wise addition you need to just remember this-when 1 and 1 are added it becomes 10(2) and so 0 becomes the resultant bit while 1 becomes a carry and the answer is 10 and when 1 and 0 are added the value is 1 so the answer is 1.The picture below shows the addition of 1 with 1 and 1 with 0.

When the bit format 11111111 11111111 11111111 01111010 is converted to integer value or decimal type,it gives 4294967162. Hence the output.So,whenever we assign negative value to unsigned int variable,2’s complement is always performed on the assigned absolute value and the integer value of the resultant bit format is the unsigned int variable value.

In the next topic(below) we will see an interesting case of signed and unsigned int type.

#### Why (signed int)~5 == -6 ?

Let’s look at the program below.

#include <iostream> using namespace std; int main( ) { signed int si=~5 ; unsigned int us=~5 ; cout<< si << endl ; cout<< us << endl ; cin.get() ; return 0 ; }

The output is,

-6

4294967290

We get -6 when the variable is signed int type and 4294967290

when the variable is unsigned int type.Let’s first discuss the -6 case.

5 in bit format is 00000000 00000000 00000000 00000101 and ~5 is 11111111 11111111 11111111 11111010.From this bit format we can deduce the sign of the resultant value(sign is -ve if the left most bit is 1 +ve if left most bit is 0),since the left most bit of ~5 is 1 the resultant value of si must have a -ve sign.Now we know the sign of the resultant value of si but we do not know the absolute value of si yet(note converting 11111111 11111111 11111111 11111010 to integer will not give you the actual value of si).So to find the actual value of si we will perform a reverse of **2’s complement** in the bit format of ~5 i.e.11111111 11111111 11111111 11111010.

**Step 1**:Subtract 1 from 11111111 11111111 11111111 11111010.After subtracting the resulting binary digit is 11111111 11111111 11111111 11111001.I will not show you here how binary subtraction is performed ,Wikipedia has a lengthy description on this topic.

**Step 2**:Do bit wise complement on the binary digit obtained in step 1.So the bit wise complement of 11111111 11111111 11111111 11111001 is 00000000 00000000 00000000 00000110.This bit format is same as the bit format of the integer value 6.Hence,the value **6** is an **absolute value of si** and since si is signed type,after adding the sign;which is -ve,the resultant value of si is -6.

For the second output the variable is **unsigned** type so,the binary format of ~5(11111111 11111111 11111111 11111010) is converted directly to decimal type giving the value 4294967290.

#### Conclusion

The case discus here –negative value assigned to unsigned int type ,is rather a small case in the whole of C++ but it can become very troublesome sometimes.Unintentionally assigning a negative value to unsigned variable and getting an unexpected output can be a real pain in the ass and more so if the origin of the error is not known.I hope this post will provide you some assistance in taking away those pain and make C++ a fun language.If you need more assistance or have anything to say comment below.Happy programming!