Numbers in mikroPascal.


Table of contents:
Introduction
Binary Numbers
Hexadecimal Numbers
BCD Numbers
Octal Numbers
Real Numbers
Appendix: binary, decimal, hexadecimal value table.


1. Introduction.

As you perhaps know, the representation of numbers in computers is in binary code. The binary value system though is only one of the (many) possible value systems used by people. People use decimal, hexadecimal, octal, BCD, ... etc to get rid of the (for people difficult to handle) binary notation.

In mikroPascal the 3 types of values known are:

Type Example
Decimal 126
Binary %01111110
HexaDecimal $7E or 0x7E


To make mP aware that a number is not a decimal one, the prefixes "%" and "$" (or "0x") are added. Those prefixes are not a part of the number itself.
mikroPascal has no prefix for Octal or BCD notation.

In this document only "whole" numbers are discussed, no "reals" or "floats". The coding of those are much more compiler dependant, though there are standards for them.

2. Binary numbers.

In the binary system each binary digit (or bit) can have only 2 values: 0 and 1. (In our decimal system every digit can have 10 values: 0..9.)
This means that binary numbers will look e.g. like this: 010010001110...

Like in the decimal system, each digit has a "weight" (or "significance"), exactly like in the well known decimal system, where the weight of the most right hand digit (units) is "1", the weight of the one left of it (tens) is "10" and so on.

Of course the weight of the digits (bits) in the binary system differs from that in the decimal system. It is as follows:
Bitnumber: 7 6 5 4 3 2 1 0
Weight (dec): 128 64 32 16 8 4 2 1

As you can see, the digits are numbered from right to left, the most right hand digit has the weight "1", and the weight doubles for each digit more to the left. Bit 0 is called the "least significant bit", bit 7 is called the "most significant bit". As with decimal numbers, the total value of a number is the sum of all digits multiplied with their weight.
This means that the binary number 01101 equals (0 * 16) + (1 * 8) + (1 * 4) + (0 * 2) + (1 * 1) = 13 decimal.

A number of 4 bits is called a nibble. It can have a decimal value from 0 to 15. There is no "nibble" type in mikroPascal however.
A number of 8 bits is called a byte. It can have a decimal value from 0 to 255.
A number of 16 bits is called a word. It can have a decimal value from 0 to 65536.
A number of 32 bits is called a dword. It can have a decimal value from 0 to 4294967295.

The numbers above are called "unsigned" because their minimal value is zero (which is considered positive).

Negative Binary numbers.

Binary numbers are also used to hold negative values (those are called "signed" numbers). In this case the meaning of the bits is different: Keep in mind that all bits available in the number (e.g.8 in a byte) are used to represent negative values. The value -1 in a signed byte is 11111111, the same value -1 in a signed word is 1111111111111111.

The "signed" counterparts of most types above are:
A signed number of 8 bits is called a short or shortint. It can have a decimal value from -128 to +127.
A signed number of 16 bits is called a integer. It can have a decimal value from -32768 to +32767.
A signed number of 32 bits is called a longint. It can have a decimal value from -2147483648 to +2147483647.

3. HexaDecimal Numbers.

The hexadecimal system is based on 16 different digits (remember that our decimal system has 10). Each hexadecimal (or hex for short) digit is capable of representing the value of 1 binary nibble (or 4 bits). The hex notation is easy to translate to/from binary (see table below), that is why it is used very often as replacement for it. The first 10 digit values are chosen the same as the decimal digits 0..9, the rest of the digits are chosen to be A..F. The latter are (in this case anyway) not characters but hexadecimal digits.

The relation between binary, hexadecimal and decimal is as follows:

Binary Hexa
Decimal
Decimal
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15
So, the 8 bits number 01011101 is represented in hex as 5D. 16 bits numbers are represented by 4 hex digits etc...
Also here the idea of digit weight (or significance) holds: in the hex number C9 the "C" is the high order digit, the "9" is the low order digit. They represent respectively the high order nibble and the low order nibble in their binary counterpart.



4. BCD Numbers.

BCD (Binary Coded Decimal) coded numbers are used to represent decimal numbers. The coding however is not binary as usual. In stead each decimal digit is binary coded into one nibble. So, for the decimal number 46 two nibbles are needed: 0100 and 1100. This also means that a byte (2 nibbles) can hold BCD numbers with 2 digits (0 to 99), and a word (2 bytes, 4 nibbles) can hold BCD numbers with 4 digits (0..9999). The BCD coding looks very much like hex coding (one digit per nibble), but only the digits 0..9 are used of course. The hex number $99 looks in binary exactly the same as the BCD number 99. There is no way that mP can distinguish those two, it is up to the programmer to interprete then differently.
This means also that e.g. assigning the literal decimal value 12 to a byte using BCD coding is done with:
 ByteVar := $12;	// assigns the decimal value 12 in BCD to ByteVar
The reason for the BCD coding is the easy conversion possibilities from BCD numbers to e.g. strings. Some devices (like e.g. the real time clock PCF8583P) give their output in BCD.

The mE library contains a number of routines to handle BCD: More BCD related functions can be found in Additional String Utils, and in Strng Utils.

5. Octal Numbers.

This is a type of notation not used very much in the microprocessor world. It works with digits ranging from 0..7, with groups of 3 bits binary coded in each octal digit. So, the octal number 345 equals 011 100 101 binary. As you can see, the 3 bit choice does not fit very well in terms of bytes and words.

6. Real Numbers (floats).

This issue is not under discussion in this document. The coding of those are much more compiler dependant, though there are standards for them.

Appendix: Binary, decimal and hexadecimal table of byte values.

00000000
0
$00
00000001
1
$01
00000010
2
$02
00000011
3
$03
00000100
4
$04
00000101
5
$05
00000110
6
$06
00000111
7
$07
00001000
8
$08
00001001
9
$09
00001010
10
$0A
00001011
11
$0B
00001100
12
$0C
00001101
13
$0D
00001110
14
$0E
00001111
15
$0F
00010000
16
$10
00010001
17
$11
00010010
18
$12
00010011
19
$13
00010100
20
$14
00010101
21
$15
00010110
22
$16
00010111
23
$17
00011000
24
$18
00011001
25
$19
00011010
26
$1A
00011011
27
$1B
00011100
28
$1C
00011101
29
$1D
00011110
30
$1E
00011111
31
$1F
00100000
32
$20
00100001
33
$21
00100010
34
$22
00100011
35
$23
00100100
36
$24
00100101
37
$25
00100110
38
$26
00100111
39
$27
00101000
40
$28
00101001
41
$29
00101010
42
$2A
00101011
43
$2B
00101100
44
$2C
00101101
45
$2D
00101110
46
$2E
00101111
47
$2F
00110000
48
$30
00110001
49
$31
00110010
50
$32
00110011
51
$33
00110100
52
$34
00110101
53
$35
00110110
54
$36
00110111
55
$37
00111000
56
$38
00111001
57
$39
00111010
58
$3A
00111011
59
$3B
00111100
60
$3C
00111101
61
$3D
00111110
62
$3E
00111111
63
$3F
01000000
64
$40
01000001
65
$41
01000010
66
$42
01000011
67
$43
01000100
68
$44
01000101
69
$45
01000110
70
$46
01000111
71
$47
01001000
72
$48
01001001
73
$49
01001010
74
$4A
01001011
75
$4B
01001100
76
$4C
01001101
77
$4D
01001110
78
$4E
01001111
79
$4F
01010000
80
$50
01010001
81
$51
01010010
82
$52
01010011
83
$53
01010100
84
$54
01010101
85
$55
01010110
86
$56
01010111
87
$57
01011000
88
$58
01011001
89
$59
01011010
90
$5A
01011011
91
$5B
01011100
92
$5C
01011101
93
$5D
01011110
94
$5E
01011111
95
$5F
01100000
96
$60
01100001
97
$61
01100010
98
$62
01100011
99
$63
01100100
100
$64
01100101
101
$65
01100110
102
$66
01100111
103
$67
01101000
104
$68
01101001
105
$69
01101010
106
$6A
01101011
107
$6B
01101100
108
$6C
01101101
109
$6D
01101110
110
$6E
01101111
111
$6F
01110000
112
$70
01110001
113
$71
01110010
114
$72
01110011
115
$73
01110100
116
$74
01110101
117
$75
01110110
118
$76
01110111
119
$77
01111000
120
$78
01111001
121
$79
01111010
122
$7A
01111011
123
$7B
01111100
124
$7C
01111101
125
$7D
01111110
126
$7E
01111111
127
$7F
10000000
128
$80
10000001
129
$81
10000010
130
$82
10000011
131
$83
10000100
132
$84
10000101
133
$85
10000110
134
$86
10000111
135
$87
10001000
136
$88
10001001
137
$89
10001010
138
$8A
10001011
139
$8B
10001100
140
$8C
10001101
141
$8D
10001110
142
$8E
10001111
143
$8F
10010000
144
$90
10010001
145
$91
10010010
146
$92
10010011
147
$93
10010100
148
$94
10010101
149
$95
10010110
150
$96
10010111
151
$97
10011000
152
$98
10011001
153
$99
10011010
154
$9A
10011011
155
$9B
10011100
156
$9C
10011101
157
$9D
10011110
158
$9E
10011111
159
$9F
10100000
160
$A0
10100001
161
$A1
10100010
162
$A2
10100011
163
$A3
10100100
164
$A4
10100101
165
$A5
10100110
166
$A6
10100111
167
$A7
10101000
168
$A8
10101001
169
$A9
10101010
170
$AA
10101011
171
$AB
10101100
172
$AC
10101101
173
$AD
10101110
174
$AE
10101111
175
$AF
10110000
176
$B0
10110001
177
$B1
10110010
178
$B2
10110011
179
$B3
10110100
180
$B4
10110101
181
$B5
10110110
182
$B6
10110111
183
$B7
10111000
184
$B8
10111001
185
$B9
10111010
186
$BA
10111011
187
$BB
10111100
188
$BC
10111101
189
$BD
10111110
190
$BE
10111111
191
$BF
11000000
192
$C0
11000001
193
$C1
11000010
194
$C2
11000011
195
$C3
11000100
196
$C4
11000101
197
$C5
11000110
198
$C6
11000111
199
$C7
11001000
200
$C8
11001001
201
$C9
11001010
202
$CA
11001011
203
$CB
11001100
204
$CC
11001101
205
$CD
11001110
206
$CE
11001111
207
$CF
11010000
208
$D0
11010001
209
$D1
11010010
210
$D2
11010011
211
$D3
11010100
212
$D4
11010101
213
$D5
11010110
214
$D6
11010111
215
$D7
11011000
216
$D8
11011001
217
$D9
11011010
218
$DA
11011011
219
$DB
11011100
220
$DC
11011101
221
$DD
11011110
222
$DE
11011111
223
$DF
11100000
224
$E0
11100001
225
$E1
11100010
226
$E2
11100011
227
$E3
11100100
228
$E4
11100101
229
$E5
11100110
230
$E6
11100111
231
$E7
11101000
232
$E8
11101001
233
$E9
11101010
234
$EA
11101011
235
$EB
11101100
236
$EC
11101101
237
$ED
11101110
238
$EE
11101111
239
$EF
11110000
240
$F0
11110001
241
$F1
11110010
242
$F2
11110011
243
$F3
11110100
244
$F4
11110101
245
$F5
11110110
246
$F6
11110111
247
$F7
11111000
248
$F8
11111001
249
$F9
11111010
250
$FA
11111011
251
$FB
11111100
252
$FC
11111101
253
$FD
11111110
254
$FE
11111111
255
$FF

-------------------------------------------