What purpose of unsigned in C++

Asked

Viewed 18,194 times

18

What is the purpose of unsigned in the C++?

Example: unsigned char ch2;

3 answers

20


As in C, the unsigned alone serves for nothing (except the one shown below), it is a modifier to determine that an integer numeric type is unsigned. In other words, you will only have positive values in it. It determines whether the most significant bit will be considered the positive or negative sign or whether this bit will enter the value, so it allows double the allowed values.

A int goes from -2147483648 to 2147483647.

A unsigned int from 0 to 4294967295.

The same goes for the char or short or long.

Note that the char intended to represent a character does not use the modifier. Only when it is used to represent an 8 bit number it is interesting to use the modifier to make explicit.

Use only unsigned without specifying anything else, the int is assumed.

In general there is the recommendation to use this type of data only if it is really necessary and if the programmer understands all its implications (example). Do not use because you do not want negative values, it does not give guarantees.

  • The examples given are correct if the int occupy 32 bits, but note that this may not be the case.

  • It’s true. I took the ordinary case.

9

Numeric variables can be flagged or not. Flagged means that the first bit (the one on the left) indicates the sign: 0: the number is positive, 1, the number is negative.

As in many applications it makes no sense to have a negative number (example number of pieces, number of children, age, etc.), it is possible to use variables without this sign:

An 8-bit variable with signal ranging from -128 to 127 The same variable, if unsigned, goes from 0 to 255.

But what’s the point of char?

If the char variable is used to register a character, use char simply. But we found the same type used to record numeric values. In this case use signed char or unsigned char.

Based on What is an unsigned char.

7

Data types marked as unsigned means that they will accept only positive values (including 0).

Various types of data can be marked as signed or unsigned:

Tipo de dados       Tipo de assinatura padrão
-------------------------------------------------
short            -> signed short
signed short
unsigned short
int              -> signed int
signed int
unsigned int
signed           -> signed int
unsigned         -> unsigned int
long             -> signed long
signed long
unsigned long
char                (is signed or unsigned depending on the implmentation)
signed char
unsigned char

Source

The guy char can also be signed, but one should be careful.

If you are using char as text, then use without marking it:

  • When literal character is 'a' or '0'.
  • When value is used to form strings as "abcde"
  • It also works as a numerical value, but has not been specified to be treated as signed or unsigned. Beware of inequality comparison if not limiting to ASCII (0-127).

If you are using numeric character, use:

  • signed char, when the values are -127 to 127. (-128 to 127 is common)
  • unsigned char, when positive values of 0 to 255.

"At least", because the C++ standard only gives the minimum range of values that each numeric type needs. sizeof (char) must be 1 (one byte), but one byte in theory can be for example 32 bits, sizeof would still have your size as 1, which means you could have sizeof (char) == sizeof (long) == 1.

Source²

Browser other questions tagged

You are not signed in. Login or sign up in order to post.