What are unsigned integers? and why to avoid it

Tram Ho

Unsigned integers (Unsigned integers)

In the previous lesson (signed integers (also known as integers) or integers), we mentioned signed integers, which are a set of data types that can contain positive integers. and negative, including 0.

C ++ also supports unsigned integers. An unsigned integer is an integer that can only contain non-negative integers.

Defines unsigned integers

To mean an unsigned integer, we use the unsigned keyword. By convention, this is preceded by the data type:

first
2
3
4
unsigned short us;
unsigned int ui;
unsigned long ul;
unsigned long long ull;

Range of unsigned integers

1-byte unsigned integers have a range from 0 to 255. Compare this value with a signed integer range, 1 byte from -128 to 127. Both can store 256 different values, but numbers signed integers will use half of their range for negative numbers, while unsigned integers can store positive numbers twice as big.

Here, a table displays the range for unsigned integers:

Size / TypeRange
1 byte unsigned0 to 255
2 bytes unsigned0 to 65,535
4 bytes unsigned0 to 4,294,967,295
8 bytes unsigned0 to 18,446,744,073,709,551,615

An n-bit unsigned variable has a range of 0 to (2n) -1.

When negative numbers are not required, unsigned integers are great for networks and systems with less memory, because unsigned integers can store more positive numbers without taking up more memory.

Things to keep in mind of integers with and without signs

New programmers sometimes confuse integers with and without signs. Here’s a simple way to remember the difference: to distinguish negative numbers from positive numbers, we use negative signs. If a number does not have positive or negative signs (+, -) then we assume that a number is positive. Therefore, a signed integer (signed integer) can have positive and negative values. If an integer has no sign (the unsigned integer), all values ​​are positive.

Overflow with unsigned integers

Trick question: What if we tried to store the number 280 (requires 9 bits to represent) in an unsigned 1-byte integer? You might think the answer is overwhelming! But, it does not overflow.

By definition, unsigned integers cannot overflow.

Let’s consider this by using 2-byte integers:

first
2
3
4
5
6
7
8
9
ten
11
twelfth
13
14
15
#include <iostream>
 
int main()
{
     unsigned short x{ 65535 }; // largest 16-bit unsigned value possible
     std::cout << "x was: " << x << 'n' ;
 
     x = 65536; // 65536 is out of our range, so we get wrap-around
     std::cout << "x is now: " << x << 'n' ;
 
     x = 65537; // 65537 is out of our range, so we get wrap-around
     std::cout << "x is now: " << x << 'n' ;
 
     return 0;
}

What do you think will be the outcome of this program?

first
2
3
x was: 65535
x is now: 0
x is now: 1
first
2
3
4
5
6
7
8
9
ten
11
twelfth
13
14
15
#include <iostream>
 
int main()
{
     unsigned short x{ 0 }; // smallest 2-byte unsigned value possible
     std::cout << "x was: " << x << 'n' ;
 
     x = -1; // -1 is out of our range, so we get wrap-around
     std::cout << "x is now: " << x << 'n' ;
 
     x = -2; // -2 is out of our range, so we get wrap-around
     std::cout << "x is now: " << x << 'n' ;
 
     return 0;
}
first
2
3
x was: 0
x is now: 65535
x is now: 65534

Controversy over unsigned integers

Many developers (and some large developers, like Google) believe that developers should generally avoid unsigned integers.

This is largely because the following two behaviors can cause problems.

First, consider the subtraction of two unsigned numbers, such as 3 and 5. 3 minus 5 is -2, but -2 can be expressed as an unsigned number as follows.

first
2
3
4
5
6
7
8
9
ten
#include <iostream>
 
int main()
{
     unsigned int x{ 3 };
     unsigned int y{ 5 };
 
     std::cout << x - y << 'n' ;
     return 0;
}

The program produces the following result:

first
4294967294

Secondly, unexpected behavior can result when you mix signed and unsigned integers. In the above example, even if one of the operands (x or y) is signed, the other operand (unsigned) will cause the signed operator to be converted into an unsigned integer, and this behavior will lead to unexpected results.

Consider the following excerpt:

first
2
3
4
5
6
7
8
9
ten
11
void doSomething(unsigned int x)
{
     // Run some code x times
}
 
int main()
{
     doSomething(-1);
 
     return 0;
}

The author of doSomething () expected someone to call this function only by a positive number. But the caller is switching to -1. What happens in this case?

The signed argument of -1 is completely converted to the unsigned parameter. -1 is not in the range of an unsigned number, so it surrounds a large number (probably 4294967295). Then your program will produce false results. Worse, there’s no good way to protect against this happening. C ++ will freely convert between signed and unsigned numbers.

If you need to protect a function against negative inputs, use an assertion or exception instead. Both are protected from this.

Some modern programming languages ​​(like Java) and frameworks (like .NET) either do not include unsigned types or restrict their use.

New programmers often use unsigned integers to represent non-negative data or to take advantage of additional ranges. Bjarne Stroustrup, the designer of C ++, said it is not a good idea to use unsigned integers instead of signed ones to have one more bit to represent positive integers.

Note: Avoid using unsigned numbers, unless otherwise specified or when unavoidable.

If you use unsigned numbers, avoid mixing unsigned and unsigned numbers if possible.

So when is it reasonable to use unsigned numbers?

There are still a few cases in which C ++ uses unsigned (or necessary) numbers.

First, unsigned numbers are preferred when processing bit operations.

Secondly, the use of unsigned zeros is still unavoidable in some cases, mostly what to do with indexing the array. We will talk more about this in array lessons and indexing arrays.

Also note that if you are developing for an embedded system (e.g. Arduino) or some other processor / memory limited contexts, the use of unsigned numbers is more common and acceptable. (and in some cases, unavoidable) for performance reasons.

Share the news now

Source : Techtalk