Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 14 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
14
Dung lượng
199,01 KB
Nội dung
University of Washington Sec.on 2: Integer & Floa.ng Point Numbers ¢ ¢ ¢ ¢ ¢ ¢ ¢ ¢ Representa.on of integers: unsigned and signed Unsigned and signed integers in C Arithme.c and shiBing Sign extension Background: frac.onal binary numbers IEEE floa.ng-‐point standard Floa.ng-‐point opera.ons and rounding Floa.ng-‐point in C Integers University of Washington Unsigned Integers ¢ Unsigned values are just what you expect § b7b6b5b4b3b2b1b0 = b727 + b626 + b525 + … + b121 + b020 Đ Â Useful formula: 1+2+4+8+ +2N-‐1 = 2N -‐1 You add/subtract them using the normal “carry/borrow” rules, just in binary 63 + 71 00111111 +00001000 01000111 Integers University of Washington Signed Integers ¢ Let's do the natural thing for the posi.ves § They correspond to the unsigned integers of the same value § ¢ Example (8 bits): 0x00 = 0, 0x01 = 1, …, 0x7F = 127 But, we need to let about half of them be nega.ve § Use the high order bit to indicate nega%ve: call it the “sign bit” Call this a “sign-‐and-‐magnitude” representaQon § Examples (8 bits): § 0x00 = 000000002 is non-‐negaQve, because the sign bit is 0 § 0x7F = 011111112 is non-‐negaQve § 0x85 = 100001012 is negaQve § 0x80 = 100000002 is negaQve… § Integers University of Washington Sign-‐and-‐Magnitude Nega.ves ¢ How should we represent -‐1 in binary? § Sign-‐and-‐magnitude: 100000012 Use the MSB for + or -‐, and the other bits to give magnitude –7 –6 1111 1110 –5 –4 +0 +1 0000 0001 1101 +2 0010 +3 1100 0011 – 1011 1010 –2 1001 0100 + 0101 +5 0110 –1 1000 –0 Integers 0111 +7 +6 University of Washington Sign-‐and-‐Magnitude Nega.ves ¢ How should we represent -‐1 in binary? § Sign-‐and-‐magnitude: 100000012 Use the MSB for + or -‐, and the other bits to give magnitude (Unfortunate side effect: there are two representaQons of 0!) –7 –6 1111 1110 –5 –4 +0 +1 0000 0001 1101 +2 0010 +3 1100 0011 – 1011 1010 –2 1001 0100 + 0101 +5 0110 –1 1000 –0 Integers 0111 +7 +6 University of Washington Sign-‐and-‐Magnitude Nega.ves ¢ How should we represent -‐1 in binary? § Sign-‐and-‐magnitude: 100000012 Use the MSB for + or -‐, and the other bits to give magnitude (Unfortunate side effect: there are two representaQons of 0!) § Another problem: math is cumbersome –7 +0 § Example: –6 +1 1111 0000 -‐ 3 != 4 + (-‐3) 1110 –5 0100 +1011 1111 –4 0001 1101 +2 0010 +3 1100 0011 – 1011 1010 –2 1001 0100 + 0101 +5 0110 –1 1000 –0 Integers 0111 +7 +6 University of Washington Two’s Complement Nega.ves ¢ How should we represent -‐1 in binary? § Rather than a sign bit, let MSB have same value, but nega%ve weight W-‐bit word: Bits 0, 1, …, W-‐2 add 20, 21, …, 2W-‐2 to value of integer when set, but bit W-‐1 adds -‐2W-‐1 when set § e.g unsigned 10102: 1*23 + 0*22 + 1*21 + 0*20 = 1010 2’s comp 10102: -‐1*23 + 0*22 + 1*21 + 0*20 = -‐610 –1 § So -‐1 represented as 11112; all –2 +1 negaQve integers sQll have MSB = 1 1111 0000 1110 0001 § Advantages of two’s complement: –3 +2 1101 0010 only one zero, simple arithmeQc § § To get negaQve representaQon of any integer, take bitwise complement and then add one! ~x + = -x –4 0011 – 1011 1010 –6 1001 0100 + 0101 +5 0110 –7 Integers +3 1100 1000 –8 0111 +7 +6 University of Washington Two’s Complement Arithme.c ¢ The same addi.on procedure works for both unsigned and two’s complement integers § Simplifies hardware: only one adder needed § Algorithm: simple addiQon, discard the highest carry bit Đ Â Called “modular” addiQon: result is sum modulo 2W Examples: Integers University of Washington Two’s Complement ¢ Why does it work? § Put another way: given the bit representaQon of a posiQve integer, we want the negaQve bit representaQon to always sum to 0 (ignoring the carry-‐out bit) when added to the posiQve representaQon § This turns out to be the bitwise complement plus one § What should the 8-‐bit representaQon of -‐1 be? 00000001 +???????? (we want whichever bit string gives the right result) 00000000 00000010 +???????? 00000000 00000011 +???????? 00000000 Integers University of Washington Two’s Complement  Why does it work? Đ Put another way: given the bit representaQon of a posiQve integer, we want the negaQve bit representaQon to always sum to 0 (ignoring the carry-‐out bit) when added to the posiQve representaQon § This turns out to be the bitwise complement plus one § What should the 8-‐bit representaQon of -‐1 be? 00000001 +11111111 (we want whichever bit string gives the right result) 100000000 00000010 +???????? 00000000 00000011 +???????? 00000000 Integers University of Washington Two’s Complement ¢ Why does it work? § Put another way: given the bit representaQon of a posiQve integer, we want the negaQve bit representaQon to always sum to 0 (ignoring the carry-‐out bit) when added to the posiQve representaQon § This turns out to be the bitwise complement plus one § What should the 8-‐bit representaQon of -‐1 be? 00000001 +11111111 (we want whichever bit string gives the right result) 100000000 00000010 +11111110 100000000 00000011 +11111101 100000000 Integers University of Washington Unsigned & Signed Numeric Values X 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Unsigned Signed –8 –7 10 –6 11 –5 12 –4 13 –3 14 –2 15 –1 l Both signed and unsigned integers have limits l l l If you compute a number that is too small, you wrap: -‐7 -‐ 3 = ? 0U -‐ 2U = ? The CPU may be capable of “throwing an excepQon” for overflow on signed values l l If you compute a number that is too big, you wrap: 6 + 4 = ? 15U + 2U = ? But it won't for unsigned C and Java just cruise along silently when overflow occurs Integers University of Washington Visualiza.ons Same W bits interpreted as signed vs unsigned: Two’s" Two’s" 2w" 2w" complement" complement" +2w–1" 2w–1" +2w–1" 2w–1" 0" 0" 0" 0" Unsigned" Unsigned" –2w–1" –2w–1" ¢ Two’s complement (signed) addi.on: x and y are W bits wide ¢ x + y! +2w" Positive overflow" +2w –1" 0" +2w –1" Normal" –2w –1" –2w" 0" –2w –1" Negative overflow" Integers University of Washington Values To Remember  Unsigned Values Đ UMin = Đ 0000 Đ UMax = Đ 1111  0 Two’s Complement Values § TMin = –2w–1 § 100…0 § TMax = 2w–1 – 1 § 011…1 § NegaQve 1 § 111…1 0xFFFFFFFF (32 bits) 2w – 1 Values for W = 16 UMax TMax TMin -1 Decimal 65535 32767 -32768 -1 Hex FF FF 7F FF 80 00 FF FF 00 00 Integers Binary 11111111 11111111 01111111 11111111 10000000 00000000 11111111 11111111 00000000 00000000 ... +00001000 01000111 Integers University of Washington Signed Integers ¢ Let's do the natural thing for the posi.ves § They correspond to the unsigned integers of the same value...University of Washington Unsigned Integers ¢ Unsigned values are just what you expect § b7b6b5b4b3b2b1b0 = b727 + b626 + b525 + … + b121 + b020 Đ Â Useful... 000000 002 is non-‐negaQve, because the sign bit is 0 § 0x7F = 011111112 is non-‐negaQve § 0x85 = 100001012 is negaQve § 0x80 = 100000 002 is negaQve… § Integers