2.1. Number Systems#
Throughout this course, we’ll use three types of number systems, which are Decimal, Binary and Hexadecimal. All three systems are positional number systems. The major difference between these systems is the base or radix that is used. Let’s start with the on that we’ve been using since childhood, i.e. Decimal.
2.1.1. Decimal#
The decimal number system has a base of 10 because it uses ten digits from 0 through 9. In the decimal number system, the positions successive to the left of the decimal point represent units, tens, hundreds, thousands and so on. Thus, every position shows a particular power of the base (10). Consider number \(2023\) as an example. The mathematical expression representing this number is
Note that the number \(2023\) is nothing but the coefficients of increasing powers of \(10\) in the equation above. This number system is mostly used to do regular math operations in a code since majority of math nowadays uses decimal number system. However, it is always stored in binary format in a computer.
2.1.2. Binary#
The base of binary number system is 2 and it thus uses only two digits, 0 and 1, to write a number. Binary numbers are generally prefixed with 0b
in C/C++ programming. Similar to decimal system, the mathematical expression representing 0b0000011111100111
is
Note that the binary number above is padded with zeros. This is done just to comply with the standard datatype sizes of 8-bit, 16-bit, 32-bit, etc. in C/C++ programming. Also note that the number of digits required, even if the padded zeros are ignored, is much higher than the decimal system.
All digital computers store information in binary number system. This is easier to implement since the two digits of this system, i.e. digit \(0\) and digit \(1\), can be represented by a zero voltage and a positive voltage respectively.
Take a look at following video if you were wondering how negative numbers might be represented in binary.
2.1.3. Hexadecimal#
This number system uses a base of 16 which require 16 digits, 0 through 9 and then a through f. Hexadecimal numbers are prefixed with 0x
in C/C++ programming. The mathematical expression to convert 0x7E7
into its decimal counterpart is
Note that letter/digit E
above is treated as number \(14\) to do the math. In fact, digits a through f are treated as numbers 10 through 15 for math’s sake. Also note that the hexadecimal system is not case-sensitive, i.e. letters/digits E
and e
both mean number \(14\).
The hexadecimal number system requires less number of digits than both binary and decimal system. Thus, it is generally used to define memory or register addresses, i.e. pointers in C/C++ programming language.