[Cs3] signed vs unsigned characters
Mikhail Nesterenko
mikhail at cs.kent.edu
Wed Jan 18 11:42:03 EST 2023
CS3 students,
The size of type "char" is implementation defined, but is guaranteed
to be at least one byte. However, it is often 1 byte and, to save
space, programmers use it as a very small integer.
Now signed types have negative values, they usually use the first bit
to represent the sign. Unsigned types do not. This changes the range
of the numbers represented. For example, if char is implemented as one
by, if it is signed, its range is -128 to 127, if it signed, then the
range is from 0 to 255.
Note that decrementing an unsigned zero value results in underflow
(the number is too small to be represented). So signed types are
used for arithmetic manipulation.
However, sign, is placed in and "odd" highest bit. So if the number is
used for bit string representation, masking, bit shifting and similar
operation the sign location is inconvenient. Therefore, unsigned types
are usually used for these operations.
Slightly more details are here:
https://stackoverflow.com/questions/16503373/difference-between-char-and-signed-char-in-c
Thanks,
--
Mikhail
More information about the cs3
mailing list