Unit1 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 64

Unit 2

Representation of Information
in Computers
Contents
2.1. Number Systems
2.2. Number Representation in Computers - Units of
Information
2.3. Representation of Integers
2.4. Operations on Integers
2.5. Logical operations on Binary Numbers
2.6. Symbol Representation

2
2.1. Number Systems

• Decimal System
• Base-b System
• Convertion from Decimal to Base-b
• Binary System
• Hexadecimal System
• Octal System

3
Number Categories
• Natural numbers
• Integers
• Rational numbers
• Irrational numbers

• Numbers are written using positional notation


943 = 9 x 102 + 4 x 10 + 3
Only in base 10 (decimal)

4
Decimal System

• The system has ten as its base


• Uses various symbols (called digits) for no more than
ten distinct values (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) to
represent any number
• Decimal separator indicates the start of a fractional part
• Sign symbols + (positive) or − (negative) in front of the
numbers to indicate sign

5
Decimal System (cont’d)

If i is an integer written in decimal form with digits dj:


i = dn-1dn-2 . . . . d1d0
then i represents the sum:

n 1
j
 j
d * 10
where n is thej  0total number of digits, and dj is
the j th digit from the rightmost position in the
decimal number.

6
Base-b System

• b digits is used in the representation of numbers.


• Example
1410 = 226 = 1123 = 11102
• If i is an integer written in base - b with digits dj:
i = dn-1dn-2 . . . . d1d0
then i represents the sum: Convert from base b
n 1
j to base 10
 j
j0
d * b

where n is the total number of digits, and dj is the j th digit from the
rightmost position in the decimal number.
• When the base is higher than 10, we need symbols to represent the
digits that correspondent to the decimal values of 10 and beyond.

7
Example
Convert 11101.112 to Decimal

8
Convert integers from decimal to base b
Remainder Method:
• Let value = (dn-1 dn-2 … d2 d1 d0)10.
• First divide value by b, the remainder is the least
significant digit b0.
• Divide the result by b, the remainder is b1.
• Continue this process until the result is less than b, giving
the most significant digit, bm-1.
• The result is bm-1 bm-2 … b2 b1 b0

9
Example 1
Use Remainder Method to convert 12 (base 10) to base 2

10
Convert Decimal Fractions to Base-b
• Begin with the decimal fraction and multiply by b. The
whole number part of the result is the first digit to the
right of the point.

• Disregard the whole number part of the previous result


and multiply by b once again. The whole number part of
this new result is the second digit to the right of the
point.

• Continue this process until you get a zero as number’s


decimal part or until you recognize an infinite repeating
pattern.

11
Example 1
Convert the decimal value .625 to a binary representation
• 0.625 x 2 = 1.25
The first binary digit to the right of the point is a 1.
• 0.25 x 2 = 0.50,
The second binary digit to the right of the point is a 0.
• 0.50 x 2 = 1.00
The third binary digit to the right of the point is a 1.
• We had 0 as the fractional part of our result,

The representation of .625(10)= .101(2)

12
Example 2
Binary representation of the decimal fraction 0.1
1. .1 x 2 = 0.2
The first binary digit to the right of the point is a 0
2. .2 x 2 = 0.4
The second binary digit to the right of the point is also a 0.
3. .4 x 2 = 0.8
The third binary digit to the right of the point is also a 0.
4. .8 x 2 = 1.6
The fourth binary digit to the right of the point is a 1.
5. .6 x 2 = 1.2
The fifth binary digit to the right of the point is a 1.
6. The next step to be performed (multiply 2. x 2) is exactly the same action
we had in step 2. We are then bound to repeat steps 2-5, then return to
Step 2 again indefinitely

.1 (decimal) = .00011001100110011 . . .
The repeating pattern is 0011

13
Binary System
• All data, including programs, in a computer system is
represented in terms of groups of binary digits
• A single bit can represent one of two values, 0 or 1.
• If we have several symbols to represent, we can make a
one-to-one correspondence between the patterns and the
symbols.
• Example : 0, 1, 2, 3 are mapped to the patterns 00,
01, 10, 11
• A group of k binary digits (bits) can be used to represent 2k
symbols

14
Hexadecimal System

Hexadecimal numbers have 16 different digits, that are


represented by the numbers from 0 to 9 and the letters A, B, C,
D, E and F.
Example

15
Octal System
• The octals numbers include only the representations for
the values from 0 to 7: 0, 1, 2, 3, 4, 5, 6, 7
• Example:
235 . 648
= 2x82 + 3x81 + 5x80 + 6x8-1 + 4x8-2
= 157. 812510

16
Data Representation in a Computer
Units of Information

•Basic Principles
•Units of Information

17
Basic Principles
• Data can be numbers, symbols, images, sounds . . .
• To store in computers, it’s necessary to represent
data in term of bit patterns
• There are different ways to encode different types of
data
• Numbers are convert to their binary
representations following some standard.
• Symbols are assigned a bit pattern
• Other data must be digitalized.

18
Data and Computers
• Every task, a computer manages data in some way
• In the past, computers dealt exclusively with numeric and
textual data
• At present, computers are multimedia devices -> deal with
different type of data:
• Numbers
• Text
• Audio
• Images and Graphics
• Video
• All of data is stored as binary digits: strings of 1s and 0s

19
2.2. Number Representation in
Computers - Units of Information

20
Analog and Digital Information

• Most part of the natural world is continuous and


infinite
• Computers are finite
• Information can be represented in analog or digital

21
Binary Representation

• One binary digit can be either 0 or 1


• To represent more than 2 things, we need more binary
digits
• N binary digits can represent 2n things

22
Representing Audio Information
• Digitize the sound wave
• Sampling: periodically measure the voltage of the
signal and record appropriate value
• Reproduce the sound: Use stored voltage values to
create a new continuous electronic signal
• Audio format : WAV,AU, AIFF, VQF, MP3

23
Representing Colors
• Often expressed as an RGB (red – green – blue)
value
• Three numbers indicate the contribution of each of
three primary colors
• Amount of data that used to represent a color is
called color depth
• HiColor: 16 bit color depth
• TrueColor: 24bit color depth

24
Three Dimensional Color Space

25
Some TrueColor RGB Values

26
Digitized Images and Graphics
• Image is a collection of individual dots called pixel
• Each pixel is composed of a single color
• The number of pixel used to represent a picture is
called resolution

27
Categorization of Data Types
• Basic types : the standard scalar predefined types that one
would expect to find ready for immediate use in any
imperative programming language
• Structured types: made up from such basic types or other
existing higher level types.

28
Units of information
• The smallest unit of information a computer can use
is bit (Binary Digit)
• The difference between two states (high current and
low current) is represented as one of two numbers (1
or 0).
• A collection of 8 bits are put together to form a byte
• Binary prefixes can be used to quantify computer
memory sizes
• Each successive prefix is multiplied by 1024 (210)
rather than the 1000 (103)

29
Units of information

30
Representation of Integers

• Unsigned Integers
• Signed Integers

31
Unsigned Integers

• Unsigned integers are represented by a fixed number of bits (typically 8,


16, 32, and/or 64)
• Only a finite set of numbers that can be represented:
• With 8 bits, 0…255 (0016…FF16) can be represented;
• With 16 bits, 0…65535 (000016…FFFF16) can be represented
• If an operation on bytes has a result outside this range, it will cause an
‘overflow’

32
Signed Integers
• The most significant bit is set to 0 and 1 for positive and
negative numbers
• Example
+4210 = 001010102
and so
-4210 = 110101102
• All number whose leftmost bit is 1 is considered negative

33
Signed Integers (cont’d)
• If we have 4 bits in our representation then,
• The most positive representable number using sign-and-
magnitude is 0111
• The most negative representable number using sign-and-
magnitude is 1000
• How to represent negative number?

34
Two’s Complement
• Representation for signed binary numbers
• Leading bit is a sign bit
• Binary number with leading 0 is positive
• Binary number with leading 1 is negative
• Magnitude of positive numbers is just the binary representation
• Magnitude of negative numbers is found by
• Complement the bits: replace all the 1's with 0's, and all the 0's with 1's
• Add one to the complemented number
• The carry in the most significant bit position is thrown away when
performing arithmetic

35
Example
Performing two's complement on the decimal 42 to get -42
• Using a eight-bit representation

42 = 00101010 Convert to binary

11010101 Complement the bits

11010101 Add 1 to the complement


+ 00000001
11010110 Result is -42 in two's complement

36
Two's Complement Arithmetic
• Computing 50 - 42 using a two's complement
representation with eight-bit numbers

50 - 42 = 50 + (-42) = 8

00110010 50

11010110 Two's complement of 42


Throw away the
high-order 00110010 Add 50 and -42
carry as we are + 11010110
using a eight bit 100001000
representation
00001000 Is the eight-bit result
37
Operations on Integers
• Addition and Subtraction

• Multiplication and Division

38
Addition and Subtraction

• Addition

• Subtraction

39
Binary Addition
0+0=0
0+1=1
1+0=1
1 + 1 = 0, and carry 1 to the next more significant bit

40
Example
00011010 + 00001100 = 00100110
1 1 carries
0 0 0 1 1 0 1 0 = 26 (base 10)
+ 0 0 0 0 1 1 0 0 = 12 (base 10)
0 0 1 0 0 1 1 0 = 38 (base 10)

00010011 + 00111110 = 01010001


1 1 1 1 1 carries
0 0 0 1 0 0 1 1 = 19 (base 10)
+0 0 1 1 1 1 1 0 = 62 (base 10)
0 1 0 1 0 0 0 1 = 81 (base 10)

41
Binary Subtraction
0-0=0
0 - 1 = 1, and borrow 1 from the next more significant bit
1-0=1
1-1=0

42
Multiplication and Division

• Multiplication

• Division

43
Binary Multiplication

0x0=0
0x1=0
1x0=0
1 x 1 = 1, and no carry or borrow bits

44
Example
00101001 × 00000110 = 11110110
0 0 1 0 1 0 0 1 = 41(base 10)
× 0 0 0 0 0 1 1 0 = 6 (base 10)
0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 1
0 0 1 0 1 0 0 1
0 0 1 1 1 1 0 1 1 0 = 246 (base 10)

00010111 × 00000011 = 01000101


0 0 0 1 0 1 1 1 = 23 (base 10)
× 0 0 0 0 0 0 1 1 = 3 (base 10)
1 1 1 1 1 carries
0 0 0 1 0 1 1 1
0 0 0 1 0 1 1 1
0 0 1 0 0 0 1 0 1 = 69 (base 10)

45
Binary Division

Binary division follows the same rules as in


decimal division .

46
Logical Operations on Binary Numbers

• Logical Operations with One or Two Bits


• Logical Operations with One or Two Integers

47
Logical Operations with One or Two Bits
AND: Compares 2 bits and if they are both 1, then the result
is 1, otherwise, the result is 0.
OR: Compares 2 bits and if either or both bits are 1, then the
result is 1, otherwise, the result is 0.
XOR: (Exclusive OR) : Compares 2 bits and if exactly one
of them is 1 then the result is 1 otherwise (if the bits are
the same), the result is 0.
NOT: Changes the value of a single bit. If it is a 1, the result
is 0; if it is a 0, the result is 1.
Truth Table of Logical Operations

48
Logical Operations
with One or Two Integers
• NOT
• AND
• OR
• XOR

49
NOT operation
Unary operation which performs logical negation on
each bit, forming the ones' complement of the given
binary value. Digits which were 0 become 1, and vice
versa.
Example:
NOT 0111
= 1000

50
AND operation
• An AND operation takes two binary representations of
equal length and performs the logical AND operation on
each pair of corresponding bits. In each pair, perform
AND operation on two bits
• Example:
0101
AND 0011
= 0001

51
OR operation
• An OR operation takes two bit patterns of equal
length, and produces another one of the same length
by matching up corresponding bits (the first of each;
the second of each; and so on) and performing the
logical OR operation on each pair of corresponding
bits.
• For example:
0101
OR 0011
= 0111

52
XOR Operation
• An exclusive or operation takes two bit
patterns of equal length and performs the
logical XOR operation on each pair of
corresponding bits.
• For example:
0101
XOR 0011
= 0110

53
Symbol Representation
Basic Principles
ASCII Code Table
Unicode Code Table

54
Basic Principles
• Text documents can be decomposed into individual
characters
• It's important to handle character data
• Character data isn't just alphabetic characters, but also
numeric characters, punctuation, spaces, etc
• They need to be represented in binary
• There aren't mathematical properties for character data, so
assigning binary codes for characters is somewhat arbitrary

55
Character Set
• A list of characters and the codes used to represent
each one.
• By using a particular character set, computer
manufacturers have made the processing of data
easier.
• Two popular character sets: ASCII & Unicode

56
ASCII Code Table
• ASCII -- American Standard Code for Information Interchange
- permitted machines from different manufacturers to exchange
data.
• The ASCII standard was developed in 1963

• ASCII standard originally used 7 bits to represent characters ->


consists of 128 binary values (0 to 127), each associated with a
character or command
• The extended ASCII character set used 8 bits to represent
characters. It also consists 128 characters representing
additional special, mathematical, graphic and foreign
characters.

57
The Standard ASCII Code Table

58
The extended ASCII characters

59
Limitations of ASCII character set
• String data types allocated one byte per character
• The extended version of ASCII which have 256
characters is ehough for English but not enough for
international use
• Logographic languages such as Chinese, Japanese,
and Korean need far more than 256 characters for
reasonable representation.
• Vietnamese need 61 characters for representation.
• Where can we find number for our characters?

 2bytes per character?

60
Unicode Code Table

Before Unicode was invented. . . .


• There were hundreds of different encoding systems
• No single encoding could contain enough characters
• Encoding systems conflict with one another : two
encodings can use the same number for two different
characters, or use different numbers for the same
character.

61
Unicode Character Set

• Provides a unique number for every character


• Uses 16 bits or more per character
• Has been adopted by such industry leaders as HP, IBM,
Microsoft, Oracle, Sun, and many others.
• Is supported in many operating systems, all modern
browsers, and many other products.

62
Some Symbols in the Unicode Character Set

63
Advantages of using Unicode

• Significant cost savings


• Enables software products to be targeted across multiple
platforms, languages and countries without re-engineering.
• Allows data to be transported through many different
systems without corruption.

64

You might also like