c Language signed integer : Introduction of signed and unsigned numbers , defect :c Language signed integer 3651/9

Understand how signed numbers and unsigned numbers are represented in computers ?

this point , You may have heard two different answers .

One is textbooks , It will tell you : For computer “ Complement ” Indicates a negative number . But it's about “ Complement ” It takes a lesson to say the concept of , These we need in the 6 One chapter in the chapter 2 Hexadecimal
everything . also , use “ Complement ” Indicates a negative number , Actually, a formula , The function of the formula is to tell you , Get the answer to the question , How to calculate . But I didn't tell you why using this formula can match the answer ?

The other is what some programmers tell you : Use the highest bit of binary number to represent the symbol , The highest level is 0, Indicates a positive number , The highest level is 1, Indicates a negative number . This statement is true in itself , But if there is no following ,
Then it's wrong . At least it can't explain , Why character type -1 Binary means yes “1111 1111”(16 Base is FF); Not what we can understand better “1000
0001”.( Why is the latter better understood ? Because since the highest position is 1 Hour means negative number , that 1000 0001 No, exactly -1 Is it ?).

Introduction to unsigned numbers :

Unsigned numbers are for binary , The table number range of unsigned numbers is non negative .

All binaries represent numeric values , No sign bit . That is, the first "0" or "1" Does not indicate positive or negative .

Unsigned numbers are relative to signed numbers

C It supports signed and unsigned number operations of all integer data types . although C The standard does not specify a representation of a signed number , But almost all machines use binary complement . usually , Most numbers are signed by default ,C Conversion between unsigned and signed numbers is also allowed , The conversion principle is that the basic bit representation remains unchanged . So on a binary complement machine , When converting from unsigned to signed , Effect is application U2Tw, When converting from signed to unsigned numbers , Application function T2Uw, among w Number of digits representing the data type .

T2Uw(x) = (x<0)?(x+2w) :x;U2Rw(x) =
(x<2w-1)?x:(x-2w); When performing an operation , If one of its operands is signed and the other is unsigned , that C Implicitly cast signed parameters to unsigned numbers , And assume that both numbers are nonnegative , To perform this operation .

Introduction to signed numbers :

Signed numbers are for binary . Use the highest bit as the sign bit ,“0” representative “+”,“1” representative “-”; The remaining digits are used as numerical digits , Representative value .

Representation of signed numbers : The data in the computer is represented in binary , The symbol of number can only be used 0/1 express . Generally, the most significant bit is used (MBS) To represent the symbol of a number , For positive numbers 0 express , For negative numbers 1 express .

Coding method of signed numbers , Commonly used is complement , In addition, there are original code and inverse code . When using different binary encoding methods to represent signed numbers , The number of machines obtained may be different , But the truth value should be the same .

The range difference between unsigned and signed numbers :

Unsigned number , All bits are used to directly represent the size of the value . The highest bit of a signed number is used to indicate positive and negative , therefore , When positive , The maximum value of this number will become smaller . Let's take a byte of numerical comparison :

Unsigned number : 1111 1111    value :255 1* 27 + 1* 26 + 1* 25 + 1* 24 + 1* 23 + 1* 22 + 1* 21
+ 1* 20

Signed number : 0111 1111    value :127          1* 26 + 1* 25 + 1* 24 + 1* 23 + 1* 22 + 1*
21 + 1* 20

Same byte , The maximum unsigned number is 255, And the maximum value of signed number is 127. The reason is that the highest bit in the signed number is removed to represent the symbol . also , We know , The highest weight is also the highest ( about 1 Yes for bytes 2 of 7 Power =128), So only less than one , The maximum value is halved at once .

however , The advantage of signed numbers is that they can represent negative numbers . therefore , Although its maximum value has shrunk , But there is an extension in the direction of negative value . We still compare the value of one byte :

Unsigned number :                        0 ----------------- 255

Signed number :         -128 --------- 0 ---------- 127

Same byte , The minimum value of unsigned is 0
, And the minimum value of signed number is -128. Therefore, the number of different values expressed by the two is the same 256 individual . But the former expresses 0 reach 255 this 256 number , The latter expresses -128 reach +127 this 256 number .

How is the minimum value of a signed data type calculated ?

The calculation method of the maximum value of signed data type is exactly the same as that of unsigned data type , It's just that it's missing a top position ( See para 3 spot ). But within the negative range , The numerical calculation method cannot be used directly 1* 26 + 1* 25
Formula of . In the computer , Divide the negative number by the highest number 1 outside , It is also expressed in the form of complement . So before calculating its value , The complement needs to be restored . These contents will be discussed in Chapter 6
Unified learning in knowledge system .

Technology