2's Complement and Floating-Point, Slides of Number Theory

An easier way to find the decimal value of a two's complement number: ~x + 1 = -x. • We can rewrite this as x = ~(-x - 1), i.e. subtract 1 from the given ...

Typology: Slides

2022/2023

Uploaded on 03/01/2023

agrata
agrata 🇺🇸

4

(7)

258 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2’s$Complement$and$
Floating-Point
CSE$351$Section$3
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download 2's Complement and Floating-Point and more Slides Number Theory in PDF only on Docsity!

2’s Complement and

Floating-Point

CSE 351 Section 3

Two’s Complement

  • An n-bit, two’s complement number can represent the range [−

, 2

− 1].

  • Note the asymmetry of this

range about 0 – there’s one more

negative number than positive

  • Note what happens when you overflow 4 - bit two’s complement range

Two’s Complement: Operations

  1. Convert numbers to binary
    • 0xAB = 0b
    • 1710 = 0b
  2. Compute x | y 0000 ... 1010 1011 | 0000 ... 0001 0001

0000 ... 1011 1011

  1. Compute ~(x | y) (flip the bits) ~ 0000 0000 0000 0000 0000 0000 1011 1011 ----------------------------------------- 1111 1111 1111 1111 1111 1111 0100 0100 int x = 0xAB; int y = 17; int z = 5; int result = ~(x | y) + z; What is the value of result in decimal?

Two’s Complement: Operations

4. Compute ~(x | y) + z

  • You can either convert to decimal before or after adding z
  • 1710 = 0b
  • Convert 111111111111111111111111010001002 to decimal (hint: - x = ~x + 1)
  • ~11111111111111111111111101000100 2 = (^0000000000000000000000000101110112)
  • ~0000……10111011 2 + 1 = 0000…… (^101111002)
    • x = 0000……10111100 2
  • x = 1000……10111100 2 int x = 0xAB; int y = 17; int z = 5; int result = ~(x | y) + z; What is the value of result in decimal?

Floating Point

  • value = (-1) S * M * 2 E
  • Value: ± 1 × Mantissa × 2 Exponent
  • Bit Fields: (-1) S × 1. M × 2 ( E +bias)
  • Bias
    • Read exponent as unsigned, but with bias of – ( w-
  • Representable exponents roughly ½ positive and ½ negative
  • Exponent 0 (Exp = 0) is represented as E = 0b 0111 1111
  • Why?
  • Floating point arithmetic = easier
  • Somewhat compatible with 2’s complement s exp mant

Floating Point

  • Exponent overflow yields +∞ or - ∞
  • Floats with value +∞, - ∞, and NaN can be used in operations
  • Result usually still +∞, - ∞, or NaN; sometimes intuitive, sometimes not
  • Floating point ops do not work like real math, due to rounding!
  • Not associative:
    • (3.14 + 1e100) – 1e100 != 3.14 + (1e100 – 1e100)
  • Not distributive:
    • 100 * (0.1 + 0.2) != 100 * 0.1 + 100 * 0.
  • Not cumulative
    • Repeatedly adding a very small number to a large one may do nothing s exp mant

Floating Point Examples

  • How do you represent 1.0 in floating point?
    • Sign bit: 0
    • First the integral part of the value: 1 = 0b
    • 1.0 10 => 1.0b
    • Don’t need to normalize because it’s already in scientific notation: 1.0 x 2 0
    • Exponent: 0 + 127 = 127 10 = 01111111 2
    • Mantissa: 00000000000000000000000 2
    • 1.010 = 0 01111111 00000000000000000000000 2

= 0x3F

s exp mant 1 8 23

Floating Point Addition (–1) s M1 E

  • (-1) s M2 E (Assume E1 > E2)
  • Exact Result: (–1) s M E
  • Sign s, mantissa M:
  • M = M1 + M2, result of signed align & add
  • Exponent E: E
  • Fixing
    • If M ≥ 2, shift M right, increment E
    • if M < 1, shift M left k positions, decrement E by k
    • Overflow if E out of range
    • Round M to fit frac precision s exp mant 1 8 23

What is floating point result of 12.3125 + 1.5?

In scientific notation: (1.1000101 x 2

3

) + (1.1 x 2

0

1100.0101 x 2 0

  • 1.1 x 20

1101.1101 x 20

Adjusted mantissa: 1.1011101 => M = 1011101

Ans: 0 10000010 10111010000000000000000

Floating Point Worksheet