# difference between signed and unsigned integer with code examples

Introduction:
In programming, we often deal with numerical data. We use variables to store different numerical values. These numerical values can be whole numbers (integers) or decimal numbers (floating-point numbers), depending on the requirement. In a computer, the numerical data is stored in binary form, where the data is represented using bits. In this article, we will discuss the difference between signed and unsigned integers and code examples to understand them better.

Signed and Unsigned Integers:
An integer is a numerical data type that can represent whole numbers, both positive and negative. The range of integers that can be stored in a computer is limited by the number of bits that are used to represent the data. In general, a 32-bit integer can represent values ranging from -2,147,483,648 to 2,147,483,647.

The difference between signed and unsigned integers is that signed integers can represent both positive and negative values, whereas unsigned integers can only represent nonnegative values (i.e., zero and positive values).

In signed integers, the most significant bit (MSB) is used to represent the sign of the number. If the MSB is 0, the number is positive, and if the MSB is 1, the number is negative. For example, in a 32-bit signed integer, the value of -1 is represented as follows in binary:

-1 = 0b11111111 11111111 11111111 11111111

Here, the MSB is 1, indicating that the number is negative. The remaining 31 bits represent the magnitude (absolute value) of the number.

In unsigned integers, all bits are used to represent the magnitude of the number. For example, in a 32-bit unsigned integer, the value of 4294967295 (which is the maximum value that can be represented by a 32-bit unsigned integer) is represented as follows in binary:

4294967295 = 0b11111111 11111111 11111111 11111111

Here, all 32 bits represent the magnitude of the number since there is no need to represent the sign.

Code Examples:
Let us understand the difference between signed and unsigned integers with the help of some code examples in C++.

Example 1: Signed vs. Unsigned Integer Arithmetic

Consider the following code snippet:

int a = -1;
unsigned int b = 2;

int c = a + b;

cout << c << endl;

What do you think will be the output of this code?

The output is undefined because we are adding a signed integer (a) and an unsigned integer (b). When we do this, the C++ compiler implicitly converts the unsigned integer to a signed integer. In this case, the value of b is converted to a very large positive signed integer. When we add a and b, the result overflows and the value of c becomes undefined.

To fix this, we should always make sure that we are doing arithmetic operations on variables of the same data type (either both signed or both unsigned). The corrected code is as follows:

int a = -1;
int b = 2;

int c = a + b;

cout << c << endl;

Now, the output is 1, which is the correct result of adding -1 and 2.

Example 2: Bitwise Operations on Signed and Unsigned Integers

Consider the following code snippet:

int a = -1;
unsigned int b = 2;

int c = a << 1;
unsigned int d = b << 1;

cout << c << endl;
cout << d << endl;

What do you think will be the output of this code?

The output is -2 and 4. Here, we are performing bitwise left shift operations on a and b. In a left shift operation, we shift the binary representation of the number to the left by a given number of bits, filling the right side with zeros. For example, if we shift the binary number 0b00001010 (which is decimal 10) to the left by 2 bits, we get 0b00101000 (which is decimal 40).

In C++, both signed and unsigned integers support bitwise operations. However, the behavior of these operations is different for signed and unsigned integers. For example, the left shift operation on a signed integer is equivalent to multiplying the number by 2 raised to the power of the shift amount. Thus, when we left shift -1 by 1 bit, we essentially multiply it by 2, which gives us -2.

On the other hand, the left shift operation on an unsigned integer is equivalent to multiplying the number by 2 raised to the power of the shift amount, and the behavior is well-defined for all valid shift amounts. Thus, when we left shift 2 (which is represented as 0b00000010 in binary) by 1 bit, we get 4 (which is represented as 0b00000100 in binary).

Conclusion:
In this article, we discussed the difference between signed and unsigned integers. We saw that signed integers can represent both positive and negative values, whereas unsigned integers can only represent nonnegative values. We also saw some code examples in C++ to understand the difference between these two data types. It is important to use signed and unsigned integers carefully and make sure we are doing arithmetic operations on variables of the same data type.

Signed and Unsigned Integers:
An important thing to note about signed and unsigned integers is that their ranges and behavior during arithmetic operations can differ for different compilers and machines. For example, in some machines, a 32-bit signed integer can represent values ranging from -2147483648 to 2147483647, while in others, it can represent values ranging from -32768 to 32767. Similarly, the behavior of arithmetic operations on signed and unsigned integers can differ depending on the machine's hardware, operating system, and compiler.

It's also worth noting that using unsigned integers can be beneficial in certain situations. For example, when storing binary data or representing timestamps, it makes sense to use unsigned integers because negative values do not make sense in those contexts. Additionally, unsigned integers can represent larger positive values than their signed counterparts (since they don't need to store the sign bit) and can handle overflow errors more gracefully in certain situations.

Code Examples:
Here are a few more code examples to illustrate the behavior of signed and unsigned integers in different contexts:

Example 3: Implicit Type Conversion

Consider the following code snippet:

int a = -1;
unsigned int b = 1;

if (a < b) {
cout << "a is less than b" << endl;
}
else {
cout << "a is greater than or equal to b" << endl;
}

What do you think will be the output of this code?

The output is "a is greater than or equal to b". Here, we are comparing a signed integer (a) with an unsigned integer (b). When we do this, the C++ compiler implicitly converts the signed integer to an unsigned integer before the comparison. In this case, -1 is converted to the largest possible unsigned integer value (which is equal to 4294967295 for a 32-bit integer). Thus, the comparison becomes 4294967295 < 1, which is false.

To fix this, we should always make sure that we are comparing variables of the same data type (either both signed or both unsigned). The corrected code is as follows:

int a = -1;
int b = 1;

if (a < b) {
cout << "a is less than b" << endl;
}
else {
cout << "a is greater than or equal to b" << endl;
}

Now, the output is "a is less than b", which is the correct result of comparing -1 and 1.

Example 4: Binary Representation of Negative Numbers

Consider the following code snippet:

int a = -5;

cout << a << endl;
cout << bitset<32>(a) << endl;

What do you think will be the output of this code?

The output is -5 and 0b11111111111111111111111111111011. Here, we are printing the decimal representation of -5 and its binary representation using the bitset template class. The binary representation has 32 bits since we are assuming a 32-bit signed integer.

It's worth noting that the binary representation of negative numbers is not as straightforward as that of positive numbers. When we represent a positive number in binary, the most significant bit (MSB) is always 0 since all bits to the left of it represent powers of 2 (i.e., 2^31, 2^30, 2^29, and so on). However, when we represent a negative number in binary, the MSB is always 1 since we need to indicate that the number is negative. The remaining bits represent the magnitude of the number in two's complement form.

In two's complement form, the magnitude of a negative number is obtained by flipping all the bits of its absolute value (i.e., the positive version of the number) and then adding 1. For example, to obtain the two's complement form of -5, we first represent the binary version of 5:

5 = 0b00000000 00000000 00000000 00000101

Then, we flip all the bits:

0b11111111 11111111 11111111 11111010

0b11111111 11111111 11111111 11111011

Thus, the binary representation of -5 in two's complement form is 0b11111111111111111111111111111011.

## Popular questions

1. What is the difference between signed and unsigned integers?
Answer: Signed integers can represent both positive and negative values, whereas unsigned integers can only represent nonnegative values (i.e., zero and positive values).

2. What is the range of values a 32-bit signed integer can represent?
Answer: A 32-bit signed integer can represent values ranging from -2,147,483,648 to 2,147,483,647.

3. Why is it important to use signed and unsigned integers carefully?
Answer: The behavior of signed and unsigned integers during arithmetic operations and their ranges can differ depending on the compiler and machine. Using them incorrectly can result in undefined behavior or incorrect results.

4. What happens when we compare a signed integer with an unsigned integer in C++?
Answer: When we compare a signed integer with an unsigned integer in C++, the signed integer is implicitly converted to an unsigned integer before the comparison. This can lead to unexpected results if the signed integer is negative.

5. What is two's complement form, and how is it used to represent negative integers?
Answer: Two's complement form is a way of representing signed integers in binary form. In two's complement form, the magnitude of a negative number is obtained by flipping all the bits of its absolute value and adding 1. This makes it easier to perform arithmetic operations on signed integers in binary form.

### Tag

"DataTypes" ##### Deeksha Dev
Have an amazing zeal to explore, try and learn everything that comes in way. Plan to do something big one day! TECHNICAL skills Languages - Core Java, spring, spring boot, jsf, javascript, jquery Platforms - Windows XP/7/8 , Netbeams , Xilinx's simulator Other - Basic’s of PCB wizard
Posts created 1994

## blooket with code examples

Begin typing your search term above and press enter to search. Press ESC to cancel.