Data Structures & Algorithms - Topic 2 - Primitive Data Structures (1)
Data Structures & Algorithms - Topic 2 - Primitive Data Structures (1)
ALGORITHMS
Primitive/Built-in Data
Structures
INSTRUCTOR: Sulaman
Ahmad Naz
1
COMPUTER WORKS ON BINARY
NUMBERS
2
COMPUTER WORKS ON BINARY
NUMBERS
3
COMPUTER WORKS ON BINARY
NUMBERS
4
COMPUTER WORKS ON BINARY
NUMBERS
5
COMPUTER WORKS ON BINARY
NUMBERS
6
COMPUTER WORKS ON BINARY
NUMBERS
7
COMPUTER WORKS ON BINARY
NUMBERS
8
COMPUTER WORKS ON BINARY
NUMBERS
9
COMPUTER WORKS ON BINARY
NUMBERS
10
COMPUTER WORKS ON BINARY
NUMBERS
11
COMPUTER WORKS ON BINARY
NUMBERS
12
Computer Memory (RAM)
■ RAM is a finite array of bits.
13
Addresses in Memory
■ In most computer
architectures, memory is
organized into addressable
units, and these units are
often a multiple of bits.
14
‘BYTE’ is addressable; not ‘BIT’
■ Here are some reasons why each individual bit is not
always addressable:
– Addressing each individual bit would require a large number of
addresses, which could be impractical and inefficient.
– Managing individual bits would introduce complexity in terms of
circuitry and operations.
– Memory operations are often performed on larger units, such as
bytes or words, for efficiency.
■ Remarks:
– There are specialized scenarios where bit-level addressing may
be more relevant. For example, in certain embedded systems or
when dealing with specific hardware-level operations, direct
manipulation of individual bits might be necessary.
15
Word Size vs. BYTE
■ The "word size" in computer architecture refers to the number of
bits that a computer's CPU (Central Processing Unit) can process
in a single operation.
■ Modern computers commonly use 64-bit architectures.
■ The choice of the smallest addressable unit in computer memory
is often related to the size of the processor's registers.
■ Registers are small, high-speed storage locations within the CPU
that hold data and instructions needed for immediate processing.
■ The size of the registers is a crucial factor in determining the word
size of a computer architecture.
16
Primitive/Built-in Data Structures
■ Primitive data Data
Structur
structures (Data Types) es
are those which are
predefined way of
storing data by the Non-
Primitiv
system. e
primitiv
e
■ The set of operations
that can be performed
on these data are also
predefined.
Integer Float Char Boolean Pointer String
BOOLEAN
■ The "bool" datatype, representing Boolean values (true
or false), is typically implemented in memory using a
single byte (8 bits) in many programming languages.
■ It can take only two values; 0 for false and 1 for true.
■ For zero:
0 0 0 0 0 0 0 0
■ For one:
0 0 0 0 0 0 0 1
18
How text is represented as
binary?
19
CODING SCHEMES TO
REPRESENT DATA
■Any text-based data is stored by the computer
in the form of bits(a series of 1s and 0s) and
follows the specified Coding Scheme.
■The coding scheme is a Standard which tells the
user’s machine which character represents
which set of bytes.
■Specifying the coding scheme used is very
important as without it, the machine could
interpret the given bytes as a different
character than intended.
20
ASCII & EXTENDED ASCII
■ ASCII stands for American Standard Code for
Information Interchange.
■ In ASCII, each character (letter, number, symbol or
control character) is represented by a binary value.
■ Extended ASCII is a version that supports
representation of 256 different characters.
■ This is because extended ASCII uses eight bits to
represent a character as opposed to seven in standard
ASCII (where the 8th bit is used for error checking).
■ The maximum number of characters that can be
represented in extended ASCII is 256.
21
22
How character constants are processed by computers
Step 1.
The user presses Step 2.
the capital letter An electronic signal for the
D (shift+D key) capital letter D is sent to the
on the keyboard. system unit.
Step 4. Step 3.
After processing, the binary The signal for the capital letter D
code for the capital letter D is is converted to its ASCII binary
converted to an image, and code (01000100) and is stored in
displayed on the output device. memory for processing.
EBCDIC
■ EBCDIC is short for Extended Binary Coded Decimal
Interchange Code.
■ It is eight bits, or one byte, wide.
■ The EBCDIC code allows for 256 different characters.
■ EBCDIC is different from, and incompatible with, the
ASCII character set used by all other computers.
■ For personal computers, however, ASCII is the
standard.
■ If you want to move text between your computer and
a mainframe, you can get a file conversion utility that
will convert between EBCDIC and ASCII. 24
A L R M U E
E RS I FO UN
IQ
UNICODEU N I V U N
■ Unicode, international character-encoding system
designed to support the electronic interchange,
processing, and display of the written texts of the
diverse languages of the modern and classical world.
■ The Unicode Standard includes letters, digits,
diacritics, punctuation marks, and technical symbols
for many of the world languages, as well as emoji
and other symbols, using a uniform encoding
scheme.
■ The first version of Unicode was introduced in 1991;
the most recent version contains 149,878 characters.
https://en.wikipedia.org/wiki/List_of_Unicode_character
s 25
h t t p s : / / w w w. y o u t u b e . c o m / w a t c h ? v = M i j m
e o H 9 LT 4
E x t r a S T U F F T O WATC H 26
27
Character DATA
These numbers allow
comparison and
arrangement of character
and string data in
alphabetical order.
Languages that differentiate
between lower case and
upper case are called case
sensitive.
Character data or string data
can be joined together with
the + operator in an
operation called
concatenation.
Different input data for computer programs
■ Data to computer programs is provided through character
set (letters and numbers (integers, floating point)),
images or audio / video etc.
■ Data in all other formats must be converted (into) /
represented (as) binary first.
■ After processing, the output of the computers is again
converted back to the same format as input (as shown in
figure).
28
How image is represented as
binary
29
How image is represented as
binary
30
How image is represented as
binary
31
How audio is represented as
binary
32
INTEGERS
■ Computers use a fixed number of bits to represent an
integer. The commonly-used bit-lengths for integers are 8-
bit, 16-bit, 32-bit or 64-bit. Besides bit-lengths, there are
two representation schemes for integers:
– Unsigned Integers: can represent zero and positive integers.
– Signed Integers: can represent zero, positive and negative
integers. Three representation schemes had been proposed for
signed integers:
■ Sign-Magnitude representation
■ 1's Complement representation
■ 2's Complement representation
16 0 (2^16)-1 (=65,535)
35
Signed Integers
■ Signed integers can represent zero, positive integers, as
well as negative integers. Three representation schemes
are available for signed integers:
I. Sign-Magnitude representation
II. 1's Complement representation
III. 2's Complement representation
■ In all the above three schemes, the most-significant bit
(msb) is called the sign bit. The sign bit is used to
represent the sign of the integer - with 0 for positive
integers and 1 for negative integers. The magnitude of
the integer, however, is interpreted differently in
different schemes. 36
n-bit Sign Integers in Sign-Magnitude
Representation
■ In sign-magnitude representation:
40
n-bit Sign Integers in Sign-Magnitude
Representation
41
n-bit Sign Integers in 1's Complement
Representation
Again, the most significant bit (msb) is the sign bit, with
value of 0 representing positive integers and 1 representing
negative integers.
The remaining n-1 bits represents the magnitude of the
integer, as follows:
■ for positive integers, the absolute value of the integer is
equal to "the magnitude of the (n-1)-bit binary pattern".
■ for negative integers, the absolute value of the integer is
equal to "the magnitude of the complement (inverse) of
the (n-1)-bit binary pattern" (hence called 1's
complement).
42
n-bit Sign Integers in 1's Complement
Representation
Example 1: Suppose that n=8 and the binary representation 0 100 0001B.
Sign bit is 0 ⇒ positive
Absolute value is 100 0001B = 65D
Hence, the integer is +65D
Example 2: Suppose that n=8 and the binary representation 1 000 0001B.
Sign bit is 1 ⇒ negative
Absolute value is the complement of 000 0001B, i.e., 111 1110B = 126D
Hence, the integer is -126D
43
n-bit Sign Integers in 1's Complement
Representation
Example 3: Suppose that n=8 and the binary representation 0 000
0000B.
Sign bit is 0 ⇒ positive
Absolute value is 000 0000B = 0D
Hence, the integer is +0D
45
n-bit Sign Integers in 1's Complement
Representation
46
n-bit Sign Integers in 2's Complement
Representation
Again, the most significant bit (msb) is the sign bit, with
value of 0 representing positive integers and 1 representing
negative integers.
The remaining n-1 bits represents the magnitude of the
integer, as follows:
■ for positive integers, the absolute value of the integer is
equal to "the magnitude of the (n-1)-bit binary pattern".
■ for negative integers, the absolute value of the integer is
equal to "the magnitude of the complement of the (n-1)-bit
binary pattern plus one" (hence called 2's complement).
47
n-bit Sign Integers in 2's Complement
Representation
Example 1: Suppose that n=8 and the binary representation 0 100 0001B.
Sign bit is 0 ⇒ positive
Absolute value is 100 0001B = 65D
Hence, the integer is +65D
Example 2: Suppose that n=8 and the binary representation 1 000 0001B.
Sign bit is 1 ⇒ negative
Absolute value is the complement of 000 0001B plus 1, i.e., 111 1110B
+ 1B = 127D
Hence, the integer is -127D
48
n-bit Sign Integers in 2's Complement
Representation
Example 3: Suppose that n=8 and the binary representation 0 000 0000B.
Sign bit is 0 ⇒ positive
Absolute value is 000 0000B = 0D
Hence, the integer is +0D
Example 4: Suppose that n=8 and the binary representation 1 111 1111B.
Sign bit is 1 ⇒ negative
Absolute value is the complement of 111 1111B plus 1, i.e., 000 0000B
+ 1B = 1D
Hence, the integer is -1D
49
n-bit Sign Integers in 2's Complement
Representation
50
n-bit Sign Integers in 2's Complement
Representation
51
n-bit Sign Integers in 2's Complement
Representation
Computers use 2's complement in representing signed
integers. This is because:
52
n-bit Sign Integers in 2's Complement
Representation
Example 1: Addition of Two Positive Integers: Suppose that n=8, 65D + 5D = 70D
65D → 0100 0001B
5D → 0000 0101B
0100 0110B → 70D (OK)
Example 2: Subtraction is treated as Addition of a Positive and a Negative
Integers: Suppose that n=8, 65D - 5D = 65D + (-5D) = 60D
65D → 0100 0001B
-5D → 1111 1011B
0011 1100B → 60D (discard carry - OK)
Example 3: Addition of Two Negative Integers: Suppose that n=8, -65D - 5D = (-
65D) + (-5D) = -70D
-65D → 1011 1111B
-5D → 1111 1011B
1011 1010B → -70D (discard carry - OK)
53
Range of n-bit 2's Complement Signed
Integers
n minimum maximum
-(2^7) +(2^7)-1
8
(=-128) (=+127)
-(2^15) +(2^15)-1
16
(=-32,768) (=+32,767)
-(2^31) +(2^31)-1
32
(=-2,147,483,648) (=+2,147,483,647)(9+ digits)
-(2^63)
+(2^63)-1
64 (=-
(=+9,223,372,036,854,775,807)(18+ digits)
9,223,372,036,854,775,808)
54
Self-Assessment Questions
1. What are the ranges of 8-bit, 16-bit, 32-bit and 64-bit
integer, in "unsigned" and "signed" representation?
2. Give the value of 88, 0, 1, 127, and 255 in 8-bit
unsigned representation.
3. Give the value of +88, -88 , -1, 0, +1, -128, and +127 in
8-bit 2's complement signed representation.
4. Give the value of +88, -88 , -1, 0, +1, -127, and +127 in
8-bit sign-magnitude representation.
5. Give the value of +88, -88 , -1, 0, +1, -127 and +127 in
8-bit 1's complement representation.
55
Answers:
1. The range of unsigned n-bit integers is [0, 2^n - 1]. The range of
n-bit 2's complement signed integer is [-2^(n-1), +2^(n-1)-1].
2. 88 (0101 1000), 0 (0000 0000), 1 (0000 0001), 127 (0111 1111),
255 (1111 1111).
3. +88 (0101 1000), -88 (1010 1000), -1 (1111 1111), 0 (0000
0000), +1 (0000 0001), -128 (1000 0000), +127 (0111 1111).
4. +88 (0101 1000), -88 (1101 1000), -1 (1000 0001), 0 (0000 0000
or 1000 0000), +1 (0000 0001), -127 (1111 1111), +127 (0111
1111).
5. +88 (0101 1000), -88 (1010 0111), -1 (1111 1110), 0 (0000 0000
or 1111 1111), +1 (0000 0001), -127 (1000 0000), +127 (0111
1111).
56
A Must Watch Video!!!
https://www.youtube.com/watch?v=lKTsv
6iVxV4
57
Floating Point Datatypes
Some important Links:
■ https://www3.ntu.edu.sg/home/ehchua/programming/j
ava/datarepresentation.html#:~:text=Representation
%20of%20floating%20point%20number,digit%20befor
e%20the%20radix%20point
– Section 4 complete
■ https://www.youtube.com/watch?v=PZRI1IfStY0
■ https://www.youtube.com/watch?v=f4ekifyijIg
■ https://www.youtube.com/watch?v=782QWNOD_Z0
58
Assignment #1
Give detailed answers to the following questions that reflect your
understanding?
1. Why are negative integers stored as 2’s complement in computers?
2. How is the last carry treated while doing arithmetic on signed
integers in 1’s complement and 2’s complement?
3. Why are real numbers called floating-point numbers in computer?
4. What is the difference between single-precision and double-
precision floating point?
5. What is floating point error? Why does it occur?
THANK YOU
60