Download ppt

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

Proofs of Fermat's little theorem wikipedia , lookup

Addition wikipedia , lookup

Location arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
CMPT 120
Topic: Binary Encoding – Part 2
Learning Outcomes
In terms of Learning Outcomes
• At the end of this course, a student is expected
to:
• Manipulate binary encodings of simple data
types.
• Do conversions from binary system to decimal and
vice versa
• Determine the range of representable values within
binary systems
• Do additions and incremental counting within
binary systems
• Decode/encode a bit string to/from its
corresponding value (unsigned, 2's complement,
ASCII)
2
Last Lecture
• Binary encoding
• How characters are represented in memory
• Binary and decimal numeral systems
• Incremental counting and adding in those numeral
systems
• Converting binary numbers <-> decimal numbers
Binary numeral
system
Hexadecimal
numeral
system
Decimal numeral
system
Octal numeral
system
3
Today’s Menu
• Binary encoding
• How integers are represented in memory
• Unsigned
• Signed
• Sign and magnitude
• 1’s complement
• 2's complement
• How floating point numbers are represented in
memory
• How many values (range) can they represent
4
How is data represented in
the computer memory?
2. Integer
• Representing an integer in memory is often called
fixed point representation (or notation)
• Stored in memory location of size -> multiple of 8
bits (1 byte)
• For example:
• 16-bit
• 32-bit
• 64-bit
• Two types of integers: unsigned and signed
5
Unsigned integer
• What is an unsigned integer?
• When would we use an unsigned integer in our
programs?
• How is an unsigned integer represented in
memory?
6
Range of an unsigned integer
• What if we store an integer in a 8-bit memory
location. How many values can this integer have?
7
Range of an unsigned integer
• Range of an unsigned integer stored in a 32-bit
memory location:
• Range of an unsigned integer stored in a 64-bit
memory location:
• In general, the range of an unsigned integer
stored in an x-bit memory location:
8
Signed integer
• What is a signed integer?
• When would we use a signed integer in our
programs?
• How is a signed integer represented in memory?
9
Signed integer
• Different “signed integer representation”
conventions:
A. Sign and magnitude
B. 1’s complement
C. 2’s complement
• All conventions: sign represented by leftmost bit
• 0 for positive integer
• 1 for negative integer
10
A. “Sign and magnitude”
Let’s store the integer _______
in an 8-bit memory location
using the “sign and
magnitude” convention:
Let’s store the integer _______
in an 8-bit memory location
using the “sign and
magnitude” convention:
1. Convert the absolute value of
the integer to a binary #
2. Set the sign bit (leftmost bit) to
appropriate value (0 or 1)
1. Convert the absolute value of
the integer to a binary #
2. Set the sign bit (leftmost bit) to
appropriate value (0 or 1)
11
B. “1’s Complement”
Let’s store the integer _______
in an 8-bit memory location
using the “1’s complement”
convention:
Let’s store the integer _______
in an 8-bit memory location
using the “1’s complement”
convention:
1. Convert the absolute value of
the integer to a binary #
2. If the integer is negative,
• “flip all its bits”
1. Convert the absolute value of
the integer to a binary #
2. If the integer is negative,
• “flip all its bits”
12
C. “2’s Complement”
Let’s store the integer _______
in an 8-bit memory location
using the “2’s complement”
convention:
Let’s store the integer _______
in an 8-bit memory location
using the “2’s complement”
convention:
1. Convert the absolute value of
the integer to a binary #
2. If the integer is negative,
• “flip all its bits”
• Then, add 12
1. Convert the absolute value of
the integer to a binary #
2. If the integer is negative,
• “flip all its bits”
• Then, add 12
13
Which convention does the
computer used?
• “2’s complement” convention is used to
represent a signed integer in memory
• Why?
14
Decimal
2’s
complement
1’s
complement
Sign
/magnitude
+7
0111
0111
0111
+6
0110
0110
0110
+5
0101
0101
0101
+4
0100
0100
0100
+3
0011
0011
0011
+2
0010
0010
0010
+1
0001
0001
0001
+0
0000
0000
0000
-0
---------
1111
1000
-1
1111
1110
1001
-2
1110
1101
1010
-3
1101
1100
1011
-4
1100
1011
1100
-5
1011
1010
1101
-6
1010
1001
1110
-7
1001
1000
1111
-8
1000
-------
--------
15
Converting back
Let’s convert __________ (“2’s
complement” representation
of the signed integer _______ )
back to that signed integer:
Let’s convert __________ (“2’s
complement” representation
of the signed integer _______ )
back to that signed integer:
1. If the binary number is positive,
i.e., the leftmost bit has the
value 0,
• convert this binary number
back to a signed integer
1. If the binary number is
negative, i.e., the leftmost bit
has the value 1,
• “flip all its bits”
• Then, add 12
2. Convert this binary number
back to a signed integer
16
Range of a signed integer
• In general, if x bits is used to represent a signed
integer, its range is [-2 x-1 .. 2 x-1 -1]
• Example:
17
How is data represented in
the computer memory?
3. Floating point number
• Examples:
• Some have a finite fractional part: 8.25, ½, 9.0
• Some do not: 5/6 (0.83333333333…), 11/12 (0.916666666666…)
• Some are positive and some are negative (signed)
• In real life, with infinite amount of paper, it is easy
to deal with such numbers!
• 5/6 -> 0.83333333333…
• 11/12 -> 0.916666666666…
• But computer memory is finite, it has a fixed
amount of bits -> so numbers must be truncated or
rounded
+0
• 5/6 -> 0.83333333333…
+1
• 11/12 -> 0.916666766666…
18
Implication
• When storing floating point numbers into
computer memory, the fact that this memory is
finite implies …
• Loss of precision
• Loss of accuracy
• Lesson: Don’t use float for currency calculation
• This is why round() is used in ChangeMachine.py
19
Floating point representation
(in memory) – Part 1
• A single-precision floating point number (IEEE-754) is
represented in memory using 32 bits:
1. Determine the 1-bit sign => 0 → positive, 1 → negative
S -> the sign
2. Convert the integral and frational parts into binary numbers
3. Normalize the binary number into normalized scientific
notation
• Syntax for scientific notation:
• Normalized scientific notation?
• In decimal numeral system, it means:
• one non-zero digit to the left of the decimal point:
⅓ = 0.3333333 = 3.333333 x 10-1
• In binary numeral system, the same convention is
used:
20
Floating point representation
(in memory) – Part 2
4. Represent exponent with Excess_127
• Add 127 to it
• Convert the result to a binary number
E -> 8 bits for the exponent => 2 b7b6b5b4b3b2b1b0
5. Discard the “1” from integral part of the binary number
-> 1.b22b21…b1b0
and keep the fractional part -> b22b21…b1b0
M: 23 bits for the fractional part
called significand or mantissa -> b22b21…b1b0
21
Range of a floating point
number
+/- 1.b22b21…b1b0 x 2 b7b6b5b4b3b2b1b0
• 8 bits for the exponent -> ranges from [-126,127]
• the smallest non-zero number ≈ 2-126 ≈ 1.17 x 10-38
• the largest number ≈ 2127 ≈ 3.40 x 1038
• 23 bits for the mantissa -> approx. 7 decimal digits
• Special representation for 0, etc…
22
Summary
• Binary Encoding
• Characters
• Integers (unsigned and signed)
• Floating point numbers
• The meaning of a sequence of 0’s and 1’s in x bytes in
memory, such as:
01101000101001010010011011110010
is determined by the context, as it could
be Unicode character, integer, floating point
number, colour, sound, microprocessor (CPU) instruction,
etc …
23
Next Lecture
• Functions
24