Chapter 1
Data-representation in binary, Octal and Hex forms
The Decimal System
The decimal system is a representation of data in the form of 0,1,2,3,4,5,6,7,8,9 and is also called the Base 10 system - Deca meaning ten. In a decimal number, the decimal point separates the positive powers of 10 from the negative powers. For instance, the number 563.127 is thus equal to
563.127 = 5 x 102 + 6 x 101 + 3 x 100 + 1 x 10 -1 + 2 x 10 -2 + 7 x 10 -3
10 2 10 1 10 0 10 -1 &;nbssp;p; 10 -2 10 -3
5 |
6 |
3 |
1 |
2 |
7 |
MSD LSD
The Binary Number System
Data representation system is used primarily to implement electronic circuits. The binary system represents data in binary format i.e., in the form of 0's and 1's and hence is used to implement circuits more accurately. It is also called the Base 2 system as it takes 2 as its base to derive from integers or decimals.
For instance, 1001.01102 = 1 x 2 3 + 0 x 2 2 + 0 x 2 1 + 0 x 2 0 + 0 x 2 -1 + 1 x 2 -2 + 1 x 2 -3 + 0 x 2 -4
= 8 + 0 + 0 + 0 = 8
= 0 + 0.25 + 0.125 + 0.0625
= 8.375 10
The Octal Number System
This system is used primarily for using in digital systems. It is also called the Base 8 system. The places to the left of the octal point are positive powers of 8 and to the right are negative powers.
For instance, 4234.65
4 |
2 |
3 |
4 |
6 |
5 |
8 3 8 2 8 1 8 0 . 8 -1 &;nbssp;p; 8 -2
Similar to the other systems, the sum of products of each value, in the above box, is the decimal equivalent of the octal number - 4234.65
= 4 x 8 3 + 2 x 8 2 + 3 x 8 1 + 4 x 8 0 =2204
= 6 x 8 -1 + 5 x 8 -2 = 0.828125
= 2204.828125
The Hex Number System
This system has symbols from 0 to 9 and continues with A to F till 15. It is also called the Base 16 system. A hex digit is represented in a group of 4 digits.
5731 = 0663 16
By the repeated division method with 16
16 | 5731 Remainder 3
16 | 358 Remainder 6
16 | 22 Remainder 6
16 | 1 0
= 0663 16
Powers of 2 table (Source: Computer science - C++ by Sumita Arora)
2n |
n |
2-n |
1 |
0 |
1.0 |
2 |
1 |
0.5 |
4 |
2 |
0.25 |
8 |
3 |
0.125 |
16 |
4 |
0.0625 |
32 |
5 |
0.03125 |
64 |
6 |
0.015625 |
128 |
7 |
0.0078125 |
256 |
8 |
0.00390625 |
512 |
9 |
0.001953125 |
1024 |
10 |
0.0009765625 |
2048 |
11 |
0.00048828125 |
4096 |
12 |
0.000244140625 |
8192 |
13 |
0.0001220703125 |
16384 |
14 |
0.00006103515625 |
32768 |
15 |
0.000030517578125 |
65536 |
16 |
0.0000152587890625 |
131072 |
17 |
0.00000762939453125 |
262144 |
18 |
0.000003814697265625 |
524288 |
19 |
0.0000019073486328125 |
1048576 |
20 |
0.00000095367431640625 |
2097152 |
21 |
0.000000476837158203125 |
4194304 |
22 |
0.0000002384185791015625 |
8388608 |
23 |
0.00000011920928955078125 |
16777216 |
24 |
0.000000059604644775390625 |
33554432 |
25 |
0.0000000298023223876953125 |
67108864 |
26 |
0.00000001490116119384765625 |
134217728 |
27 |
0.000000007450580596923828125 |
268435456 |
28 |
0.0000000037252902984619140625 |
536870912 |
29 |
0.00000000186264514923095703125 |
1073741824 |
30 |
0.000000000931322574615478515625 |
2147483648 |
31 |
0.0000000004656612873077392578125 |
4294967296 |
32 |
0.00000000023283064365386962890625 |
8589934592 |
33 |
0.000000000116415321826934814453125 |
17179869184 |
34 |
0.0000000000582076609134674072265625 |
34359738368 |
35 |
0.00000000002910383045673370361328125 |
68719476736 |
36 |
0.000000000014551915228366851806640625 |
2's complement table
Binary |
Decimal |
0111 |
7 |
0110 |
6 |
0101 |
5 |
0100 |
4 |
0011 |
3 |
0010 |
2 |
0001 |
1 |
0000 |
0 |
1111 |
-1 |
1110 |
-2 |
1101 |
-3 |
1100 |
-4 |
1101 |
-5 |
1010 |
-6 |
1001 |
-7 |
1000 |
-8 |
Relationship between the different data sets
Binary |
Decimal |
Octal |
Hex |
0000 |
0 |
0 |
0 |
0001 |
1 |
1 |
1 |
0010 |
2 |
2 |
2 |
0011 |
3 |
3 |
3 |
0100 |
4 |
4 |
4 |
0101 |
5 |
5 |
5 |
0110 |
6 |
6 |
6 |
0111 |
7 |
7 |
7 |
1000 |
8 |
10 |
8 |
1001 |
9 |
11 |
9 |
1010 |
10 |
12 |
A |
1011 |
11 |
13 |
B |
1100 |
12 |
14 |
C |
1101 |
13 |
15 |
D |
1110 |
14 |
16 |
E |
1111 |
15 |
17 |
F |
ASCII code table
Character | ASCII | Octal | Hex | Character | ASCII | Octal | Hex |
A |
100 0001 |
101 |
41 |
0 |
011 0000 |
060 |
30 |
B |
100 0010 |
102 |
42 |
1 |
011 0001 |
061 |
31 |
C |
100 0011 |
103 |
43 |
2 |
011 0010 |
062 |
32 |
D |
100 0100 |
104 |
44 |
3 |
011 0011 |
063 |
33 |
E |
100 0101 |
105 |
45 |
4 |
011 0100 |
064 |
34 |
F |
100 0110 |
106 |
46 |
5 |
011 0101 |
065 |
35 |
G |
100 0111 |
107 |
47 |
6 |
011 0110 |
066 |
36 |
H |
100 1000 |
110 |
48 |
7 |
011 0111 |
067 |
37 |
I |
100 1001 |
111 |
49 |
8 |
011 1000 |
070 |
38 |
J |
100 1010 |
112 |
4A |
9 |
011 1001 |
071 |
39 |
K |
100 1011 |
113 |
4B |
SPACE |
010 1000 |
040 |
20 |
L |
100 1100 |
114 |
4C |
_ |
010 1110 |
056 |
2E |
M |
100 1101 |
115 |
4D |
( |
110 1000 |
050 |
28 |
N |
100 1110 |
116 |
4E |
+ |
010 1011 |
053 |
2B |
O |
100 1111 |
117 |
4F |
$ |
010 0100 |
044 |
24 |
P |
101 0000 |
120 |
50 |
* |
010 1010 |
052 |
2A |
Q |
101 0001 |
121 |
51 |
) |
010 1001 |
051 |
29 |
R |
101 0010 |
122 |
52 |
- |
010 1101 |
055 |
2D |
S |
101 0011 |
123 |
53 |
/ |
010 1111 |
057 |
2F |
T |
101 0100 |
124 |
54 |
' |
010 1100 |
054 |
2C |
U |
101 0101 |
125 |
55 |
= |
011 1101 |
075 |
3D |
V |
101 0110 |
126 |
56 |
ENTER |
000 1101 |
015 |
0D |
W |
101 0011 |
127 |
57 |
| |
000 1010 |
012 |
0A |
X |
101 1000 |
130 |
58 |
TAB |
000 1001 |
011 |
09 |
Y |
101 1001 |
131 |
59 |
BKSPACE |
000 1000 |
010 |
08 |
Z |
101 1010 |
132 |
5A |
|
|
|
|
In the above table, most of the keys of a keyboard have been represented in the various data-representation formats except the lower-case alphabets, the arrow keys and the special function keys
How to obtain the ASCII value of a key
Each key on the keyboard is represented by a unique code called the scan code of a key, which is an unique integer value. For example, capital 'A' has a scan code - 65 and small 'a' - 97; small 'b' - 98; small 'c' - 99 etc. After obtaining the integer value, conversion to binary through the repeated division method or power of 2 method is very simple and similarly to Octal and Hex. For a complete list of all the keys, write a program in C, which will take input using the getch() function (in conio.h). Getch() returns an integer value for the key pressed.
#include <conio.h>
#include <stdio.h>
void main()
{
int key, i=0;
while (i==0){
key=getch(); // will return scan code of key pressed into variable 'key'
printf("%d",key); // Display scan code
if (key==27) // Escape key
i=1;
}
}
ASCII is the standardised format of data transfer over distances. It is a 7-bit code and hence the making up of the 8th bit is to be noted here. There is a simple way to obtain the 8th bit to make a byte - count the number of 1's in the 7-bit code. If there are even number of 1's add 0 as the MSB or 1 (to the leftmost bit of the byte).