C++

What is the difference between signed and unsigned int in C++?

In C++ data types play an important role in computing for storing and assessing the data stored in the memory. One of them is the integer data type used to represent integers such as whole numbers. Integers come in two types: signed and unsigned. Although at first look they may appear to be interchangeable, there is a difference. this article will show you the crucial distinctions between the two that might influence where they are used in code and also explain the basic concept of signed and unsigned int in C++.

The signed int in C++

A signed int is referred to as a data type in C++ that uses a defined amount of bits to store integer values, positive as well as negative. The number of bits allotted for a signed int, which on modern systems is typically 32 bits, determines the range of integers that it can store.

Syntax

The basic and straightforward C++ syntax for signed integers:

signed int variable_Name;

The signed int is a data type that declares a variable named as variable_Name. The signed keyword indicates that the variable may hold values that are positive as well as negative.

Simple Example of signed int

Sure, let’s examine the application of signed integers in C++ using an example program:

#include <iostream>
using namespace std;

int main() {
    signed int num1 = -11;
    signed int num2 = 200;
    cout << "Signed integer num1 as negative integer: " << num1 << endl;
    cout << "Signed integer num2 as positive integer: " << num2 << endl;
    num1= 2147483647;
    num2= -2147483648;
    cout << "Signed integer num1 as positive integer: " << num1 << endl;
    cout << "Signed integer num2 as negative integer: " << num2 << endl;
   
    return 0;
}

In this example program, two signed int variables named num1 and num2 have been defined. Then, we set the initial values of a and b to be -11 and 200, respectively.

Next, we use the cout statement to output the integers of both variables. The result demonstrates the ability of the signed integers num1 and num2 to store both positive and negative values.

The highest and lowest values that can be contained in a 32-bit signed integer are then once more assigned. A 32-bit signed integer can hold up to 2147483647 bits of data, while a 32-bit signed integer can hold down to -2147483648 bits of data.

We then print the values for both variables once more. The result demonstrates that the 32-bit signed integers num1 and num2 may store the highest and smallest values possible, respectively as the output showing below:

It’s vital to remember that when defining integer variables in C++, using a signed number is the default option. The variable is presumed to have the value of a signed integer if the keyword for signed is not present.

The unsigned int in C++

An unsigned int is a data type in C++ that uses a fixed amount of bits and may carry integer values, but only positive values. The total number of bits allotted for an unsigned int also affects the range of numbers that it may store.

Let’s see the syntax and application of unsigned integers in the programming language C++ in more detail.

Syntax

The C++ unsigned integer syntax is given below:

unsigned int var_Name;

The unsigned int is shown as a datatype in C++ used to define a variable named var_Name. The unsigned keyword tells that the variable can save only the positive values.

Example of unsigned int

#include <iostream>
using namespace std;

int main() {
    unsigned int num3 = 11;
    unsigned int num4 = 200;
    cout << "Unsigned integer num3 as a positive integer: " << num3 << endl;
    cout << "Unsigned integer num4 as a positive integer: " << num4 << endl;
    num3 = 4294967295;
    cout << "Unsigned integer num3 with update positive integer: " << num3 << endl;
   
    return 0;
}

In the above program, two unsigned int variables named num3 and num4 have been declared. The next step is to initialize num3 and num4 with positive values of 11 and 200, respectively.

Next, we use the cout statement to output the value of both variables. The result demonstrates that only non-negative values can be stored in the unsigned integers num3 as well as num4.

The 32-bit unsigned integer num3 is subsequently given its maximum possible value. A 32-bit unsigned integer can have a maximum value of 4294967295.

We then display the integer value of the variable num3 once more. The result demonstrates that the unsigned number a can store the largest value possible for a 32-bit unsigned. When the code executes, the following outcome will be observed:

It’s essential to keep in mind that arithmetic operations can have unexpected outcomes when unsigned integers are used. For instance, what we get may “turn around” to a huge positive number if we negate a bigger number from a smaller one. Unsigned integers should only be used if non-negative values need to be used and with caution.

Difference between signed and unsigned int in C++

Sure, here’s a table summarizing the differences between signed and unsigned integers in C++:

Property Signed Integers Unsigned Integers
Range of Values -2^(n-1) to 2^(n-1)-1, while n is the integer’s bit representation. For example, the storage range of a 32-bit signed integer is from -2,147,483,648 to 2,147,483,647. 0 to 2^(n-1), in which n is the integer’s bit representation size. A 32-bit unsigned integer, for instance, can hold values between 0 and 4,294,967,295.
Overflow-Behavior The integer with signed int overflow has undefined behavior. If an integer that is unsigned exceeds its bounds, a consequence is reduced modulo 2n, where n is the integer’s bit representation size. The phrase “wraparound” behavior describes this.
The Use of Memory Represents the integer’s sign using 1 bit. Displays an integer’s sign using one bit.
Represents the size of the integer using all bits.
By Default Choice The variable is presumed to be a signed integer even if the keyword “signed” is not used. If the keyword “unsigned” disappears, it is presumed that the variable is a signed integer automatically.
Arithmetic Operations It shows both positive and negative numbers. It only supports numbers that are non-negative.
Binary representation The leftmost bit in signed integers denotes the sign of the value (positive or negative). Whereas in unsigned integers, the number’s magnitude is expressed by all bits.

Based on the specific needs of the program, integer types need to be chosen. If you need negative values, you should use a signed integer. An unsigned integer ought to be used when only non-negative integers are needed.

Conclusion

In C++, the way we think of negative values is the primary distinction among both the signed and unsigned numbers. Depending on the exact use case, it may be best to utilize signed integers when it may be necessary to employ negative values and unsigned integers when only non-negative numbers are required.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.