Computer Science 2210
Notes
Chapter 1 Data representation
Topic: 1.1 Number systems
DIGITAL NUMBER SYSTEM
A digital system can understand positional number system only where there are a few symbols called
digits and these symbols represent different values depending on the position they occupy in the number.
A value of each digit in a number can be determined using:
The digit
The position of the digit in the number
The base of the number system (where base is defined as the total number of digits available in
the number system).
You should understand the following number systems which are frequently used in computers.
S.N. Number System & Description
1 Binary Number System
Base 2. Digits used: 0, 1
2 Decimal/Denary Number System
Base 10. Digits used: 0 to 9
3 Hexadecimal Number System
Base 16. Digits used: 0 to 9, Letters used: A to F
Binary Number System
Characteristics:
Uses two digits, 0 and 1.
Also called base-2 number system
Each position in a binary number represents a power of the base (2). Example: 20
Last position in a binary number represents an x power of the base (2). Example: 2 x where x
represents the last position ─ 1.
Example:
Binary Number: 101012
Decimal/Denary Number System
Characteristics:
Uses 10 digits, 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.
Also called base-10 number system
Each position in a denary number represents a power of the base (10). Example: 100
Last position in a denary number represents an x power of the base (10). Example: 10x where x
represents the last position ─ 1.
Example:
Denary Number: 532410
Hexadecimal Number System
Characteristics:
Uses 10 digits and 6 letters, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F.
Letters represents numbers starting from 10. A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
Also called base-16 number system.
Each position in a hexadecimal number represents a power of the base (16). Example 160.
Last position in a hexadecimal number represents an x power of the base (16). Example 16x where
x represents the last position−1.
Example:
Hexadecimal Number: 19FDE16
Compiled by: Md. Monirul Islam Page | 1
Uses of hexadecimal system
Hexadecimal system is used to represent the following things:
Error codes
MAC addresses
IPv6 addresses
HTML colour codes
NUMBER SYSTEM CONVERSION
There are many methods or techniques which can be used to convert numbers from one base to another.
We'll demonstrate here the following:
Decimal to Other Base System
Other Base System to Decimal
Binary to Hexadecimal
Hexadecimal to Binary
Decimal to Other Base System
Steps
Step 1 − Divide the decimal number to be converted by the value of the new base.
Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new
base number.
Step 3 − Divide the quotient of the previous divide by the new base.
Step 4 − Record the remainder from Step 3 as the next digit (to the left) of the new base
number.
Repeat Steps 3 and 4, getting remainders from right to left, until the quotient becomes zero in
Step-3.
The last remainder thus obtained will be the Most Significant Digit (MSD) of the new base number.
Example:
Decimal Number: 2910
Calculating Binary Equivalent
Steps Operation Quotient Remainder
1 29 / 2 14 1
2 14 / 2 7 0
3 7/2 3 1
4 3/2 1 1
5 1/2 0 1
As mentioned in Steps 2 and 4, the remainders have to be arranged in the reverse order so that the first
remainder becomes the Least Significant Digit (LSD) and the last remainder becomes the Most Significant
Digit (MSD).
Decimal Number, 2910 = Binary Number, 111012.
Other Base System to Decimal System
Steps
Step 1 − Determine the column (positional) value of each digit (this depends on the position
of the digit and the base of the number system).
Step 2 − Multiply the obtained column values (in Step 1) by the digits in the corresponding
columns.
Step 3 − Sum the products calculated in Step 2. The total is the equivalent value in decimal.
Compiled by: Md. Monirul Islam Page | 2
Example
Binary Number, 111012
Calculating Decimal Equivalent
Steps Decimal Number
1 ((1 × 24) + (1 × 23) + (1 × 22) + (0 × 21) + (1 × 20))10
2 (16 + 8 + 4 + 0 + 1)10
3 2910
Binary Number, 111012 = Decimal Number, 2910
Binary to Hexadecimal
Steps
Step 1 − Divide the binary digits into groups of four (starting from the right).
Step 2 − Convert each group of four binary digits to one hexadecimal symbol.
Example
Binary Number, 101012
Calculating hexadecimal Equivalent
Steps Hexadecimal Number
1 0001 0101
2 110 510
3 1516
Binary Number, 101012 = Hexadecimal Number, 1516
Hexadecimal to Binary
Steps
Step 1 − Convert each hexadecimal digit to a 4-digit binary number (the hexadecimal digits may
be treated as decimal for this conversion).
Step 2 − Combine all the resulting binary groups (of 4 digits each) into a single binary number.
Example
Hexadecimal Number, 1516
Calculating Binary Equivalent
Step Hexadecimal Number Binary Number
Step 1 1516 110 510
Step 2 1516 00012 01012
Step 3 1516 000101012
Hexadecimal Number, 1516 = Binary Number, 101012
Representation of signed integers
In many cases we need to identify whether the number is positive or negative, so we use a signed
integer.
1. Sign-Magnitude Representation
A signed integer can just have the binary code for the value with an extra bit to define the sign.
This is referred to as ‘sign and magnitude representation’. For this the convention is to use a 0 to
represent ‘+’ and a 1 to represent ‘─’. For example,
Signed denary number to
+7 00000111
+1 00000001
+0 00000000
–0 100000000
–1 10000001
–7 10000111
–8 Not represented
Compiled by: Md. Monirul Islam Page | 3
Advantages:
Easy to Read
Disadvantages:
Two Representatives for Zero
Using our 4-bit Sign-Magnitude example we have,
+0 = 0000
─0 = 1000
Arithmetic is difficult
Example, lets add Positive 2 to Negative 4, which we expect the answer to be Negative 2.
+2 0010
─4 1100
---- ------
─6 1110
The result is -6, which is not what we expect
Another Example lets add +2 to ─2, we expect the answer to be zero
+2 0010
─2 1010
---- -----
0 1100
The result of ─4, which again is not what we expect
2. Two's Complement Representation
Two's complement is the method computers use to represent Signed numbers.
The most-significant-bit is the sign bit.
0 = positive value
1 = negative value
To represent a positive denary integer value as the equivalent two’s complement binary form, the
process is as follows.
Convert the denary value to a binary value.
Add a 0 in front of this binary value to fill in the required number of bits.
The negative representation of a number is created by
Start with the positive number
Flip the bits (Change '0' to '1' and '1' to '0') to get 1’s complement
Add the value 1 to get 2’s complement
One’s complement: the binary number obtained by subtracting each digit in a binary number from 1
Two’s complement: the one’s complement of a binary number, plus 1
Methods for converting a negative number expressed in two’s complement form to the corresponding
denary number
Consider the two’s complement binary number 10110001.
Method 1. Convert to the corresponding positive binary number then convert to denary before adding
the minus sign
Converting 10110001 to two’s complement leaves unchanged the 1 in the least significant bit
position then changes all of the remaining bits to produce 01001111.
Compiled by: Md. Monirul Islam Page | 4
You ignore the leading zero and apply one of the methods from Section 1.01 to convert the
remaining binary to denary which gives 79.
You add the minus sign to give −79.
Method 2. Sum the individual place values but treat the most significant bit as a negative value
You follow the approach illustrated in Table below to convert the original binary number 10110001 as
follows:
You now add the values in the bottom row to get −79.
Advantages:
One value for Zero.
Conversion from Positive to Negative and Negative to Positive numbers is easy and
consistent.
Example:
+ 2 = 0010
Flip the bits = 1101 and add 1 makes 1110 which is ─2
─2 = 1110
Flip the bits = 0001 and add 1 makes 0010 which is +2
Arithmetic is easy.
Example, lets add Positive 2 to Negative 4, which we expect the answer to be Negative 2.
+2 0010
─4 1100
---- ------
─2 1110
The result is ─2 as expected!
Another Example lets add +2 to ─2, we expect the answer to be zero
+2 0010
─2 1110
---- -------
0 0000
The result of zero, as expected!
The carry of 1 from the sign bit is ignored.
Disadvantages:
Difficult to read
Compiled by: Md. Monirul Islam Page | 5
Binary addition
Compiled by: Md. Monirul Islam Page | 6