《(2.3)--2.3Representationofintegers.pdf》由会员分享,可在线阅读,更多相关《(2.3)--2.3Representationofintegers.pdf(11页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Representation of integersRepresentation of integers2.3Integer includes positive integers 1,2,3,.,n,.,0(zero),as well as negativeintegers-1,-2,-3,.,-n,.,where n is an integer.In computers,binary numbers are used to represent integers in two forms:unsigned(unsigned integers)and signed(signed integers
2、).Unsigned integers can onlyrepresent positive integers and 0.Signed integers can be used to represent positiveintegers,0,and negative integers.Representation of integers2.3Representation of unsigned integerThe encoding of an unsigned integer is the same as its value.Therefore,anunsigned integer can
3、 only represent positive integers and 0.Solution:01001011(binary)=027+126+025+024+123+022+121+120=0+64+0+0+8+0+2+1=75Therefore,binary number 01001011 represents unsigned integer 75.Eg1Assuming that the binary number 01001011 represents an unsigned integer,then whats the value of the unsigned integer
4、 it represented?Representation of integers2.3Its thinking is to digitize the sign of a number.For n-bit signed integers,use the most significant bit(MSB)to represent its sign:Positive numbers take 0,and negative numbers take 1.The remaining(n-1)digits are numeric digits,where the absolute value of t
5、he number is stored.If the absolute value isfewer than n-1 digits,make up with 0.1.Sign-magnitude representationSolution:xsign-magnitude=01011011ysign-magnitude=11011011zsign-magnitude=10010110Eg2Assume that x=+1011011,y=-1011011,and z=-10110(=-0010110),write out the8-bit binary form of x,y,and z us
6、ing sign-magnitude representation.Representation of signed integerRepresentation of integers2.3The range of an 8-bit sign-magnitude representation is:-127x127-(28-1)x(28-1)Representation of integers2.32.Complement representationsThe method to calculate complement(twos complement):The twos complement
7、 of a positive number is the same as its sign-magnituderepresentation.The twos complement of a negative number equals the bitwise NOT(0-1,1-0)ofits sign-magnitude except for the sign bit(also called ones complement)plus 1.Representation of integers2.3Eg3How to represent 75 with 1 byte in computer?So
8、lution:1 byte is 8 binary bits Since 75 is a positive integer,the sign bit(most significant digit)should be 0,indicating a positive value.The remaining7 bits are used to represent the absolute value of 75.Since the 75 in decimalequals 1001011 in binary.In computers,the representation of 75 using 1 b
9、yteshould be:01001011Representation of integers2.3Eg4How to represent-75 with 1 byte in computer?Solution:1 byte is 8 binary bits 75 in decimal equals 1001011 in binary.Thus the sign-magnituderepresentation of-75 should be 11001011.Next,well calculate the twos complement of-75 Calculate the bitwise
10、NOT of the last 7 digits.(Bitwise NOT means 0-1,1-0)Ones complement(Magnitude):1001011Bitwise NOT:0110100 Plus 1 to the least significant digit0110100+00000010110101Therefore,in computers,the representation of-75 using 1 byte should be:10110101Representation of integers2.3Properties of twos compleme
11、nt representation:x+y2s complement=x2s complement+y2s complementThe 2s complement of two numbers sum equals the sum of the 2s complement of these two numbers.x-y2s complement=x2s complement+-y2s complementThe 2s complement of the difference of two numbers equals the sum of 2s complements of the subt
12、rahend and the inverse of minuend.x 2s complement2s complement=x magnitudeThe 2s complement of the 2s complement of a number equals its magnitude representation.Representation of integers2.3Assume that we need to use 1byte to represent a signedinteger.Given that X=107,Y=-41,please calculate X+Y.Eg5S
13、olution:X2s complement=01101011,Y2s complement=11010111x+y2s complement=x2s complement+y2s complement01101011+11010111101000010Since the bit length of the arithmetic unit in a computer is a fixed as 8 bits,the MSB generated in the above calculation is automatically abandoned.X+Y2s complement=0100001
14、0Since the MSB is 0,which stands for a positive value,then X+Y magnitude=X+Y2s complementSince binary 1000010 is decimal 66.Therefore,the value of X+Y is 66.Representation of integers2.3Eg6Assume that we need to use 1 byte to represent a signed integer.Given that X=41,Y=107,now please caluclate X-YS
15、olution:X2s complement=00101001 -Y2s complement=-1072s complement=10010101x-y2s complement=x2s complement+-y2s complement00101001+1001010110111110Since the MSB of x-y2s complementis 1,its value is negative,and the 2s complement of x-y2s complementequals x-y2s magnitude=11000010.Since the highest bit is 1,which stands for a negative value,and binary 1000010 is decimal 66.Therefore,the value of X-Y is-66.