Integer Representations
Integer Representations(整数表示)
C语言整数数据类型的典型取值范围
C data type | Minimum32bit | Minimum64bit | Maximum32bit | Maximum64bit | Bytes32bit | Bytes64bit |
---|---|---|---|---|---|---|
[signed] char | $-2^7$ | $-2^7$ | $2^{7}-1$ | $2^{7}-1$ | 1 | 1 |
unsigned char | 0 | 0 | $2^{8}-1$ | $2^{8}-1$ | 1 | 1 |
short | $-2^{15}$ | $-2^{15}$ | $2^{15}-1$ | $2^{15}-1$ | 2 | 2 |
unsigned short | 0 | 0 | $2^{16}-1$ | $2^{16}-1$ | 2 | 2 |
int | $-2^{31}$ | $-2^{31}$ | $2^{31}-1$ | $2^{31}-1$ | 4 | 4 |
unsigned | 0 | 0 | $2^{32}-1$ | $2^{32}-1$ | 4 | 4 |
long | $-2^{31}$ | $-2^{63}$ | $2^{31}-1$ | $2^{63}-1$ | 4 | 8 |
unsigned long | 0 | 0 | $2^{31}-1$ | $2^{64}-1$ | 4 | 8 |
int32_t | $-2^{31}$ | $-2^{31}$ | $2^{31}-1$ | $2^{31}-1$ | 4 | 4 |
uint32_t | 0 | 0 | $2^{32}-1$ | $2^{32}-1$ | 4 | 4 |
int64_t | $-2^{63}$ | $-2^{63}$ | $2^{63}-1$ | $2^{63}-1$ | 8 | 8 |
uint64_t | 0 | 0 | $2^{64}-1$ | $2^{64}-1$ | 8 | 8 |
Unsigned Encodings
For vector $\vec{x} = [x_{w-1},x_{w-2},…,x_0]$
$$ B2U_{w}(\vec{x})\stackrel{.}{=}x_{w-1}{\cdot}2^{w-1}+x_{w-2}{\cdot}2^{w-2}+…+x_{0}{\cdot}2^{0}=\sum_{i=0}^{w-1}{x_{i}2^{i}} $$
for example:
$$ B2U_{4}([0101])=0{\cdot}2^3+1{\cdot}2^2+0{\cdot}2^1+1{\cdot}2^0=5 $$
$$ B2U_{4}([1011])=1{\cdot}2^3+0{\cdot}2^2+1{\cdot}2^1+1{\cdot}2^0=11 $$
十进制:
$$ B10U_{w}(\vec{y})\stackrel{.}{=}y_{w-1}{\cdot}10^{w-1}+y_{w-2}{\cdot}10^{w-2}+…+y_{0}{\cdot}10^{0}=\sum_{i=0}^{w-1}{y_{i}10^{i}} $$
Two’s Complement Encodeings
补码编码
For vector $\vec{x} = [x_{w-1},x_{w-2},…,x_0]$
$$ B2T_{w}(\vec{x})\stackrel{.}{=}x_{w-1}{\cdot}-2^{w-1}+x_{w-2}{\cdot}2^{w-2}+…+x_{0}{\cdot}2^{0}=-x_{w-1}2^{w-1}+\sum_{i=0}^{w-2}{x_{i}2^{i}} $$
for example:
$$ B2U_{4}([0101])=-0{\cdot}2^3+1{\cdot}2^2+0{\cdot}2^1+1{\cdot}2^0=5 $$
$$ B2U_{4}([1011])=-1{\cdot}2^3+0{\cdot}2^2+1{\cdot}2^1+1{\cdot}2^0=-11 $$
Unsigned Vs Signed
Unsigned Maximum
work size | Binary | Hexadecimal | Decimal |
---|---|---|---|
8 | 1111 1111 | 0xFF | $2^8-1$ |
16 | $\underbrace{1111…1111}_{16}$ | 0xFFF | $2^{16}-1$ |
32 | $\underbrace{1111…1111}_{32}$ | 0xFFFFFFFF | $2^{32}-1$ |
64 | $\underbrace{1111…1111}_{64}$ | $0x\underbrace{FFF…FFF}_{16}$ | $2^{64}-1$ |
Signed Maximum
work size | Binary | Hexadecimal | Decimal |
---|---|---|---|
8 | 0111 1111 | 0x7F | $2^7-1$ |
16 | $0\underbrace{111…1111}_{15}$ | 0x7FF | $2^{15}-1$ |
32 | $0\underbrace{111…1111}_{31}$ | 0x7FFFFFFF | $2^{31}-1$ |
64 | $0\underbrace{111…1111}_{63}$ | $0x7\underbrace{FF…FFF}_{15}$ | $2^{63}-1$ |
Signed Minimum
work size | Binary | Hexadecimal | Decimal |
---|---|---|---|
8 | 1000 0000 | 0x80 | $-2^7$ |
16 | $1\underbrace{000…0000}_{15}$ | 0x8000 | $-2^{15}$ |
32 | $1\underbrace{000…0000}_{31}$ | 0x80000000 | $-2^{31}$ |
64 | $1\underbrace{111…1111}_{63}$ | $0x8\underbrace{00…000}_{15}$ | $-2^{63}$ |
Special Numeric
Value | 8 | 16 | 32 | 64 |
---|---|---|---|---|
-1 | 1111 1111 | $\underbrace{1111…1111}_{16}$ | $\underbrace{1111…1111}_{32}$ | $\underbrace{1111…1111}_{64}$ |
UMax | 1111 1111 | $\underbrace{1111…1111}_{16}$ | $\underbrace{1111…1111}_{32}$ | $\underbrace{1111…1111}_{64}$ |
Error : 1000 0001
weight | $\pm2^{15}$ | $2^{14}$ | $2^{13}$ | $2^{12}$ | $2^{11}$ | $2^{10}$ | $2^{9}$ | $2^{8}$ | $2^{7}$ | $2^{6}$ | $2^{5}$ | $2^{4}$ | $2^{3}$ | $2^{2}$ | $2^{1}$ | $2^{0}$ |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$12345_2$ | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 |
$12345_{10}$ | 0 | 0 | 8192 | 4096 | 0 | 0 | 0 | 0 | 0 | 0 | 32 | 16 | 8 | 0 | 0 | 1 |
$-12345_2$ | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 |
$-12345_{10}$ | -32768 | 16384 | 0 | 0 | 2048 | 1024 | 512 | 256 | 128 | 64 | 0 | 0 | 0 | 4 | 2 | 1 |
$53191_2$ | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 |
$53191_{10}$ | 32768 | 16384 | 0 | 0 | 2048 | 1024 | 512 | 256 | 128 | 64 | 0 | 0 | 0 | 4 | 2 | 1 |
Conversion between Signed and Unsigned
|
|
运行结果
a = -12345, b = 53191
-12345: 1100 1111 1100 0111
53191: 1100 1111 1100 0111
Two’s Complement to Unsigned
$$ B2U_{w}(\vec{x})\stackrel{.}{=}x_{w-1}{\cdot}2^{w-1}+x_{w-2}{\cdot}2^{w-2}+…+x_{0}{\cdot}2^{0} $$
$$ B2T_{w}(\vec{x})\stackrel{.}{=}x_{w-1}{\cdot}-2^{w-1}+x_{w-2}{\cdot}2^{w-2}+…+x_{0}{\cdot}2^{0} $$
$$ B2U_{w}-B2T_{w}=x_{w-1}{\cdot}2^{w-1}-x_{w-1}{\cdot}-2^{w-1}=x_{w-1}{\cdot}2^{w} $$
$T2U_{w}(x)$
$$ B2U_{w}(\vec{x})=B2T_{w}(\vec{x})+x_{w-1}{\cdot}2^{w} $$
$$ T2U_{w}(x)= \begin{cases} x+2^w, & \text {x < 0 } \\ x, & \text{ x $\geq$ 0} \end{cases} $$
$U2T_{w}(u)$
$$ B2T_w=B2U_w-x_{w-1}{\cdot}2^w $$
$$ U2T^w(u)= \begin{cases} u, & \text {u $\leq$ $TMax_w$ } \\ u-2^w, & \text{ u > $TMax_w$} \end{cases} $$
Signed versus Unsigned in C
|
|
Expanding the bit Representation of a Number
unsigned char a;
$x_7$ | $x_6$ | $x_5$ | $x_4$ | $x_3$ | $x_2$ | $x_1$ | $x_0$ |
---|
unsigned short b;
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | $x_7$ | $x_6$ | $x_5$ | $x_4$ | $x_3$ | $x_2$ | $x_1$ | $x_0$ |
---|
Signed Extension
有符号数 符号扩展
char b;
$x_7$ | $x_6$ | $x_5$ | $x_4$ | $x_3$ | $x_2$ | $x_1$ | $x_0$ |
---|
short b;
$$ \underbrace{x_{7}\text{|}x_{7}\text{|}x_{7}\text{|}x_{7}\text{|}x_{7}\text{|}x_{7}\text{|}x_{7}\text{|}x_{7}}_{sign\ extension} $$
$$ x_{7}\text{|}x_{6}\text{|}x_{5}\text{|}x_{4}\text{|}x_{3}x_{2}\text{|}x_{1} $$
$x_7 = 0$
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | $x_7$ | $x_6$ | $x_5$ | $x_4$ | $x_3$ | $x_2$ | $x_1$ | $x_0$ |
---|
$x_7=1$
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | $x_7$ | $x_6$ | $x_5$ | $x_4$ | $x_3$ | $x_2$ | $x_1$ | $x_0$ |
---|
资料由
九曲阑干
视频提供,如有侵权,联系sliver_horn@qq.com
删!