I expect that the readers of this blog has a basic understanding of the C programming language.
Topics Covered:
Hexadecimal Number System
Handling negative numbers in digital world.
Hexadecimal Number System
The decimal, or base-10, system of numbers that we use on a daily basis uses the 10 symbols from 0-9 to represent values.
The hexadecimal number system, commonly known as base-16, is a system of numbers that use 16 different symbols to represent values. They are 0-9 and A-F symbols.
Whereas, the binary number system or base-2 system of numbers use 0's and 1's to represent values.
Decimal, Hexadecimal and Binary Representation of Numbers
Decimal | Hexadecimal | Binary |
0 | 0x0 | 0000 |
1 | 0x1 | 0001 |
2 | 0x2 | 0010 |
3 | 0x3 | 0011 |
4 | 0x4 | 0100 |
5 | 0x5 | 0101 |
6 | 0x6 | 0110 |
7 | 0x7 | 0111 |
8 | 0x8 | 1000 |
9 | 0x9 | 1001 |
10 | 0xA | 1010 |
11 | 0xB | 1011 |
12 | 0xC | 1100 |
13 | 0xD | 1101 |
14 | 0xE | 1110 |
15 | 0xF | 1111 |
16 | 0x10 | 10000 |
17 | 0x11 | 10001 |
18 | 0x12 | 10010 |
Handling negative numbers in digital world
In digital systems, negative numbers are represented using the two's complement method.
Example 1: Convert -2 to its equivalent two's complement
step 1:
Take the binary equivalent of 2.
It is 0000 0010
step 2:
Take the 1's complement of the binary number i.e., invert all 0's to 1's and all 1's to 0's.
0000 0010 -> 1111 1101
step 3:
Add '1' to the 1's complement gives 2's complement.
1111 1101
+ 1
---------------
1111 1110
Now let's check if the result is correct.
2+(-2) = 0
0000 0010
+1111 1110
-----------------
0000 0000
so, the result is correct.
Suppose that the CPU has only 3 bits to store a number. What is the maximum number of positive and negative numbers that can be stored? How are positive and negative numbers represented?
The MSB(most significant bit) is the sign bit used to represent negative numbers using 2's complement.
Decimal | Binary |
| MSB |
0 | 0 00 |
1 | 0 01 |
2 | 0 10 |
3 | 0 11 |
-4 | 1 00 |
-3 | 1 01 |
-2 | 1 10 |
-1 | 1 11 |
Happy learning!!!!!!
This is the link to the previous blog from this series C programming for Embedded systems 3 (risefromashes.blog)
For other exciting contents visit my website Motivation | General (risefromashes.blog)
Comments