当前位置:网站首页>[postgraduate entrance examination planning group] conversion between signed and unsigned numbers
[postgraduate entrance examination planning group] conversion between signed and unsigned numbers
2022-06-26 08:08:00 【Live in the heart of sunshine】
The landlord has something to say :
Reviewing 《 The principle of computer organization 》 when , Encountered the problem of conversion between signed and unsigned numbers , To deal with this kind of problem , The landlord looked up the information carefully and summed up some experience in combination with the title , The contents are as follows ( If there is a new summary , It will be revised over and over again ):
Be careful :
1、 The word length is 16 position ( It's more convenient )
2、 I suggest you go to have a general understanding of C++ Byte length and indication range of basic data type in ; Such as :
char | 1 byte | int | 4 byte | short [int] | 2 byte | float | 4 byte | long [int] | 4 byte |
signed char | 1 byte | signed int | 4 byte | signed short [int] | 2 byte | double | 8 byte | signed long [int] | 4 byte |
unsigned char | 1 byte | unsigned int | 4 byte | unsigned short [int] | 2 byte | long double | 8 byte | unsigned long [int] | 4 byte |
3、(1) Range of fixed-point integers ( 16 bit ) : A signed :-32767 <= x <= 32767
(2) Range of fixed-point integer complement ( 16 bit ) : A signed :-32768 <= x <= 32767
4、 In the computer , Negative numbers are usually represented by complements . Be careful : All the examples in this article , It is to convert the number to be assigned into the form of complement first .
5、 clarify 0 and 1 The original code of 、 Inverse code 、 Complement code .(0 There is only one form of complement and shift ; The original code of a positive number 、 Inverse code 、 The complement is the same ;)
Original code | Inverse code | Complement code | |
[ +0 ] | 0000 0000 | 0000 0000 | 0000 0000 |
[ -0 ] | 1000 0000 | 1111 1111 | 0000 0000 |
[ +1 ] | 0000 0001 | 0000 0001 | 0000 0001 |
[ -1 ] | 1000 0001 | 1111 1110 | 1111 1111 |
One 、 Same type ( Like a signed number , Or both are unsigned numbers ) transformation
( One )1. Both are unsigned numbers ( From short to long : Add... At the top “ 0 ”.)
unsigned short x = 255; //short by 2 byte
unsigned int y = x; //int by 4 byte
x = 255 D = 1111 1111 B
y = 0000 0000 1111 1111 B = 255 D
summary : When both are unsigned numbers , From a shorter number to a longer number , That is, add... To the highest position “ 0 ”, be called “ Zero expansion ”.
( One )2. Both are unsigned numbers ( From long to short : Add... At the top “ 0 ”.)
unsigned int x = 255; //short by 2 byte
unsigned short y = x; //int by 4 byte
x = 255 D = 0000 0000 1111 1111 B
y = 1111 1111 B = 255 D
summary : When both are unsigned numbers , Change from a longer number to a shorter number , That is, remove the high order “ 0 ” byte , The lower byte remains unchanged .
( Two ) Both are signed numbers ( Changing from short to long is the same as changing from long to short “( One ) Both are unsigned numbers ” equally )
short x = -120; //short by 2 byte
int y = x; //int by 4 byte
x = -120 D , Its original code is :1111 1000 B, Its complement is :1000 1000 B (= 88 H)
y = 1111 1111 1000 1000 B = -120 D (= FF 88 H)
summary : When both are signed numbers , Use symbols to extend , When it is a negative number , High byte plus “ 1 ”; Positive number , High byte plus “ 0 ”.
Two 、 The conversion between signed and unsigned numbers
( One ) Signed number to unsigned number
short n = -1;
unsigned short m = n;
n = -1 D, Its complement is :1111 1111 B
m = 1111 1111 B = 65535 D
notes :n The highest position of “ 1 ” It represents the sign bit , because m Is an unsigned number , therefore m The highest position of “ 1 ” It means “ 2 Of 7 Power ”.
summary : When a signed number is changed to an unsigned number , When the signed number is negative , The key is to put the highest “ 1 ” become “ 2 To the power of ”.
( Two ) Unsigned number to signed number
unsigned short a = 65535;
short b = -1;
a = 65535 = FF FF FF FF H
b The complement of is :FF FF FF FF H = -1 D
notes : because b Is a signed number , The highest position is “ 1 ”, therefore b It's a negative number , It can be seen that its value is -1.
summary : When an unsigned number is converted to a signed number , First get the complement of the assigned number , It is the complement of the assigned number ; Then the complement of the assigned number is inversely added to get the original code , Calculate again to get the decimal value .
Another example :
unsigned char i = 130;
char k = i;
i = 130 D = 1000 0010 B ( Be careful : The original code of a positive number = A complement to a positive number = The inverse of a positive number )
k The complement of is :1000 0010 B, The original code is obtained by adding one to the reverse :1111 1110 B, because k Is a signed number , The highest position is “ 1 ”, therefore k It's a negative number , That is to say -126 D.
practice :( Enclosed 2011 True questions of the annual unified examination , Easy to understand and consolidate )
Suppose in a 8 Bit word long computers run as follows C Procedures section :
unsigned int x = 134;
unsigned int y = 246;
int m = x;
int n = y;
unsigned int z1 = x - y;
unsigned int z2 = x + y;
int k1 = m - n;
int k2 = m + n;
If the compiler compiles 8 individual 8 Bit register R1 - R8 Assign to variables separately x、y、m、n、z1、z2、k1、k2, Please answer the following questions ( Tips : Signed integers are represented by complements )
( 1 ) After executing the above program segment , register R1、 R5 and R6 What are the contents of ?( In hexadecimal notation )
( 2 ) After executing the above program segment , Variable m and k1 What are the values of ?( In decimal )
( 3 ) The above program segment involves the addition of signed integers / reduce 、 Unsigned integer plus / Subtraction operation , Can these four operations be realized by the same adder auxiliary circuit ? Brief reasons .
( 4 ) How to judge the addition of signed integers in the computer / Whether the result of subtraction overflows ? In the above program segment , The execution results of signed integer operation statements will overflow ?
Explain :( 1 ) register R1、 R5 and R6 The content of , They correspond to each other x, z1(x - y) , z2(x + y).
x = 134 D, The original code is equal to the complement code 1000 0110 B, That is to say 86 H, That is to say R1
y = 246 D, The original code is equal to the complement code 1111 0110 B, That is to say F6 H
z1 = x - y = x Complement + [-y] Complement = 1000 0110 B + 0000 1010 B = 1001 0000 B = 90 H, That is to say R5
z2 = x + y = x Complement + y Complement = 1000 0110 B + 1111 0110 B = (1) 0111 1100 B = 7C H, That is to say R6
( Be careful :z2 Overflow at , But the addition and subtraction of unsigned integers , Generally, overflow is not considered , Only when output, if the highest bit of the signed number is the sign bit , intend z2 If it is converted to a signed number , The highest bit is 1.)
therefore R1 = 134 = 86 H;
R5 = 90 H;
R6 = 7C H;
( 2 )m Is an unsigned integer x To signed integer ,n Is an unsigned integer y To signed integer , from (1) know
x The complement of is 1000 0110 B,y The complement of is 1111 0110 B, therefore
m The complement of is 1000 0110 B, Its original code is 1111 1010 B, Its value is -122 D
n The complement of is 1111 0110 B, Its original code is 1000 1010 B, Its value is -10 D
namely k1 = m - n = m Complement + -n Complement = 1000 0110 B + 0000 1010 B = 1001 0000 B = 90 H, Get the original code as 1111 0000 B = -112 D
By the way k2 = m + n = m Complement + n Complement = 1000 0110 B + 1111 0110 B = (1) 0111 1100 B = 7C H( Be careful : There is overflow here )
therefore ,m = -122;
k1 = -112;
Digression ( Yes OF 、SF、CF Also pay attention to distinguish ):
k2 = m + n, From the known m = -122,n = -10, The sum of the two is still negative , But the result is positive . therefore , overflow flag OF = 1, It means overflow , Description register R6 The content in is not the real result ; Symbol sign bit SF = 0, Indicates that the result is a positive number ( The overflow flag is 1, There is an error in the symbol ), Carry flag bit CF = 1, It only indicates that the adder has the highest carry , It doesn't mean anything to the result of the operation .
( 3 ) can .n The bit adder implements modulo 2 Of n Power unsigned integer addition operation . Because in a computer , Both positive and negative numbers are represented by complements , The addition of signed integers and unsigned integers , It can be realized directly by adder . For subtraction , As long as the complement of the subtracted number plus the complement of the negative number of the subtracted number , But consider the overflow situation . So signed integers plus / reduce 、 Unsigned integer plus / The subtraction operation can be realized by the same adder auxiliary circuit ( The overflow judgment circuit is different ).
( 4 ) yes . The computer internally judges that a signed integer plus / Whether the result of subtraction overflows , Yes 3 Methods . One is if two inputs of the adder ( Add ) The same symbol as , And different from the output ( and ) The symbol of , The result overflows . Second, when the adder completes the addition operation , If the second highest ( Highest digit ) Carry and highest bit of ( Sign bit ) The carry of is different , The result overflows . Third, both signed integers are negative numbers , When the two are added together , The result is less than 8 The smallest negative number that a binary can represent , The result overflows .
In the implementation of k2 Overflow occurs when the statement of .k2 = 1000 0110 B + 1111 0110 B = (1) 0111 1100 B, In parentheses is the carry of the adder , because 2 Signed integers are all negative numbers , After addition, the highest bit of the result is 0 , So overflow .
( Overflow judgment method simple version : Double sign decision 、 Highest carry 、 The symbol of the operand with the same symbol is different from that of the original operand , The result overflows .)
notes :
Double sign decision :
Double sign bit operation with complement ( The positive sign is 00, The negative sign is 11), The first sign bit represents the symbol of the final result , The second sign bit indicates whether the operation result overflows . The first sign bit is the same as the second sign bit , There is no overflow ,; If different , Then overflow . As follows :
If the sign bit of the operation result is 01, Is a positive overflow ;
If the double sign of the result is 10, Negative overflow ;
If the double sign bit of the result is 00, The result is a positive number , No overflow .
If the double sign bit of the result is 11, The result is a negative number , No overflow .
边栏推荐
- QT之一个UI里边多界面切换
- Es performance tuning and other features
- MySQL practice: 3 Table operation
- JMeter performance testing - Basic Concepts
- Software engineering - high cohesion and low coupling
- Here is the command to display the disk space usage. Go ahead and pay attention to more wonderful things!
- Listview control
- Which securities company has the lowest Commission for opening a mobile account? Is it safe to open an account online?
- Chapter 3 (data types and expressions)
- Minor problems in importing D
猜你喜欢
. eslintrc. JS configuration
The difference between setstoragesync and setstorage
Go语言浅拷贝与深拷贝
The 9th zero code training camp is officially open for registration! (Beijing, Shanghai, Guangzhou and Shenzhen)
How to debug plug-ins using vs Code
QT之一个UI里边多界面切换
Database learning notes II
Interview for postgraduate entrance examination of Baoyan University - machine learning
Go language shallow copy and deep copy
Apache inlong graduated as a top-level project with a million billion level data stream processing capability!
随机推荐
Google Earth engine (GEE) 02 basic knowledge and learning resources
1002: easy to remember phone number
JS precompile - Variable - scope - closure
You can command Siri without making a sound! The Chinese team of Cornell University developed the silent language recognition necklace. Chinese and English are OK
. eslintrc. JS configuration
The first multi platform webcast of 2021ccf award ceremony pays tribute to the winners! CCF outstanding engineer
Click the button to call the system browser to open Baidu home page
Detailed explanation and code implementation of soft voting and hard voting mechanism in integrated learning
[UVM practice] Chapter 2: a simple UVM verification platform (5) build test cases
[UVM practice] Chapter 3: UVM Fundamentals (3) field automation mechanism
What is Wi Fi 6 (802.11ax)? Why is Wi Fi 6 important?
我想要股票账户优惠开户,如何操作?手机开户安全么?
Webrtc has become the official standard of W3C and IETF, and mobile terminal development
What if the service in Nacos cannot be deleted?
Introduction to uni app grammar
Uniapp uses uviewui
技术分享 | MySQL:caching_sha2_password 快速问答
Chapter 5 (array)
Web technology sharing | webrtc recording video stream
[UVM basics] connect of UVM_ Phase execution sequence