0% found this document useful (0 votes)
12 views25 pages

CNG111 Lecture 2

The lecture covers binary data representation, explaining how integers, real numbers, and characters are represented using binary numbers (1s and 0s). It discusses methods for converting decimal numbers to binary, including two's complement for signed integers, and outlines the representation of real numbers using fixed-point and scientific notation. Additionally, it touches on the binary representation of textual information through ASCII and Unicode, as well as the digitization of sound and image data.

Uploaded by

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

CNG111 Lecture 2

The lecture covers binary data representation, explaining how integers, real numbers, and characters are represented using binary numbers (1s and 0s). It discusses methods for converting decimal numbers to binary, including two's complement for signed integers, and outlines the representation of real numbers using fixed-point and scientific notation. Additionally, it touches on the binary representation of textual information through ASCII and Unicode, as well as the digitization of sound and image data.

Uploaded by

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

Lecture 2 – Binary Data Representation

CNG111 - Lecture 2 1/23


Objective

• Binary Data Representation


– .. of integers, real numbers, characters etc.

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

• Binary numbering system


– Base-2
– Uses ones and zeros
– Each position is a power of 2
1101 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20

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

• Therefore, 35 has the binary representation: 100011


CNG111 - Lecture 2 5
Binary Representation of Numeric Information
(continued)
• 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?

– Answer: 2k - 1
• Ex: given 4 bits, the largest binary number is 1111 (in
decimal 15)

• So, the largest is 24-1 = 15

CNG111 - Lecture 2 6
Binary Representation of Numeric Information (continued)

• Signed integers must also represent the sign (positive or


negative) – How ?

• Method 1: 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)
CNG111 - Lecture 2 7
Binary Representation of Numeric
Information (continued)
• Method 2: Two’s complement instead of sign-magnitude
representation
– Positive numbers have a leading 0.
• 5 => 0101
– The representation for negative numbers is found by
subtracting the absolute value from 2N for an N-bit system(2N
–(absolute value of the number):
• Example: Assume that N=4. Then, -5 => 24 – 5 = 16 – 5 =
1110 => 10112
• Advantages:
– 0 has a single representation: +0 = 0000, -0 = 0000
– Arithmetic works fine without checking the sign bit:
• 1011 (-5) + 0110 (6) = 0001 (1)
• 1011 (-5) + 0011 (3) = 1110 (-2)
CNG111 - Lecture 2 8
Binary Decimal Value in
Binary Representation of Number Value Two’s
Compleme
Numeric Information (continued) nt
0000 0 0
• Shortcut to convert from “two’s 0001 1 1
complement” : 0010 2 2
– If the leading bit is zero, no need to 0011 3 3
convert. 0100 4 4
– If the leading bit is one, invert the 0101 5 5

number and add 1. 0110 6 6


0111 7 7
• What is our range? 1000 8 -8
– With 2’s complement we can 1001 9 -7
represent numbers from - 1010 10 -6
2N-1 to 2N-1 - 1 using N bits. 1011 11 -5
– 8 bits: -128 to +127. 1100 12 -4

– 16 bits: -32,768 to +32,767. 1101 13 -3


1110 14 -2
– 32 bits: -2,147,483,648 to
1111 15 -1
+2,147,483,647. CNG111 - Lecture 2 9
Binary Representation of Numeric
Information (continued)
• Example:
– We want to compute: 12 – 6
– 12 => 01100
– -6 => -(00110) => (11001)+1 => (11010)
• 12 – 6 =
01100
+ 11010
So, addition and subtraction operations
----------- are simpler in the Two’s Complement
00110 => 6 representation

CNG111 - Lecture 2 10
Binary Representation of Numeric Information
(continued)

• Due to its advantages, two’s complement is the most


common way to represent integers on computers.

CNG111 - Lecture 2 11
Binary Representation of Real Numbers

Conversion of the digits after the dot into binary:


• 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
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:

1 1 1 1 1 1 1 1 = 1*23 + 1*22 + 1*21 + 1*20 +


1*2-1 + 1*2-2 + 1*2-3 + 1*2-4 = 15.9375

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:

1 1 1 1 1 1 1 1 = 1*24 + 1*23 + 1*22 + 1*21 + 1*20


1*2-1 + 1*2-2 + 1*2-3 = 31.875

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.

• Check out these useful links:


– http://steve.hollasch.net/cgindex/coding/ieeefloat.ht
ml
– http://babbage.cs.qc.edu/IEEE-754/Decimal.html
– http://www.cs.uaf.edu/~cs301/notes/Chapter4/node
13.html
CNG111 - Lecture 2 19
Binary Representation of Textual Information

• Characters are mapped onto binary numbers


– ASCII (American Standard Code for Information Interchange) code set
• Originally: 7 bits per character; 128 character codes
– Unicode code set
• 16 bits per character
– UTF-8 (UCS Transformation Format) code set.
– UTF-8 is capable of encoding all 1,112,064 valid character code
points in unicode using one to four one-byte (8-bit) code units.
• Variable number of 8-bits.

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

50 0011001 2 0x34 0x0034 4


0 0x35 0x0035 5
51 0011001 3 0x36 0x0036 6
1
ASCII 52 0011010 4
0x37 0x0037 7 Unicode
7 bits long 0 0x38 0x0038 8 16 bits long
53 0011010 5 0x39 0x0039 9
1 0x3A 0x003A :
54 0011011 6 0x3B 0x003B ;
0
0x3C 0x003C <
55 0011011 7
1 0x3D 0x003D =
56 0011100 8 0x3E 0x003E >
0 Partial
0x3F 0x003F ?
57 0011100 9 0x40 0x0040 @
listings
1
0x41 0x0041 A
only!
58 0011101 :
0 0x42 0x0042 B
59 0011101 ;CNG111 - Lecture 2 21
1
Binary Representation of Sound and Images

• Multimedia data is sampled to store a digital


form, with or without detectable differences

• Representing sound data


– Sound data must be digitized for storage in a computer

– Digitizing means periodic sampling of amplitude values

CNG111 - Lecture 2 22
Binary Representation of Sound and Images
(continued)

– From samples, original sound may be approximated

– To improve the approximation:

• Sample more frequently (increase sampling rate)

• Use more bits for each sample value ( bit depth)

CNG111 - Lecture 2 23
Digitization of an Analog Signal

(a) Sampling the Original


Signal

(b) Recreating the


Signal from the Sampled
Values

CNG111 - Lecture 2 24
Binary Representation of Sound and Images
(continued)
• Representing image data

– Images are sampled by reading


color and intensity values at
even intervals across the image

– Each sampled point is a pixel

– Image quality depends on


number of bits at each pixel

– More image information: http://c


at.xula.edu/tutorials/imaging/gra
yscale.php
CNG111 - Lecture 2 25
http://photo.net/equipment/digital/basics/

You might also like