0% found this document useful (0 votes)
26 views39 pages

cs303 ch1 BinarySystems

Uploaded by

berkay.akyurt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views39 pages

cs303 ch1 BinarySystems

Uploaded by

berkay.akyurt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
You are on page 1/ 39

Binary Systems

Logic and Digital System Design - CS 303


Atıl Utku Ay
Sabanci University
Binary Numbers 1/2
Internally, information in digital systems is of binary form
groups of bits (i.e. binary numbers)
all the processing (arithmetic, logical, etc) are performed on
binary numbers.
Example: 4392
In decimal, 4392 = 4×103 + 3×102 + 9×101 + 2×100.
Convention: write only the coefficients.
A = a1 a0 . a-1 a-2 a-3 where aj ∈ {0, 1, …, 9}
Binary Numbers 2/2
Decimal system
coefficients are from {0,1, …, 9}
and coefficients are multiplied by powers of 10
base-10 or radix-10 number system
Using the analogy, binary system {0,1}
base(radix)-2
Example: 25.625
25.625 = 2×101 + 5 ×100 + 6×10-1 + 2×10-2 + 5×10-3
25.625 = 1×24 + 1×23 + 1×20 + 1×2-1 + 1×2-3
25.625 = (11001.101)2
Base-r Systems
base-r (n, m)
A = an-1 rn-1 +… + a1 r1 + a0 r0 + a-1 r-1 + a-2 r-2 + … +a-m r-m
Octal
base-8 = base-23
digits {0,1, …, 7}
Example: (31.5)8 = octal expansion =
Hexadecimal
base-16
digits {0, 1, …, 9, A, B, C, D, E, F}
Example:
(19.A)16 = hexadecimal expansion =
Powers of 2
210 = 1,024 (K) -
220 = 1,048,576 (M) -
230  (G) -
240  (T) -
250  (P) -
exa, zetta, yotta, … (exbi, zebi, yobi, ...)
Examples:
A byte is 8-bit, i.e. 1 B
16 GB = ?? B = 17,179,869,184
Arithmetic with Binary Numbers

0 0 1 0 multiplicand (2)
× 1 0 1 1 multiplier (11)
0 0 1 0
0 0 1 0
0 0 0 0
+0 0 1 0
0 0 1 0 1 1 0 product (22)
Multiplication with Octal Numbers

3 4 5 229 multiplicand
× 6 2 1 401 multiplier
3 4 5
7 1 2
+2 5 3 6
2 6 3 2 6 5 91829 product
Base Conversions
From base-r to decimal is easy
expand the number in power series and add all the terms
Reverse operation is somewhat more difficult
Simple idea:
divide the decimal number successively by r
accumulate the remainders
If there is a fraction, then integer part and fraction part
are handled separately.
Base Conversion Examples 1/3
Example 1:
55
(decimal to binary)

Example 2:
144
(decimal to octal)
Base Conversion Examples 2/3
Example 1: 0.6875 (decimal to binary)
When dealing with fractions, instead of dividing by r multiply
by r until we get an integer
0.6875×2 = 1.375  1
0.375 ×2 = 0.750  0
0.750×2 = 1.5  1
0.5×2 = 1.0  1

0.6875 = (0.1011)2
Base Conversion Examples 2/3
We are not always this lucky
Example 2: (144.478) to octal
Treat the integer part and fraction part separately
0.478×8 = 3.824 = 3 + 0.824  a-1 = 3
0.824×8 = 6.592 = 6 + 0.592  a-2 = 6
0.592×8 = 4.736 = 4 + 0.736  a-3 = 4
0.736×8 = 5.888 = 5 + 0.888  a-4 = 5
0.888×8 = 7.104 = 7 + 0.104  a-5 = 7
0.104×8 = 0.832 = 0 + 0.832  a-6 = 0
0.832×8 = 6.656 = 6 + 0.656  a-7 = 6
144.478 = (220.3645706…)8
Conversions between Binary, Octal and
Hexadecimal
r = 2 (binary), r = 8 (octal), r = 16 (hexadecimal)

10110001101001.101100010111

10 110 001 101 001.101 100 010 111

10 1100 0110 1001.1011 0001 0111


Octal and hexadecimal representations are more
compact.
Therefore, we use them in order to communicate with
computers directly using their internal representation
Complement
Complementing is an operation on base-r numbers
Goal: To simplify subtraction operation
Rather turn the subtraction operation into an addition
operation
Two types
Radix complement (a.k.a. r’s complement)
Diminished complement (a.k.a. (r-1)’s complement)
When r = 2
1. 2’s complement
2. 1’s complement
How to Complement?
A number N in base-r (n-digit)
1. rn – N r’s complement
2. (rn-1) – N (r-1)’s complement
3. where n is the number of digits we use
Example: r = 2, n = 4, N = 7
rn = 24 = 16, rn -1 = 15.
2’s complement of 7  16-7 = 9
1’s complement of 7  15-7 = 8
Easier way to compute 1’s and 2’s complements
Use binary expansions
1’s complement: negate
2’s complement: negate + increment
Subtraction with Complements 1/4
Conventional subtraction
Borrow concept
If the minuend digit is smaller than the subtrahend digit, you
borrow “1” from a digit in higher significant position
With complements
M-N = ?
rn – N r’s complement of N
M + (rn – N) =
Subtraction with Complements 2/4

M-N  M + (rn – N)
M + (rn – N) = M – N + rn
1. if M ≥ N,
the sum will produce a carry, that can be discarded
2. Otherwise,
the sum will not produce a carry, and will be equal to rn – (N-
M), which is the r’s complement of N-M
Since M – N + rn = rn – (N-M)
Subtraction with Complements 3/4
Example:
X = 1010100 (84) and Y = 1000011 (67)
X-Y = ? and Y-X = ?

X 10101
2’s complement of Y + 00
01111
01
100100
01
Y 10000
2’s complement of X + 11
01011
00
011011
11
Subtraction with Complements 4/4
Example: Previous example using 1’s complement
Signed Binary Numbers
Pencil-and-paper
Use symbols “+” and “-”
We need to represent these symbols using bits
Convention:
0 positive1 negative
The leftmost bit position is used as a sign bit
In signed representation, bits to the right of sign bit is the
number
In unsigned representation, the leftmost bit is a part of the
number (i.e., the most significant bit (MSB))
Signed Binary Numbers
Example: 5-bit numbers
01011  (unsigned binary)
 (signed binary)
11011  (unsigned binary)
 (signed binary)
This method is called “signed-magnitude” and is rarely used in
digital systems (if at all)
In computers, a negative number is represented
by the complement of its absolute value.
Signed-complement system
positive numbers have always “0” in the MSB position
negative numbers have always “1” in the MSB position
Signed-Complement System
Example:
11 = (01011)
How to represent –11 in 1’s and 2’s complements
1’s complement - 11 = 10100
2’s complement - 11 = 10101
If we use eight bit precision:
11 = 00001011
1’s complement -11 = 11110100
2’s complement -11 = 11110101
Signed Number Representation

Issues: balance, number of zeros, ease of operations


Which one is best? Why?
Which One?
Signed magnitude:
Where to put the sign bit?
Adders may need an additional step to set the sign
There are two representations for 0.
Try to subtract a large number from a smaller one.
2=0010
5=0101
=1101
2’s complement provides a natural way to represent signed
numbers (every computer today uses two’s complement)
Think that there is an infinite number of 1’s in a signed number
-3 = 1101 ≡ …11111101
What is 11111100?
Arithmetic Addition
Examples:

+1 000010 -11 111101


1
+9 + 11
000010 +9 + 01
000010
01 01

+1 000010 -11 111101


1
-9 + 11
111101 -9 + 01
111101
11 11

No special treatment for sign bits


Arithmetic Overflow 1/2
In hardware, we have limited resources to accommodate
numbers
Computers use 8-bit, 16-bit, 32-bit, and 64-bit registers for the
operands in arithmetic operations.
Sometimes the result of an arithmetic operation get too large
to fit in a register.
Arithmetic Overflow 2/2
Example:

+ 011 - 110
7+ + 1
011 3- + 1
101
6 0 6 0

Rule: If the MSB and the bits to the left of it differ, then
there is an overflow
Subtraction with Signed Numbers
Rule: is the same
We take the 2’s complement of the subtrahend
It does not matter if the subtrahend is a negative number.
(±A) - (-B) = ±A+B

Signed-complement numbers are added and subtracted in the


same way as unsigned numbers
With the same circuit, we can do both signed and unsigned
arithmetic
Alphanumeric Codes
Besides numbers, we have to represent other types of
information
letters of alphabet, mathematical symbols.
For English, alphanumeric character set includes
10 decimal digits
26 letters of the English alphabet (both lowercase and
uppercase)
several special characters
We need an alphanumeric code
ASCII
American Standard Code for Information Exchange
Uses 7 bits to encode 128 characters
ASCII Code
7 bits of ASCII Code
(b6 b5 b4 b3 b2 b1 b0)2
Examples:
A  65 = (1000001), …, Z  90 = (1011010)
a  97 = (1100001), …, z  122 = (1111010)
048 = (0110000), …, 9  57 = (0111001)
128 different characters
26 + 26 + 10 = 62 (letters and decimal digits)
32 special printable characters %, *, $
34 special control characters (non-printable): BS, CR, etc.
Representing ASCII Code
7-bit
Most computers manipulate 8-bit quantity as a single
unit (byte)
One ASCII character is stored using a byte
One unused bit can be used for other purposes such as
representing Greek alphabet, italic type font, etc.
The eighth bit can be used for error-detection
parity of seven bits of ASCII code is prefixed as a bit to the ASCII
code.
A  (0 1000001) even parity
A  (1 1000001) odd parity
Detects one, three, and any odd number of bit errors
Binary Logic
Binary logic is equivalent to what it is called “two-valued
Boolean algebra”
Or we can say that it is an implementation of two-valued
Boolean algebra
Deals with variables that take on “two discrete values”
and operations that assume logical meaning
Two discrete values:
{true, false}
{yes, no}
{1, 0}
Binary Variables and Operations
We use A, B, C, x, y, z, etc. to denote binary variables
each can take on {0, 1}
Logical operations
AND  x · y = z or xy = z
OR  x+y=z
NOT  x = z or x’ = z
For each combination of the values of x and y, there is a
value of specified by the definition of the logical operation.
This definition may be listed in a compact form called truth
table.
Truth Table

x y AND OR NOT
x·y x+y x’
0 0 0 0 1

0 1 0 1 1

1 0 0 1 0

1 1 1 1 0
Logic Gates
Binary values are represented as electrical signals
Voltage, current
They take on either of two recognizable values
For instance, voltage-operated circuits
0V  0
4V  1
Electronic circuits that operate on one or more input
signals to produce output signals
AND gate, OR gate, NOT gate
Logic Gate Symbols
Logic Gate Symbols
NOT TRANSFER

AND XOR

O XNOR
R

NAND

NOR
Range of Electrical Signals
What really matters is the range of the signal value
Gates Operating on Signals
V
x 0 1 1 0 0
t

y 0 0 1 1 0
t

AND: 0 0 1 0 0
xy t

OR: 0 1 1 1 0
xy t

NOT: 1 0 0 1 1
x’ t
Input-Output Signals for
gates
Gates with More Than Two Inputs

You might also like