CENG240-2021 Week2 The World of PL and Data Representation
CENG240-2021 Week2 The World of PL and Data Representation
CENG240-2021 Week2 The World of PL and Data Representation
ly
us
¢ Objectives
vio
ly
us
¢
Pre
context: An electronic
device that has a
‘microprocessor’ in it.
§ Binary
ly
us
vio
Pre
ly
us
vio
Pre
ly
us
¢
Pre
(RAM)
¢ Allows reading and writing
operations
¢ Each access requires an
address
ly
us
Fetch
vio
¢
Pre
ly
us
vio
Pre
¢ Quiz 1 announced!
¢ Labs starting in two weeks.
¢ Midterm: 1 June, Tuesday, 17:40
Step 1: Get how many numbers will be provided and store that in N
Step 2: Create a variable named Result with initial value 0
Step 3: Execute the following step N times:
Step 4: Get the next number and add it to Result
Step 5: Divide Result by N to obtain the average
¢ Imperative
¢ Functional
¢ Object-oriented
¢ Logical-declarative
¢ Concurrent
¢ Event-driven
MEET
Guido van Rossum (1956 - )
¢ An interpretive/scripting PL that:
§ Longs for code readability
§ Ease of use, clear syntax
§ Wide range of applications, libraries, tools
¢ Multiple Paradigms:
§ Functional
§ Imperative
§ Object-oriented
REPRESENTATION OF DATA IN
COMPUTERS (CH3)
2021 S. Kalkan - CEng 240 34
Data Representation
METU Computer Engineering
¢ Based on 1s and 0s
§ So, everything is represented as a set of binary
numbers
¢ We will now see how we can represent:
§ Integers: 3, 1234435, -12945 etc.
§ Floating point numbers: 4.5, 124.3458, -1334.234
etc.
§ Characters: /, &, +, -, A, a, ^, 1, etc.
§…
¢ Representing integers
§ Decimal integers are converted to binary integers
§ Question: given k bits, what is the value of the largest
integer that can be represented?
§ 2k - 1
§ Ex: given 4 bits, the largest is 24-1 = 15
¢ Sign/magnitude notation
1 101 = - 5
0 101 = + 5
¢ Problems:
§ Two different representations for 0:
§ 1 000 = -0
§ 0 000 = +0
§ Addition & subtraction require a watch for the
sign! Otherwise, you get wrong results:
§ 0 010 (+2) + 1 010 (-2) = 1 100 (-4)
+
1 1 1 0
+ 14 !"
1 1 0 0 1 9 !"
In other words:
• Numbers larger than or equal to 16 (2! ) are discarded in a 4-bit representation.
• Therefore, 11 + 14 yields 9 in this 4-bit representation.
• This is actually modular arithmetic:
11 + 14 𝑚𝑜𝑑 16 ≡ 9 𝑚𝑜𝑑 16
¢ Example:
§ We want to compute: 12 – 6
§ 12 => 01100
§ -6 => -(00110) => (11001)+1 => (11010)
¢ 12 – 6 =
01100 So, addition and subtraction operations
+ 11010 are simpler in the Two’s Complement
----------- representation
00110 => 6
Inversion
2021 S. Kalkan - CEng 240 45
Why does Two’s Complement
work?
METU Computer Engineering
¢ A 2nd perspective:
§ i - j mod 2N = i + (2N-j) mod 2N
§ Example:
§ Consider X and Y are positive numbers.
§ 𝑋 + −𝑌 = 𝑋 + 2" − 𝑌
= 2" − (𝑌 − 𝑋) = −(𝑌 − 𝑋) = 𝑋 − 𝑌
http://en.wikipedia.org/wiki/Method_of_complements
2021 S. Kalkan - CEng 240 47
Binary Representation of Real Numbers
METU Computer Engineering
¢ 1st Way:
§ 0.375 à 0x½ + 1x¼ + 1x1/8 à 011
¢ 2nd Way:
§ Multiply by 2 and get the integer part until we get ‘0’
after the dot:
§ 0.375 x 2 = 0.750 = 0 + 0.750
§ 0.750 x 2 = 1.500 = 1 + 0.500
§ 0.500 x 2 = 1.000 = 1 + 0.000
• M x 2E 2 − 2#$% ×2&$'
• Exponent (E): 8 bits
• Add 127 to the exponent value before storing it
• E can be 0 to 255 with 127 representing the real
zero.
• Fraction (M - Mantissa): 23 bits
• 2128 = 1.70141183 × 1038
• Example: 12.375
• The digits before the dot:
• (12)10 à (1100)2
• The digits after the dot:
• 1st Way: 0.375 à 0x½ + 1x¼ + 1x1/8 à 011
• 2nd Way: Multiply by 2 and get the integer part until 0:
• 0.375 x 2 = 0.750 = 0 + 0.750
• 0.750 x 2 = 1.50 = 1 + 0.50
• 0.50 x 2 = 1.0 = 1 + 0.0
• (12.375)10 = (1100.011)2
• NORMALIZE (move the point): (1100.011)2= (1.100011)2x23
• Exponent: 3, adding 127 to it, we get 1000 0010
• Fraction: 100011
• Then our number is: 0 10000010 10001100000000000000000
2021 S. Kalkan - CEng 240 55
Why add bias to Binary
Number
Decimal
Value
Value in Two’s
Complement
Value with
bias 7
the exponent?
0000 0 0 -7
0001 1 1 -6
METU Computer Engineering
0010 2 2 -5
0011 3 3 -4
http://steve.hollasch.net/cgindex/coding/ieeefloat.html
2021 S. Kalkan - CEng 240 57
IEEE 32bit Floating-Point Number
Representation
METU Computer Engineering
¢ Integers
¢ Floating point numbers
¢ Complex numbers
point
7 U+007F 0xxxxxxx
11 U+07FF 110xxxxx 10xxxxxx
16 U+FFFF 1110xxxx 10xxxxxx 10xxxxxx
21 U+1FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
26 U+3FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
31 U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
representations:
1. A fixed-length number representing the length of the text
followed by the binary values of the characters in the text.
§ Ex: “ABC” =>
00000011 01000001 01000001 01000001 (3 ‘A’ ‘B’ ‘C’)
2. Binary values of the characters in the text ended with a
unique marker, like “00000000” which has no value in the
ASCII table.
§ Ex: “ABC” =>
01000001 01000001 01000001 00000000 (‘A’ ‘B’ ‘C’ END)
Width =
¢ If you have lots and lots of one type 4 bytes