COA Chapter 2
COA Chapter 2
Data Representation
Binary Terminology
When writing values in decimal, it is common to separate the places or positions of large
numbers in groups of three digits separated by commas. For example, 34532374510 is typically
written 345,323,74510 showing that there are 345 millions, 323 thousands, and 745 ones. This
practice makes it easier to read and comprehend the magnitude of the numbers. Binary numbers
are also divided into components depending on their application. Each binary grouping has been
given a name.
To begin with, a single place or position in a binary number is called a bit, short for binary digit.
For example, the binary number 01102 is made up of four bits. The rightmost bit, the one that
represents the ones place, is called the Least Significant Bit or LSB. The leftmost bit, the one
that represents the highest power of two for that number, is called the Most Significant Bit or
MSB. Note that the MSB represents a bit position. It doesn't mean that a '1' must exist in that
position.
The next four terms describe how bits might be grouped together.
Byte – A unit of storage for a single character, typically an eight bit (2 nibble) binary number
(short for binary term)
Bit 12
Nibble 10102
Byte 101001012
Word 10100101111100002
In general, (anan-1… a0 . f1f2 … fm)10 = (an x 10n) + (an-1x10n-1) + … + (a0 x 100) + (f1 x 10-1) + (f2 x
10-2) + … + (fm x 10-m)
Decimal-to-Binary Conversion
Step 1: Break the number in two parts: Whole number and fraction part.
Step 2: Repeated Division-by-2 Method (for whole numbers)
To convert a whole number to binary, use successive division by 2 until the quotient is 0.
The remainders form the answer, with the first remainder as the least significant bit
(LSB) and the last as the most significant bit (MSB). (43)10 = (101011)2
2 43
2 21 rem 1 LSB
2 10 rem 1
2 5 rem 0
2 2 rem 1
2 1 rem 0
0 rem 1 MSB
Binary-Octal/Hexadecimal Conversion
- (12)10, - (1100)2
0 for +
1 for -
Example: an 8-bit number can have 1-bit sign and 7-bits magnitude.
1 0000000 -(0)10
Example:
1 1111111
0 = +ve; 1 = -ve.
Given a number x which can be expressed as an n-bit binary number, its negative value
can be obtained in 1s-complement representation using:
- x = 2n - x - 1
-(00001100)2 = - (12)10
= (28 - 12 - 1)10
= (243)10
= (11110011)1s
2s Complement
2s complement of an unsigned number is obtained by inverting all the bits and adding 1.
Examples:
= (11111111)2s (add 1)
= (10000010)2s (add 1)
Example:
-(00001100)2 = - (12)10
= (28 - 12)10
= (244)10
= (11110100)2s
Complements
(i) 1s Complement
(ii) 2s Complement
Use of complements
Hence, the computer needs only binary adder and complementing circuit to
handle both addition and subtraction
2s complement addition
Algorithm:
Check for overflow: Overflow occurs if the carrier into and out of the MSB are
different.
2s complement subtraction
A-B = A + (-B)
Examples: 2s addition/Subtraction
4-bits system:
The signed and unsigned numbers representation given are fixed point numbers.
The binary point is assumed to be at a fixed location, say, at the end of the number:
(001010.11)2s = (10.75)10
(111110.11)2s = -(000001.01)2
= -(1.25)10
Fixed point numbers have limited range.
To represent very large or very small numbers, we use floating point numbers (cf.
scientific numbers). Examples:
A 16-bit floating point number may have 10-bit mantissa and 6-bit exponent.
More bits in exponent gives larger range.
More bits for mantissa gives better precision.
Arithmetic is more difficult for floating point numbers.
MULTIPLICATION
Example:
= (0.24 x 1031)10
ADDITION
(iii) normalise
Example:
= (0.14 x 103)10
Decimal numbers are more natural to humans. Binary numbers are natural to
computers. Quite expensive to convert between the two.
If little calculation is involved, we can use some coding schemes for decimal numbers.
One such scheme is BCD, also known as the 8421 code.
Represent each decimal digit as a 4-bit binary code.
Decimal digit 0 1 2 3 4
Decimal digit 5 6 7 8 9
Some codes are unused, eg: (1010)BCD, (1011) BCD, …, (1111) BCD. These codes are
considered as errors.
Easy to convert, but arithmetic operations are more complicated.
Suitable for interfaces such as keypad inputs and digital readouts.
Examples:
Alphanumeric codes
ASCII: 7-bit, plus a parity bit for error detection (odd/even parity).
EBCDIC: 8-bit code
Errors can occur data transmission. They should be detected, so that re-transmission can
be requested.
With binary numbers, usually single-bit errors occur.