Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - The difference between signed and unsigned in C language
The difference between signed and unsigned in C language
1, indicating different numerical ranges.

A sign means a sign, its first bit means a sign, and the rest bits mean a size. For example, the size range of signedint is-128~ 127.

Unsigned means unsigned, all bits are size, unsigned. For example, the size range of unsignedint is 0~ 127.

2. Different types

Integers of signed type are encoded only by removing the most significant bit and the remaining 15 bit, while the most significant bit is only used as a sign to mark the positive and negative integers, with 0 representing positive and 1 representing negative. Therefore, for signed integers, the storage range of is (-215to215-1), that is, integers from -32768 to +32767.

For unsigned integers, all 16 bits are used for encoding, and the storage range is (0to2 16- 1), that is, non-negative integers from 0 to 65535. So you can declare inta= 1 or inta=- 1, but you can't declare unsigneda=- 1. However, it should be mentioned that no matter the type of integer is signed or unsigned, it is stored with 16 bits, which means that all 16 bits are used to store data.

3. The values expressed are different.

Integers of signed type are encoded only by removing the remaining 15, and the highest bit is used to mark the positive and negative of the integer, with 0 being positive and 1 being negative. So the integer storage range of signed is (-215 ~ 215-1).

If all 16 of unsigned integers are used for encoding, the storage range of unsigned integers is (0 ~ 2 16- 1).