1 / 21

Bit Representation of Integer and Floating point numbers

Bit Representation of Integer and Floating point numbers. An overview Slides adapted from book Computer Systems: A Programmer's Perspective, R. Bryant and D. O'Hallaron , 2 nd ed , Ch 2. Agenda. Two’s complement Floating point Bit-level operation in C. Two’s complement. 2’s complement.

lyndon
Download Presentation

Bit Representation of Integer and Floating point numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bit Representation of Integer and Floating point numbers An overview Slides adapted from book Computer Systems: A Programmer's Perspective, R. Bryant and D. O'Hallaron, 2nded, Ch 2

  2. Agenda • Two’s complement • Floating point • Bit-level operation in C

  3. Two’s complement

  4. 2’s complement • A coding to represent signed integer number • Format for n-bit: • most significant bit indicates sign • 0 for nonnegative • 1 for negative • Bit representation of the number • Positive number (x): regular binary representation of the number x • Negative number (-x): binary representation of ~x + 1

  5. X B2U(X) B2T(X) 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 –8 1001 9 –7 1010 10 –6 1011 11 –5 1100 12 –4 1101 13 –3 1110 14 –2 1111 15 –1 2’s complement example A 4-bit representation of two’s complement and unsigned integer Binary to Unsigned (B2U) Binary to two’s complement (B2T) 2’s complement numbers 0: 00..000 -1: 11..111 Maximum number (Tmax): 0111..111 Minimum number (Tmin):1000…000

  6. -1 x 1 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 + ~x 0 1 1 0 0 0 1 0 2’s Complement Negation and Complement • Negation: Following Holds for 2’s Complement ~x + 1 == -x • Complement • Observation: ~x + x == 1111…111 == -1

  7. Floating point numbers

  8. Carnegie Mellon IEEE Floating Point • IEEE Standard 754 • Established in 1985 as uniform standard for floating point arithmetic • Before that, many idiosyncratic formats • Supported by all major CPUs • Driven by numerical concerns • Nice standards for rounding, overflow, underflow • Hard to make fast in hardware • Numerical analysts predominated over hardware designers in defining standard

  9. Carnegie Mellon Floating Point Representation • Numerical Form: (–1)sM 2E • Sign bits determines whether number is negative or positive • SignificandM normally a fractional value in range [1.0,2.0) or [0, 1). • ExponentE weights value by (possibly negative) power of two • Encoding • MSB s is sign bit s • exp field encodes E (but is not equal to E) • frac field encodes M (but is not equal to M)

  10. Carnegie Mellon Precisions • Single precision: 32 bits (float in C) • Double precision: 64 bits (double in C) • Extended precision: 80 bits (Intel only)

  11. Floating point encoding cases(depending on exp value) • Normalized values • Denormalized values • Special values

  12. Carnegie Mellon Normalized Values • exp field to encode Exponent E: (–1)sM 2E • Condition: exp ≠ 000…0 and exp ≠ 111…1 • Exponent coded as a signed biased value: E= exp– Bias • exp: unsigned value (ek-1..e0) • Bias = 2k-1 - 1, where k is number of exponent bits

  13. Carnegie Mellon Normalized Values • frac field to Encode M: (–1)sM 2E • M coded with implied leading 1: M = 1.xxx…x2 • xxx…x: bits of frac • Minimum when frac=000…0 -> M= 1.0 • Maximum when frac=111…1 ->M= 2.0 – ε • Get extra leading bit for “free”

  14. Normalized Encoding Example • Value: Float F = 15213.0; • 1521310 = 111011011011012 = 1.11011011011012x 213 • Significand M = 1.11011011011012 frac = 110110110110100000000002 • Exponent E = 13 Bias = 127 Exp = 140 = 100011002 • Result:0 10001100 11011011011010000000000 s exp frac

  15. Carnegie Mellon Denormalized Values • Condition: exp = 000…0 • Exponent value: E = –Bias + 1 (instead of E = 0 – Bias) • Significand coded with implied leading 0: M = 0.xxx…x2 • xxx…x: bits of frac • Cases • exp = 000…0, frac = 000…0 • Represents zero value • Note distinct values: +0 and –0 • exp = 000…0, frac ≠ 000…0 (E=-126 for a single precision) • Numbers very close to 0.0

  16. Carnegie Mellon Special Values • Condition: exp = 111…1 • Case: exp = 111…1, frac = 000…0 • Represents value (infinity) • Operation that overflows • Both positive and negative • E.g., 1.0/0.0 = −1.0/−0.0 = +, 1.0/−0.0 = − • Case: exp = 111…1, frac ≠ 000…0 • Not-a-Number (NaN) • Represents case when no numeric value can be determined • E.g., sqrt(–1), − , 0

  17. Carnegie Mellon Visualization: Floating Point Encodings − + −Normalized +Denorm +Normalized −Denorm NaN NaN 0 +0

  18. C bit-wise operations

  19. Bit-Level Operations in C • Operations (&:and, |:or, ^:xor, ~:not) • Apply to any “integral” data type • long, int, short, char, unsigned • View arguments as bit vectors • Arguments applied bit-wise • Examples (Char data type) • ~(not) • ~0x41 → 0xBE • ~010000012→ 101111102 • & (and) • 0x69 & 0x55 →0x41 • 011010012&010101012→010000012 • | (or) • 0x69 | 0x55 →0x7D • 011010012| 010101012→ 011111012 • ^(xor) • 0X03 ^ OX05 → 0x06 • 000000112^ 000001012 → 000001102

  20. Contrast: Logic Operations in C • Logic operations (&&: and, ||: or , !:not) • View 0 as “False” • Anything nonzero as “True” • Always return 0 or 1 • Lazy evaluation or early termination • Do not evaluate the second argument if the result can be determined from the first argument • Examples (char data type) • ! (Not) • !0x41 →0x00 • !0x00 →0x01 • !!0x41 → 0x01 • && (and) • 0x69 && 0x55 → 0x01 • || (or) • 0x69 || 0x55 →0x01 • NULL &&p (avoids null pointer access) • 0 && 5/0 → 0 (avoids division by zero)

  21. Argument X Argument X Arith. >> 2 Arith. >> 2 Log. >> 2 Log. >> 2 11101000 01100010 00010000 00011000 00011000 10100010 00010000 00101000 11101000 00101000 00010000 11101000 00101000 00010000 00011000 00011000 00011000 00010000 00010000 00011000 << 3 << 3 Shift Operations • Left Shift: x << y • Shift bit-vector x left y positions • Throw away extra bits on left • Fill with 0’s on right • Right Shift: x >> y • Shift bit-vector x right y positions • Throw away extra bits on right • Logical shift • Fill with 0’s on left • Arithmetic shift • Replicate most significant bit on right • (C under most of machines support arith. Shift)

More Related