Data Representation Lecture 2
Data Representation Lecture 2
Week 2
Data, Representation &
Numeracy
Data, Representation & Numeracy
– what is data
Data refers to raw information that consists of
basic facts and figures. Computer data include
different forms of data, such as numerical data,
images, coding, notes, and financial data.
– how is data represented in computers
Computers use 1s and 0s because data is
stored as binary numbers.
Bits, Bytes, Words ...
• The basic units of data are organized as follows
– bit
• this is the fundamental unit of data
• 2-state switch values: 0/1 (e.g. true/false; off/on)
– byte
• a group of 8 bits taken as a single unit
• the smallest unit of memory that is addressable
– word
• A unit of digital information that represents a group of bits that can
be processed as a single unit by a computer's CPU (central
processing unit). The size of a word can vary depending on the
architecture of the computer system, but it is typically 16, 32, or 64
bits in size. Words are often used to represent numerical values or
memory addresses.
• Pentium I, II, III 32 bits (4 bytes)
• Pentium IV 64 bits (8 bytes)
– other units are used, such as blocks, strings, and so on
• these are considered non-standard application structures
Binary Coding/ Gray Code
• Binary, or base-2, coding is used for all data in the computer
• 2-state switches
– only 0 and 1 are used as values
– each corresponds to a specific electrical (magnetic/optical)
signal value
• Characters
– the ASCII system is used to represent printable characters and
also some non-printable signals used in inter-computer
communication across networks
• Numbers
– unsigned integer
– signed integer
– real, or floating point, numbers
– decimal (base-10) numbers are also used in computers
Unsigned Binary
Unsigned binary is a number system used in computing and digital electronics to
represent non-negative integers using only two digits, 0 and 1. It is called
"unsigned" because it does not use a sign bit to indicate whether a number is
positive or negative.
• Here's an example of an unsigned binary number:
• 10110101
• To convert this binary number to decimal, we can use the method I described
earlier:
10110101 (binary) = 12^7 + 02^6 + 12^5 + 12^4 + 02^3 + 12^2 + 02^1 + 12^0
= 128 + 0 + 32 + 16 + 0 + 4 + 0 + 1
= 181 (decimal)
So, the binary number 10110101 represents the decimal value 181. Note that this
is an unsigned binary number, because it does not include a sign bit to indicate
whether the number is positive or negative.
Unsigned Binary
• We can construct an unsigned binary sequence using the
algorithm
– Build up starting from 0.
– Add 1 to the previous number.
– Repeat previous step until a string of 1’s of length L is reached.
Decimal Binary Binary Code
0 0000 0
1 0001 1
2 0010 10
3 0011 11
4 0100 100
5 0101 101
6 0110 110
7 0111 111
8 1000 1000
Signed Binary - 1’s complement
0101
1010
Use of 1's complement
1's complement plays an important role in representing the signed
binary numbers. The main use of 1's complement is to represent a
signed binary number. Apart from this, it is also used to perform
various arithmetic operations such as addition and subtraction.
In signed binary number representation, we can represent both
positive and negative numbers. For representing the positive
numbers, there is nothing to do. But for representing negative
numbers, we have to use 1's complement technique. For
representing the negative number, we first have to represent it with
a positive sign, and then we find the 1's complement of it.
Signed Binary - 2’s complement
• We need a more efficient representation of positive and
negative integers using an L-bit form.
• One straightforward approach is to consider the operation:
X
+Y
0 What representation of Y gives a zero sum?
3 00000011
+ bbbbbbb Y
00000000
Signed Binary - 2’s complement
X
+Y
0 What representation of Y gives a zero sum?
1111111 Carry
3 00000011
-3 + 11111101
1 00000000
Carry out
Signed Binary - 2’s complement
• We need a more efficient representation of positive and
negative integers using an L-bit form.
3 00000011
1
~3 11111100
+ 1 2
-3 11111101
Use of 2's complement
• 2's complement is used for representing signed numbers
and performing arithmetic operations such as
subtraction, addition, etc. The positive number is simply
represented as a magnitude form. So there is nothing to
do for representing positive numbers. But if we represent
the negative number, then we have to choose either 1's
complement or 2's complement technique. The 1's
complement is an ambiguous technique, and 2's
complement is an unambiguous technique.
(r-1)’s and r’s complements
• The (r-1)’s complement and r's complement are two methods
of representing negative numbers in binary form.
• The (r-1)’s complement of a binary number is found by
subtracting each digit from the digit base r-1, where r is the
base of the number system. For example, in a binary system
(base 2), the (r-1)’s complement of the binary number 1010
would be:
(2-1) - 1 = 1 - 1 = 0
(2-1) - 0 = 1 - 0 = 1
(2-1) - 1 = 1 - 1 = 0
(2-1) - 0 = 1 - 0 = 1
• So the (r-1)’s complement of 1010 in base 2 is 0101.
(r-1)’s and r’s complements
• The r's complement of a binary number is found by taking the (r-1)’s
complement of the number and then adding 1 to the result. For example,
the r's complement of the binary number 1010 would be:
(2-1) - 1 = 1 - 1 = 0
(2-1) - 0 = 1 - 0 = 1
(2-1) - 1 = 1 - 1 = 0
(2-1) - 0 = 1 - 0 = 1
• So the (r-1)’s complement of 1010 in base 2 is 0101.
• Adding 1 to the (r-1)’s complement gives:
0101 + 1 = 0110
• So the r's complement of 1010 in base 2 is 0110.
Both (r-1)’s complement and r's complement are used to represent negative
numbers in binary arithmetic, with r being the base of the number system. The
main difference between the two is that r's complement is easier to compute
because it requires only one addition operation.
Subtraction of unsigned Numbers
• Problem: Find (M – N) where both M and N are unsigned
numbers in base-r, size L digits.
• Recall that we first learned to do this subtraction using the
concept of “borrowing” – computers can be designed to do
this, but there is a simpler way using r’s complements and
addition
Binary: 10110001101
Binary Conversions - Octal, Hexadecimal
Binary: 10110001101
Binary: 10110001101
Binary: 10110001101011
Hexadecimal: 2 C 6 B
114
57 0
28 1
14 0
Quotients
7 0 Remainders
3 1
1 1
0 1
1110010
Binary/Decimal Conversions
1 0 1 1 => 1 x 2 3 + 0 x 22 + 1 x 21 + 1 x 20
Binary/Decimal Conversions
• Converting to and from binary to decimal requires some
more effort.
• Examples: Decimal fraction (0.12) to Binary
• Examples:
– Addition
– Subtraction
– Multiplication
– Division