03.1 Numbers, Binary
03.1 Numbers, Binary
1101 → 1 1 0 1
𝑥𝑥 = 0
𝑥𝑥 = 2(0) + 1 = 1
𝑥𝑥 = 2(1) + 1 = 3
𝑥𝑥 = 2 3 + 0 = 6
𝑥𝑥 = 2 6 + 1 = 13
Interpreting binary numbers
• Double-dabble algorithm
̶ initialize a decimal register 𝑥𝑥 = 0
̶ for each digit in the binary number, double 𝑥𝑥 and add the binary value
0110 1010 → 0 1 1 0 1 0 1 0
𝑥𝑥 = 0
𝑥𝑥 = 0
𝑥𝑥 = 1 𝑥𝑥 = 6 𝑥𝑥 = 26 𝑥𝑥 = 106
𝑥𝑥 = 3 𝑥𝑥 = 13 𝑥𝑥 = 53
Converting Decimal → Binary
• Remainder method
1) continuously divide a number by 2
2) the binary representation is given by the sequence of remainder values
0 1
2 1 1
1310 = 11012
2 3 0
2 6 1
2 13
Signed Values
• Mathematical expressions 𝑥𝑥 often take on negative values 𝑥𝑥 < 0
• Consider representing the number:
−1710 = − 0001 0001 2
̶ how could this value be represented in a binary register?
• Sign and magnitude: leading bit represents the sign
̶ 0 indicates a positive (+) value, 1 indicates a negative (−) value
1101. 1 0 1 1
1) Convert the integer component
1101. 1011
2) Convert the fractional bits 11
13 11 13
16
3) The second set of bits provide the numerator for the fractional
component
Binary Arithmetic
• Arithmetic in any base can be performed using traditional methods:
• Examples: 73 mod 7 = 3
13 mod 3 = 1
15 mod 5 = 0
• Common in numerical methods (ex. generating random numbers)
Overflow – unsigned integers
• Overflow: 1111 1111+1=1 0000 0000
8 bits 9 bits
• Overflow behavior is defined for unsigned integer values
1111 1101
+ 0001 0101
1 0001 0010 0001 0010
• This truncation is equivalent to the modulus operation: 𝑥𝑥 mod 2𝑛𝑛
̶ where 𝑛𝑛 is the number of bits in the truncated representation
1111 1101 253
+ 0001 0101 + 21
1 0001 0010 0001 0010 274 mod 28 = 1810 = 0001 0010