Sunday, August 18, 2019

CS2100: Number representation

Base-R to Decimal Conversion

e.g
1101 (Base P) = 1´R3  +  1´R2  +  1´R0  

or

REMAINDER Method

Common Data Types Mapping

1. Common 
Byte = 8-bit
Character = 8-bit

2. 32 - bit Processor
Integer = 32 bit
Single-Precision Floating Point Number = 32-bit
Double-Precision Floating Point Number = 64-bit

3. 64 Bit processor
Integer = 64 bit
Single-Precision Floating Point Number = 64-bit
Double-Precision Floating Point Number = 128-bit

Integers (Both Positive and Negative)

Unsigned numbers: Only non neg values
Signed:
Have positive and negative
Represent with complement system

One sign but + 7 magnitude bit
The one is use to represent the negative bit

Largest Value: 01111111 = +127(base 10)
Smallest Value: 11111111 = -127 (Base 10)

1s Complement System

Flipping positive to negative:
Inverse everything
2 -> 010
-2 -> 101

nAlgorithm for addition, A + B:
1.Perform binary addition on the two numbers.
2.If there is a carry out of the MSB, add 1 to the result.
3.Check for overflow.
nOverflow occurs if result is opposite sign of A and B.
n
nAlgorithm for subtraction, A – B:
  A – B = A + (-B)
1.Take 1s-complement of B.
2.Add the 1s-complement of B to A.

Signs of overflow:
positive add positive -> negative
negative add negative -> positive

2s - Complement System

1. Keep the first 1 bit
2. Invert the rest

e.g
2-> 010
-2 -> 110

3-> 0011
-3 -> 1101

nAlgorithm for addition, A + B:
1.Perform binary addition on the two numbers.
2.Ignore the carry out of the MSB.
3.Check for overflow. Overflow occurs if the ‘carry in’ and ‘carry out’ of the MSB are different, or if result is opposite sign of A and B.
nAlgorithm for subtraction, A – B:
  A – B = A + (-B)
1.Take 2s-complement of B.
2.Add the 2s-complement of B to A.





Floating point numbers

Format: IEEE 754
We use this format so that we can get an equal amount of positive numbers and negative number. This is to "scale" the numbers.

There are 32 bits in total.

Start from the left
bit 1: Sign bit (0 for postive, 1 for negative)
bit 2 - 9 : Exponent bit (Excess 127)
Remaining bits : fraction bits

1. Convert fraction into binary
2. Move the decimal till the first 1 (ie 1.X, X is the fraction)
3. Split the bits accordingly:
 - Sign bit: Follow the sign
 - Exponent bit: Number of digits moved forward + 127 (convert to binary)
 - Fraction bit: X


<Prev                        Next>

No comments:

Post a Comment