What purpose of unsigned in C ++

13

What is the purpose of unsigned in C ++?

Example: unsigned char ch2;

    
asked by anonymous 30.07.2015 / 16:05

3 answers

15

Just as C% w / w% is not good for anything (except shown below), it is a modifier to determine that an integer numeric type is unsigned. That is, you will only have positive values in it. It determines if the most significant bit will be considered the positive or negative signal or if this bit will enter the value, so it allows double the allowed values.

A unsigned goes from -2147483648 to 2147483647.

A int goes from 0 to 4294967295.

The same is true for unsigned int or char or short .

Note that long with the intention of representing a character does not use the modifier. Only when it is used to represent an 8-bit number that it is interesting to use the modifier to make explicit.

If you only use char without specifying anything else, unsigned is assumed.

In general there is a recommendation to use this type of data only if it is really necessary and if the programmer understands all its implications.

    
30.07.2015 / 16:25
7

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

Since many applications do not make sense to have a negative number (example number of pieces, number of children, age, etc.), you can use variables without this sign:

  

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

But what is the meaning for char ?

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

Based on What is an unsigned char .

    
30.07.2015 / 16:29
5

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

Multiple data types 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

Font

Type char can also be signed, but care must be taken.

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

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

If you are using a numeric type character, use:

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

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

Font

    
30.07.2015 / 16:42