CNG111 Lecture 2
CNG111 Lecture 2
CNG111 - Lecture 2 2
Data Representation
• 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.
– …
CNG111 - Lecture 2 3
Binary Representation of Numeric Information
• Decimal numbering system
– Base-10
– Each position is a power of 10
3052 = 3 x 103 + 0 x 102 + 5 x 101 + 2 x 100
CNG111 - Lecture 2 4
Decimal-to-binary Conversion
• Divide the number until zero:
Example: Convert decimal value 35 to binary
–35 / 2 = 17x2 + 1
–17 / 2 = 8x2 + 1
–8 / 2 = 4x2 + 0
–4 / 2 = 2x2 + 0
–2 / 2 = 1x2 + 0
–1 / 2 = 0x2 + 1
– Answer: 2k - 1
• Ex: given 4 bits, the largest binary number is 1111 (in
decimal 15)
CNG111 - Lecture 2 6
Binary Representation of Numeric Information (continued)
CNG111 - Lecture 2 10
Binary Representation of Numeric Information
(continued)
CNG111 - Lecture 2 11
Binary Representation of Real Numbers
•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
CNG111 - Lecture 2 12
Binary Representation of Real Numbers
• Approach 1: Use fixed-point
– Similar to integers, except that there is a decimal point.
– E.g: using 8 bits:
Assumed decimal
point
CNG111 - Lecture 2 13
Binary Representation of Real Numbers
• Location of the decimal point changes the value of the
number.
– E.g.: using 8 bits:
Assumed decimal
point
CNG111 - Lecture 2 14
Binary Representation of Real Numbers
• Solution: Use scientific notation: a x 2b (or ±M x B±E)
– Example: 5.75
• 5 101
• 0.75 ½ + ¼ 2-1+2-2 11
• 5.75 101.11 x 20
• Number is then normalized so that the first significant
digit is immediately to the left of the binary point
– Example: 1.0111 x 22
• We take and store the mantissa and the exponent.
CNG111 - Lecture 2 15
Binary Representation of Real Numbers
• This needs some standardization for:
– where to put the decimal point
– how to represent negative numbers
– how to represent numbers less than 1
CNG111 - Lecture 2 16
IEEE 32bit Floating-Point Number
Representation (8bit exp, 23 bit fraction)
• M x 2E
• 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
CNG111 - Lecture 2 17
IEEE 32bit Floating-Point Number
Representation (1 bit sign, 8 bits exponent, 23 bits fraction)
• 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: (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
2013 G. Ucoluk & S. Kalkan - CEng 111 18
IEEE 32bit Floating-Point Number
Representation
• What is the maximum positive IEEE floating
point value that can be stored?
– Just less than 2129.
CNG111 - Lecture 2 20
Binary Representation of Textual Information (cont’d)
Decima Binary Val. Dec. Unicode Charac.
l 0x30 0x0030 0
48 0011000 0 0x31 0x0031 1
0
0x32 0x0032 2
49 0011000 1
1 0x33 0x0033 3
CNG111 - Lecture 2 22
Binary Representation of Sound and Images
(continued)
CNG111 - Lecture 2 23
Digitization of an Analog Signal
CNG111 - Lecture 2 24
Binary Representation of Sound and Images
(continued)
• Representing image data