220 likes | 235 Views
This chapter explains binary arithmetic, including addition, subtraction, multiplication, and operations with negative numbers, including two’s complement. It also covers ASCII codes and BCD representation. Learn various techniques and practical examples to work with binary numbers efficiently.
E N D
Chapter 6 Binary Arithmetic
Binary Addition • Binary addition is very simple. • This is best shown in an example of adding two binary numbers… 1 1 1 1 1 1 carries 1 1 1 1 0 1 + 1 0 1 1 1 --------------------- 1 0 1 0 1 0 0
Binary addition example worked out • Some terms are given here • Exercise: what are these numbers equivalent to in decimal? The initial carry in is implicitly 0 1 1 1 0 (Carries) 1 0 1 1 (Augend) + 1 1 1 0 (Addend) 1 1 0 0 1 (Sum) most significant bit (MSB) least significant bit (LSB)
1 10 0 10 10 0 0 10 1 0 0 1 1 0 1 - 1 0 1 1 1 ------------------------ 1 1 0 1 1 0 borrows Binary Subtraction • We can also perform subtraction (with borrows in place of carries). • Let’s subtract (10111)2 from (1001101)2…
Ignore carry Negative Numbers Subtract by adding 73 -35 38 73 +65 138 10’s complement
Two’s Complement Shortcuts • Algorithm – Simply complement each bit and then add 1 to the result. • Finding the 2’s complement of (01100101)2 and of its 2’s complement… N = 01100101 [N] = 10011011 10011010 01100100 + 1 + 1 --------------- --------------- 10011011 01100101
Negative Numbers Subtract by adding 73 -35 38 0 1 0 0 1 0 0 1 1 1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 • 01001001 • 35 00100011 • 2’s comp 11011100 flip • +1 • ----------- • -35 11011101
Signed Numbers 4-bit: 8H = -8 to 7H = +7 1000 to 0111 8-bit: 80H = -128 to 7F = +127 16-bit: 8000H = -32,768 to 7FFFH = +32,767 32-bit: 80000000H = -2,147,483,648 to 7FFFFFFFH = +2,147,483,647
Questions What is the two’s complement of 00101100? What hex number represents the decimal number -40?
Binary Multiplication • Binary multiplication is much the same as decimal multiplication, except that the multiplication operations are much simpler… 1 0 1 1 1 X 1 0 1 0 ----------------------- 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 1 0 1 1 1 ----------------------- 1 1 1 0 0 1 1 0
ASCII • The most commonly used code for representing letters, numerals and punctuation characters (alphanumeric data) • Each character is represented with a 7-bit string; for example: • ‘3’ = 00110011 (hex 33) • ‘ ’ = 00100000 (hex 20) • An 8-bit extension of ASCII has also been defined
ASCII Code • American Standard Code for Information Interchange • ASCII is a 7-bit code, frequently used with an 8th bit for error detection (more about that in a bit).
ASCII Properties Q1: What is relationship between a decimal digit (0, 1, …) and its ASCII code?
ASCII Properties (2) Q2: What is the difference between an upper-case letter (A, B, …) and its lower-case equivalent (a, b, …)?
Binary Coded Decimal • Binary coded decimal (BCD) represents each decimal digit with four bits • Ex. 0011 0010 1001 = 329BCD • This is NOT the same as 0011001010012 • Why do this? Because people think in decimal. 3 2 9
Putting It All Together • BCD not very efficient • Used in early computers (40s, 50s) • Used to encode numbers for seven-segment displays. • Easier to read?