Numeral Systems: Binary, Decimal and Hexadecimal Numbers
Numeral Systems: Binary, Decimal and Hexadecimal Numbers
Svetlin Nakov
Telerik Corporation
www.telerik.com
Table of Contents
1. Numerals Systems
Binary and Decimal Numbers
Hexadecimal Numbers
Conversion between Numeral Systems
3. Representation of Numbers
Positive and Negative Integer Numbers
Floating-Point Numbers
4. Text Representation
2
Numeral Systems
Conversion between Numeral Systems
Decimal Numbers
Decimal numbers (base 10)
4
Binary Numeral System
Binary numbers are represented by sequence
of bits (smallest unit of information 0 or 1)
Bits are easy to represent in electronics
1 01 10 1 0
1 10 1 10
5
Binary Numbers
Binary numbers (base 2)
Represented by 2 numerals: 0 and 1
Each position represents a power of 2:
101b = 1*22 + 0*21 + 1*20 = 100b + 1b = 4+1=
= 5
110b = 1*22 + 1*21 + 0*20 = 100b + 10b = 4 + 2 =
= 6
110101b = 1*25 + 1*24 + 0*23 + 1*22 + 0*21 + 1*20 =
= 32 + 16 + 4 + 1 =
= 53
6
Binary to Decimal Conversion
Multiply each numeral by its exponent:
1001b = 1*23 + 1*20 = 1*8 + 1*1 =
= 9
0111b = 0*23 + 1*22 + 1*21 + 1*20 =
= 100b + 10b + 1b = 4 + 2 + 1 =
= 7
110110b = 1*25 + 1*24 + 0*23 + 1*22 + 1*21 =
= 100000b + 10000b + 100b + 10b =
= 32 + 16 + 4 + 2 =
= 54
7
Decimal to Binary Conversion
Divide by 2 and append the reminders in
reversed order:
500/2 = 250 (0)
250/2 = 125 (0)
125/2 = 62 (1)
62/2 = 31 (0) 500d = 111110100b
31/2 = 15 (1)
15/2 = 7 (1)
7/2 = 3 (1)
3/2 = 1 (1)
1/2 = 0 (1)
8
Hexadecimal Numbers
Hexadecimal numbers (base 16)
10
Hexadecimal to Decimal
Conversion
Multiply each digit by its exponent
1F4hex = 1*162 + 15*161 + 4*160 =
= 1*256 + 15*16 + 4*1 =
= 500d
FFhex = 15*161 + 15*160 =
= 240 + 15 =
= 255d
11
Decimal to Hexadecimal
Conversion
Divide by 16 and append the reminders in
reversed order
500/16 = 31 (4)
1/16 = 0 (1)
12
Binary to Hexadecimal
(and Back) Conversion
The conversion from binary to hexadecimal
(and back) is straightforward: each hex digit
corresponds to a sequence of 4 binary digits:
0x0 = 0000 0x8 = 1000
0x1 = 0001 0x9 = 1001
0x2 = 0010 0xA = 1010
0x3 = 0011 0xB = 1011
0x4 = 0100 0xC = 1100
0x5 = 0101 0xD = 1101
0x6 = 0110 0xE = 1110
0x7 = 0111 0xF = 1111
13
Numbers Representation
Positive and Negative Integers and
Floating-Point Numbers
Representation of Integers
A short is represented by 16 bits
100 = 26 + 25 + 22 =
= 00000000 01100100
An int is represented by 32 bits
65545 = 216 + 23 + 20 =
= 00000000 00000001 00000000 00001001
A char is represented by 16 bits
0 = 48 = 25 + 24 =
= 00000000 00110000
15
Positive and Negative Numbers
A number's sign is
determined by the
Most Significant Bit (MSB)
Only in signed integers: sbyte, short, int, long
Leading 0 means positive number
Leading 1 means negative number
Example: (8 bit numbers)
0XXXXXXXb > 0 e.g. 00010010b = 18
00000000b = 0
1XXXXXXXb < 0 e.g. 10010010b = -110
16
Positive and Negative Numbers (2)
The largest
positive 32-bit int number is:
2 147 483 647 (231 - 1) = 0111111111b
The smallest negative 32-bit number is:
-2 147 483 648 (-231) = 1000000000b
17
Representation of 8-bit Numbers
Positive 8-bit numbers have the +127 = 01111111
format 0XXXXXXX ...
+3 = 00000011
Their value is the decimal of
+2 = 00000010
their last 7 bits (XXXXXXX)
+1 = 00000001
Negative 8-bit numbers have +0 = 00000000
the format 1YYYYYYY -1 = 11111111
Their value is 128 (27) minus (-)
-2 = 11111110
the decimal of YYYYYYY -3 = 11111101
7
...
10010010b = 2 10010b =
-127 = 10000001
= 128 - 18 = -110
-128 = 10000000
18
Floating-Point Numbers
Floating-point numbers representation
(according to the IEEE 754 standard*):
2k-1 20 2-1 2-2 2-n
S P0 ... Pk-1 M0 M1 ... Mn-1
* See http://en.wikipedia.org/wiki/Floating_point
Example:
1 10000011 01010010100000000000000
Questions?
http://academy.telerik.com
Exercises
1. Write a program to convert decimal numbers to their
binary representation.
2. Write a program to convert binary numbers to their
decimal representation.
3. Write a program to convert decimal numbers to their
hexadecimal representation.
4. Write a program to convert hexadecimal numbers to
their decimal representation.
5. Write a program to convert hexadecimal numbers to
binary numbers (directly).
6. Write a program to convert binary numbers to
hexadecimal numbers (directly).
25
Exercises (2)
7. Write a program to convert from any numeral system
of given base s to any other numeral system of base
d (2 s, d 16).
8. Write a program that shows the binary
representation of given 16-bit signed integer number
(the C# type short).
9. * Write a program that shows the internal binary
representation of given 32-bit signed floating-point
number in IEEE 754 format (the C# type float).
Example: -27,25 sign = 1, exponent = 10000011,
mantissa = 10110100000000000000000.
26