Sie sind auf Seite 1von 40

Binary Arithmetic

Binary Addition

Binary Subtraction

Binary Multiplication

Binary Division
Binary Addition

The rules for binary addition are


0+0= 0 Sum = 0, carry = 0
0+1= 0 Sum = 1, carry = 0
1+0= 0 Sum = 1, carry = 0
1 + 1 = 10 Sum = 0, carry = 1

When an input carry = 1 due to a previous result, the


rules are
1 + 0 + 0 = 01 Sum = 1, carry = 0
1 + 0 + 1 = 10 Sum = 0, carry = 1
1 + 1 + 0 = 10 Sum = 0, carry = 1
1 + 1 + 1 = 11 Sum = 1, carry = 1
Examples
10101 111
+ 110 + 111
________ ________

10011
+ 1111101
_________
Binary Subtraction

The rules for binary subtraction are


00=0
11=0
10=1
10 1 = 1 with a borrow of 1

Subtract the binary number 00111 from 10101 and show the
equivalent decimal subtraction.
111
10101
/ / / 21
00111 7
01110 = 14
Examples
1011011
- 10010
__________

1010110
- 101010
__________
Binary Multiplication

The rules for binary


multiplication are:

00=0
01=0
10=0
11=1
Binary Division

Binary division is performed in the same manner as


decimal division, as demonstrated below.
Examples
111 x 101

1101 / 11

1001010/1000
Octal Addition
(162)8 + (537)8

(136)8 + (636)8

(67.5)8 + (45.6)8
Hexadecimal Addition
(4A6)16 + (1B3)16

(BA3)16 + (5DE)16
Complement of numbers
Radix complement: rs complement
Diminished Radix complement: (r-1)s
complement

Binary numbers: 2s complement and 1s


complement
1s Complement

The 1s complement of a binary number is the number that


is formed by reversing (inverting) all the digits. To form the
1s complement, change all 0s to 1s and all 1s to 0s.

For example, the 1s complement of 11001010


is: 00110101
In digital circuits, the 1s complement is formed by using
inverters:
1 1 0 0 1 0 1 0

0 0 1 1 0 1 0 1
2s Complement

The 2s complement of a binary number is found by


adding 1 to the LSB of the 1s complement.
Recall that the 1s complement of 11001010 is
00110101 (1s complement)
For the 2s complement, add 1: +1
1 1 0 0 1 0 1 0 00110110 (2s complement)
1

0 0 1 1 0 1 0 1

0 0 1 1 0 1 1 0
Find the 1s and 2s complement of

1101100
0010011 <- 1s complement
0010101 <- 2s complement
Signed Binary numbers
In the real world of mathematics, computers
must represent both positive and negative
binary numbers.
Even when dealing with positive arguments,
results may be negative!
124- 237 = 113.

Hence consistent method required to represent


sign.
1 digit (bit) of the binary number is utilized to
represent sign.
3 methods to represent
signs
Sign magnitude

1s complement

2s complement

** Representation using all these 3 methods


requires 1 bit more than what is normally
used.
Sign Magnitude numbers
MSB represents the sign
0 positive
1 negative

Represent +25 as an 8-bit signed number

Represent -39 as an 8-bit signed number


Signed vs unsigned
Determine the decimal value of this
signed binary number: 1101
(What is the range of signed numbers that can be
represented with 4 bits?)

Determine the decimal value of this


unsigned binary number: 1101
(What is the range of unsigned numbers that can be
represented with 4 bits?)
Sign Magnitude numbers
Advantage:
Simple representation of +/- numbers

Disadvantage:
Increased number of bits
Can result in +0 and -0 which is illogical.
Can make Math more difficult since the
first bit needs to be ignored in
calculations
Arithmetic with sign-magnitude
numbers
If signs are the same:
Add both magnitudes and give answer the
same sign.

If signs are different:


Subtract smaller from larger number, give
answer the sign of the larger magnitude.
1s complement
Ones complement: The negative number of the
same magnitude as any given positive number is its
ones complement.

If +5 = 0101
-5 = 1010

Look at MSB to determine if it is representing a


positive or a negative number.
MSB is 0 for positive binary numbers
1 for negative numbers.
1s complement
Again, easy to implement

But,
if m 00000000 then its 1s
complement is 11111111;
there are two zeros in this method as
well.
2s complement
Overcoming the problems faced by the
previous 2 methods, this is the most
preferred method.

If +5 = 0101
-5 = 1011
Look at MSB to determine if it is representing
a positive or a negative number.
2s complement
Another way to understand this:

Assign a ve weight to the MSB, and write the binary


equivalent as you normally would (i.e. the sum of
the weights should give you the decimal equivalent)
So for -5,
-8 4 2 1

1 0 1 1 = -8 + 2 + 1 = -5

This method is also called Signed 2s complement


representation

Here, we get only 1 unique representation for 0.


Decoding numbers with extra (filler)
bits
Lets say you have an 8 bit processor
+12 is represented as 00001100
For representing -12:
+12: 00001100
1s comp: 11110011
2s comp: 11110100 Hence -12 : 11110100

For decoding:
Look for a continuous trail of 1s and consider only the rightmost 1 as the
sign bit, and perform 2s complement for the numbers to the right of that 1.

To interpret it using negative weights: -16 8 4 2 1

0 0 0 1 0 1 0 0 = -16 + 4 = -12
Comparison between different
representations
Addition

Rules for addition: Add the two signed numbers. Discard


any final carries. The result is in signed form.

Examples:
00011110 = +30 00001110 = +14 11111111 = 1
00001111 = +15 11101111 = 17 11111000 = 8
00101101 = +45 11111101 = 3 1 11110111= 9
Subtraction
Rules for subtraction: 2s complement the subtrahend and add the
numbers. Discard any final carries. The result is in signed form.

Repeat the examples done previously, but subtract:


00011110 (+30) 00001110(+14) 11111111 (1)
00001111 (+15) 11101111
(17) 11111000 (8)

2s complement subtrahend and add:


00011110 = +30 00001110 = +14 11111111 =
11110001 = 15 00010001 = +17 1
1 00001111 = +15 00011111 = +31 1 00001000
==
00000111 +7
+8
Overflow
Implies the answer value has exceeded the
magnitude that can be represented by the allotted
number of bits.
Results in overflow error, meaning that the number
of bits in use cannot represent the answer.
Two examples are:

010001 = +17 101111 = 17


010011 = +19 101101 = 19
100100 =28 Discard carry 1011100 = +28

Wrong! The answer is incorrect


and the sign bit has changed.
Trick to determine if your answer is
correct or wrong
If 2 ve numbers are added, result should be ve
If 2 +ve numbers are added, result should be +ve

So if we have 17 + 19, we should get a +ve answer.


Here 010001
the leading
= +171 implies that the answer is ve, and the 2s
complement
010011 =needs
+19 to be taken to interpret the answer. This is
wrong
100100 =28
Correct answer = 36 (outside limits)
If we have (-17)+(-19) we should get a ve answer
Here the leading 0 implies that the answer is +ve, and is to be
interpreted
101111 the way it is. This is wrong.
= 17
101101 = 19 Correct answer = -36
Discard (outside limits)
carry 1011100 = +28

This problem will never arise if we have 2 numbers with


opposite signs, because they will never exceed the limit.
Codes: Binary coded decimal
Decimal Binary BCD
0 0000 0000
1 0001 0001
2 0010 0010
Binary coded decimal (BCD) is a
3 0011 0011
weighted code that is commonly used 4 0100 0100
in digital systems, such as clock 5 0101 0101
displays, where it is necessary to show 6 0110 0110
decimal numbers. 7 0111 0111
8 1000 1000
The table illustrates the difference 9 1001 1001
between straight binary and BCD. BCD 10 1010 0001 0000
represents each decimal digit with a 4-bit 11 1011 0001 0001
code. Notice that the codes 1010 through 12 1100 0001 0010
1111 are not used in BCD. 13 1101 0001 0011
14 1110 0001 0100
15 1111 0001 0101
Codes: Gray Code
Decimal Binary Gray code
0 0000 0000
1 0001 0001
2 0010 0011
Gray code is an unweighted 3 0011 0010
code that has a single bit change 4 0100 0110
between one code word and the 5 0101 0111
next in a sequence. Gray code is 6 0110 0101
7 0111 0100
used to avoid problems in 8 1000 1100
systems where an error can 9 1001 1101
occur if more than one bit 10 1010 1111
changes at a time. 11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
Gray Code application: Shaft Encoder
A shaft encoder is a typical application. Three IR emitter/detectors are
used to encode the position of the shaft. The encoder on the left uses
binary and can have three bits change together, creating a potential
error. The encoder on the right uses gray code and only 1-bit changes,
eliminating potential errors.

Binary sequence Gray code sequence


Codes: ASCII
ASCII is a code for alphanumeric characters and control
characters. In its original form, ASCII encoded 128
characters and symbols using 7-bits. The first 32
characters are control characters, that are based on
obsolete teletype requirements, so these characters are
generally assigned to other functions in modern usage.

In 1981, IBM introduced extended ASCII, which is an 8-


bit code and increased the character set to 256. Other
extended sets (such as Unicode) have been introduced
to handle characters in languages other than English.
Parity Method for Error Detection
The parity method is a method of error detection for simple
transmission errors involving one bit (or an odd number of
bits).

A parity bit is an extra bit attached to a group of bits to


force the number of 1s to be either even (even parity) or odd
(odd parity).

Odd parity:
T
Suppose you receive a binary bit
word 0101 and you know you are
using an odd parity.
Is the binary word correct?

Add odd parity bit Add even parity bit


1000000( ) 1010010( )
1111101( ) 1000111( )
1001001( ) 1011001( )
The ASCII character for a is 1100001 and for A is 1000001.
What is the correct bit to append to make both of these have odd
parity?

The ASCII a has an odd number of bits that are equal to 1;


therefore the parity bit is 0. The ASCII A has an even number
of bits that are equal to 1; therefore the parity bit is 1.
Parity Method for Error Detection

Advantage: Simple method for detecting


bit errors
Disadvantage: Can detect only 1-bit
errors. May not be able to detect multiple
bit errors.
Cyclic Redundancy Check
The cyclic redundancy check (CRC) is an error detection method that
can detect multiple errors in larger blocks of data. At the sending end, a
checksum is appended to a block of data. At the receiving end, the
checksum is generated and compared to the sent checksum. If the check
sums are the same, no error is detected.

Das könnte Ihnen auch gefallen