CS计算机代考程序代写 UART

UART

Signed and Fractional Values
Christopher Mar

Representing Signed Values
Relevant Textbook Section: 2.4

Two’s Complement
Used to represent negative numbers
If most significant bit (MSB) is 0, signed value is positive
Also equivalent to its unsigned value
If MSB is a one, signed value is negative
A conversion must be used to determine the magnitude of the number

Converting From Two’s Complement
Flip all bits (bitwise NOT)
Add the value, 1

Example Conversion
4-bit two’s compliment value, 0b1110
Flip bits:
0b0001
Add 1:
0b0010
This is equal to 2 so the 0b1110 is equal to -2

Example Conversion 2
What is the two’s compliment of 0b0010 (210)?
Flip bits:
0b1101
Add 1:
0b1110
This is the value we just determined was -210

Two’s Complement Table
Bits Signed Value Unsigned Value
011 3 3
010 2 2
001 1 1
000 0 0
111 -1 7
110 -2 6
101 -3 5
100 -4 4

The -1 contains all one’s. As the magnitude of the unsigned number decreases, the signed value becomes more negative.

Lecture Question
How would 7 (the positive value) be represented in 4-bit two’s complement?
1001
0111
1000
0001
1110

Lecture Question
How would -7 be represented in 4-bit two’s complement?
1001
0111
1000
0001
1110

Lecture Question
Are these signed values equivalent?
4-bit value, 1010, and 8-bit value, 11111010

Yes
No

Sign Extension
To convert a signed value from a smaller bit width to a large bit width (e.g. from 32-bit to 64-bit):
If positive, add zeros to the left
3 bit 0b010 becomes 5-bit 0b00010
If negative, add ones to the left
3 bit 0b110 becomes 5-bit 0b11110

Representing Fractions
Relevant Textbook Section: 3.5

Fractions in Binary

Powers of 2
22 4
21 2
20 1
2-1 0.5
2-2 0.25
2-3 0.125
2-4 0.0625
2-5 0.03125

Decimal to Binary
Separate number into whole and fractional part and:
Convert whole part (any method including divide-by-2 method we will discuss)
Use divide-by-2 method on fractional part, but with multiplication
Combine conversion results

Decimal to Binary
Example Conversion:
Convert 20.375 to binary

Divide-by-2 Method (Whole)
Tabulation method
Track remainders while repeatedly dividing decimal value by 2
Binary value is the list of remainders in opposite order

Decimal to Binary
Example Conversion:
Convert 20.375 to binary
Whole part: 20

Divide-by-2 Method
Decimal Remainder
20

19

Divide-by-2 Method
Decimal Remainder
20 0

Divide-by-2 Method
Decimal Remainder
20 0
10

Divide-by-2 Method
Decimal Remainder
20 0
10 0

Divide-by-2 Method
Decimal Remainder
20 0
10 0
5

Divide-by-2 Method
Decimal Remainder
20 0
10 0
5 1

Divide-by-2 Method
Decimal Remainder
20 0
10 0
5 1
2

Divide-by-2 Method
Decimal Remainder
20 0
10 0
5 1
2 0

Divide-by-2 Method
Decimal Remainder
20 0
10 0
5 1
2 0
1

Divide-by-2 Method
Decimal Remainder
20 0
10 0
5 1
2 0
1 1

2010 = 101002

“Divide-by-2” Method (Fraction)
Multiply by fractional part by 2
The whole part of each product is appended to the current fractional part of the binary result (after the binary point)
Whole part is ignored in next multiplication
Continue until fractional part is 0

Decimal to Binary
Example Conversion:
Convert 20.375 to binary
Whole part: 20 = 101002
Fractional part: .375

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375

31

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375 0

32

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375 0
.75

33

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375 0
.75 1

34

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375 0
.75 1
.5

35

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375 0
.75 1
.5 1

36

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375 0
.75 1
.5 1
.0

37

“Divide-by-2” Method (Fraction)
Decimal Binary Fraction
.375 0
.75 1
.5 1
.0 Done

0.37510 = 0.0112

38

Decimal to Binary
Example Conversion:
Convert 20.375 to binary
Whole part: 20 = 101002
Fractional part: .375 = .0112
Combine: 10100.0112

Decimal to Binary
Example Conversion:
Convert 20.375 to binary
Whole part: 20 = 101002
Fractional part: .375 = .0112
Combine: 10100.0112

Lecture Question
How would 5.75 be represented in binary?
11.11
101.101
111.111
101.11
101.011

Limitations of Binary Fractions
Some decimal fractions require an infinite number of bits to represent them
With a finite number of bits we can only get close to these fractions
Example: 0.110
In binary: 0.000110011001100…

Limitations of Binary Fractions
Number of Bits Binary Value
1 0
2 0
3 0
4 0.0625
5 0.09375
6 0.09375
7 0.09375
8 0.09765625
9 0.099609375

Limitations of Binary Fractions
With a finite number of bits, the position of the decimal point limits either:
Magnitude of whole part
Accuracy of fractional part
Accuracy == closeness to true value
Precision == closeness between results
A binary representation is precise, but not always accurate

Floating Point
IEEE 754
Scientific notation with specific number of bits for each part
-4.539 * 107

Sign [31] Exponent [30:23] Mantissa [22:0]

Sign
Mantissa
Base
Exponent

Floating Point vs. Fixed Point
Exponent allows binary point to move relative to zero position, or “float”
Having a set number of bits to represent the fractional part of a number is referred to as fixed point

IEEE 754 Details
Multiple precisions (total number of bits)
Single (32-bit) and double (64-bit) are most common, but ranges from half to octuple
Binary scientific notation will always have a 1 before the mantissa (binary point)
Saves one bit in representation

IEEE 754 Details
Base will be 2 (rather than 10)
Exponent has bias:
Bias subtracted from value of exponent field
27 – 1 = 127 (single precision)
210 – 1 = 1023 (double precision)
More details in textbook page 246

Limitations of Floating Point
Very large numbers (large exponent) have limited accuracy
Result of 1.0*230 + 1 will be represented in floating point as 1.0*230
For addition and subtraction, results become more meaningful the closer the exponents