Download Decimal and Binary Numbers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Binary Representation
and Computer
Arithmetic
DECIMAL and BINARY system

The Decimal System
 This uses ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
to represent numbers

The Binary System
 uses just two digits, 0 and 1
 easier for a computer to distinguish between
two different voltage levels (than ten)
POSITIONAL NOTATION (in decimal)
Consider the decimal number 5237.
Positional
thousands hundreds
Name
tens
units
Positional
103
Weight
102
101
100
Value
1 000
100
10
1
5
2
3
7
5 237
5 x 1 000 + 2 x 100 + 3 x 10 + 7 x 1
POSITIONAL NOTATION (in binary)
Consider the byte 1100 0101
Positional
Weight
27
26
25
24
23
22
21
20
Value
128
64
32
16
8
4
2
1
Number
1
1
0
0
0
1
0
1
Positional
Notation
1100 0101 = 1x128 + 1x64 + 0x32 + 0x16 + 0x8 +
1x4 + 0x2 + 1x1 = 197
Note the LSD in 1100 0101 is the right most digit which is 1
and the MSD is the left most digit which is 1.
Representing Integers in Binary
ASCII codes are pure or true binary
numbers. By that we mean there is no
positive or negative sign attached to these
numbers.
We are now going to look at representing
integers in binary.
Representing Integers in Binary
Here are some of the challenges we face when we move to
representing integers in binary:
1. Integers can be positive or negative.
2. Must have a unique representation.
3. Should be able to add and subtract them
4. Operations must be efficient ( using the
smallest number of circuits possible)
INTEGER
REPRESENTATIONS
There are 4 commonly known (1 not common) integer
representations. All have been used at various times for various
reasons.
1. unsigned
2. sign magnitude
3. one's complement
4. two's complement
5. biased (not commonly known)
UNSIGNED BINARY NUMBER
SYSTEM
The standard binary encoding already given
 only positive values
 range: 0 to 2**n - 1, for n bits
example:
4 bits, values 0 to 15
n=4, 2**4 -1 is 15

Sign-Magnitude Binary Number System


The sign and magnitude number system is a
simple binary code system used to represent
positive and negative integers.
In this system, the first bit (the MSB) in a
binary representation is a sign bit (0 for
positive and 1 for negative) followed by
the magnitude bits.
Sign-Magnitude Binary Number System
Example
0101 represents the number +5.

The most significant bit (MSB) =0. The 0 indicates the
number is positive.

The remaining 3 bits represent the magnitude of the
number. The decimal value of binary 101 is 4 + 0 + 1 = 5.

Therefore, 0101, using a 4-bit sign-magnitude convention
is the integer +5
PROBLEM
How many positive binary numbers can be
represented in a nibble using the signmagnitude notation?
How many positive binary numbers can be
represented in a byte using the signmagnitude notation?
Representation of Negative Integers using
Sign-Magnitude Notation
Example

When an 8-bit sign-magnitude convention for
representing numbers is used, the number
1011 0111 represents the negative integer -55.

The first bit, the MSB, is 1 indicating the integer is
negative.

The next 7 bits 011 0111 represent the magnitude of the
integer.
Binary 011 0111 has decimal value 32 + 16 + 4 + 2 + 1 =
55.
The 1's Complement form

The 1's complement representation in binary of a
positive integer is same as the sign-magnitude
representation of that integer.
Example
Using a byte, the 1's complement in binary
of +84 is 0101 0100.
The 1's Complement in Binary of a
Negative Integer
Rule:
The 1's complement in binary of a negative
integer is obtained by subtracting its
magnitude from 2n -1 where n is the number
of bits used to store the integer in binary.
BINARY ARITHMETIC
BINARY ADDITION
Rule 1
0
+0
0
Rule 2
0
+1
1
Rule 3
1
+0
1
Rule 4
Rule 5
1
+1
10
1
1
+1
11
BINARY ADDITION
View online lesson
BINARY ADDITION
Example
 Add these binary numbers:

1100 (12)+

+ 1101 (13)

1 1001 (25)
BINARY SUBTRACTION
Rule 1
Rule 2
Rule 3
Rule 4
0
-0
0
1
-1
0
1
-0
1
0
-1
1
BINARY SUBTRACTION
View online lesson
BINARY SUBTRACTION
Example




Subtract these binary numbers:
1101 (13)+ 0110 ( 6)
0111 (7)
The 1's Complement in Binary of a
Negative Integer
Example
Store the integer -36 in a byte in 1's complement form.
Step 1:
+3610 = 0010 01002
[convert the magnitude of the integer to binary]
Step 2:
1111 1111
- 0010 0100
1101 1011
[A byte contains 8 bits. Therefore, subtract 0010 0100
from 28-1= 255. In binary 255 is 1111 1111]
EASY WAY
Determine the 1's complement of a negative integer

Determine the 1's complement of the
corresponding positive integer

invert all bits.
EXAMPLE
Using a byte, find the 1's complement of -57.

+57 stored in a byte in 1's complement form
is 0011 1001.

Invert all bits to determine the 1's
complement of -57 to be 1100 0110.
Two's Complement Notation
The 2's Complement in Binary of a Positive
Integer

same as sign-magnitude notation
Two's Complement Notation
The 2's Complement of a Negative Integer
1.
2.
3.
First, we write the positive value of the number
in binary.
Next, we reverse each bit to get the 1's
complement.
Last, we add 1 to the number.
Example
Store -27 in a byte using 2's complement notation.

Step 1: +27 in binary is 0001 1011.

Step 2: Invert bits to yield 1110 0100. Then the 1's complement of -27 is
1110 0100.

Step 3: Add 1 to the 1's complement:
1110 0100
+
1
1110 0101
The 2's complement of -27 is 1110 0101.
Binary Arithmetic


Subtraction with one’s complement
Subtraction with two’s complement
Fractions
Converting a fractional decimal value to binary
Floating Point Arithmetic
Floating point arithmetic derives its name from something
that happens when you use exponential notation.
Consider the number 123: it can be written using
exponential notation as:
 1.23 * 10 2
 12.3 * 10 1
 123 * 10 0
 .123 * 10 3
 1230 * 10 -1
Floating Point Arithmetic
All of these representations of the number 123
are numerically equivalent.
 They differ only in where the decimal point
appears in the number.
The decimal point "floats" within the number as
the exponent is changed. This phenomenon
gives floating point numbers their name.

Floating Point Arithmetic

The first representation, 1.23 * 10 2, is in a form
called "scientific notation“

In scientific notation, the significand is always a
number greater than or equal to 1 and less than
10. (normalization)
Floating Point Number
A floating-point number is often stored as three parts:
 A significand or mantissa (indicating the digits that
define the number's magnitude)
 An exponent or scale (indicating the position of the
radix point)
 A sign (indicating whether the number is positive or
negative)
Floating Point Arithmetic
The exponent does not always indicate a power of ten; sometimes
powers of sixteen are used instead, or, most commonly of all,
powers of two
The real number -0.125 is equal to
 -1.25 x 10^-1 if powers of ten are used,
 -2 x 16^-1 if powers of sixteen are used,
 -1 x 2^-3 if powers of two are used.
The absolute value of the mantissa is, however, always
greater than or equal to 1 and less than the base of
numeration.
Floating Point Formats

Over the years, floating point formats in
computers have not exactly been standardized.
While the IEEE (Institute of Electrical and
Electronics Engineers) has developed standards
in this area, they have not been universally
adopted.
In the IEEE single-precision
representation of a real number



One bit is reserved for the sign, and it is set to 0
for a positive number and to 1 for a negative
one.
A representation of the exponent is stored in
the next eight bits.
The remaining twenty-three bits are occupied by
a representation of the mantissa of the number.
IEEE 754 Standard (32 bits)
31
30
29
28
27
26
25
24
23
22
21
2
Sign bit
Exponent
Mantissa
(1 bit)
(8 bits)
(23 bits)
1
0
IEEE 754 Standard (32 bits)



Sign bit = 0 or 1
The exponent, which is a signed integer in the range
from -126 to 127, is represented as a biased value. (127 is
the bias used)
Only the part of the mantissa that comes after the
binary point is actually stored. This suppressed digit at
the beginning of the mantissa is called the ``hidden bit.''
Floating Point Arithmetic

In a binary computer, all numbers are stored in
base 2 instead of base 10

The absolute value of the mantissa is, however,
always greater than or equal to 1 and less than 2.
EXAMPLE
Find the IEEE single-precision representation of
5.75.
 The sign bit is 0, since the number is positive
 (5) = (101)
 To convert 0.75 do repeated multiplication
10
2
EXAMPLE

.75 X 2 = 1.5 -------- 1

.5X2 = 1.0 ----------1

0 ------ (stop)
(.75) = (.11)
10
2
EXAMPLE
(5.75)10 = (101.11)2

Normalizing 101.11 --- 1 . 0111 X 22
Hidden bit


mantissa
Adding bias to the exponent
127+2 = 129
(129)10 = ( 1000 0001 )2
Exponent
And the result….
Lining up, we get
0 10000001 01110000000000000000000
More Examples
Convert from Decimal to Binary