Examples of Unsigned int
For Complete YouTube Video: Click Here
In this class, we will try to understand Examples of Unsigned int.
We have already discussed the concepts of qualifiers in our previous class.
Note: To understand this class, we need a bit of knowledge of the number system in Digital logic Design.
Table of Contents
Examples of Unsigned int
To understand this concept, we will consider the programming example below.
In the above program, we have declared two unsigned integers i, j and assigned zero to both the values.
The value of ‘i’ has been incremented by 1, and the value of j has been decremented by 1.
The new value of i is now 1, and the value of j is -1.
In the printf statements, we have used the ‘%u’ format specifier for an unsigned integer.
The ‘%u’ format specifier will use all the bits for magnitude and will print 15 as the output, but –j is -1.
How does this happen?
To understand this consider a 4-bit computer.
Consider the j = 0.
Zero in 4-bit representation is 0000.
–j is -1.
How is a negative number represented?
Negative numbers are represented in 2’s complement format.
0001 is the binary representation of decimal number 1.
The 2’s complement representation of 0001 is 1111. A clear explanation of 2’s complement is here.
For signed numbers, the most significant bit is considered for the sign, and the remaining bits are considered for the magnitude of a number as shown below.
If the most significant bit is 1, the number is negative. If the most significant number is 0, it is a positive number.
Consider 1111 is a signed number, the most significant bit 1, which is a negative number.
As it is a negative number to get the magnitude of the remaining bits 111, we have to do the 2’s compliment again.
The 2’s complment of 111 is 001.
001 is the binary representation 1.
So the value of 1111 is -1.
The above is the case for the signed numbers.
In the case of unsigned format specifiers in 1111, all bits are considered the magnitude of the number.
The decimal value of 1111 is 15.
So, the output of the second printf statement is 15.
For a more clarified explanation, please click the youtube link above.