Number Systems in computer programming

Numbers are represented in different ways in computers. From the user or programmer’s perspective, numbers are represented in decimals, but at the machine level numbers are represented in binaries.

Basically, number systems are ways of representing numbers. Naturally, we count in the decimal number system. Here, the smallest digit is 0 and the largest is 9. In computing, numbers are represented internally in computers in binaries.

At the basic or machine level, computers only understand instructions in the form of zeros and ones. Programming languages such as Python allow programmers to write codes in letters, numbers and symbols, but these instructions are converted to streams of 0s and 1s.

There are essentially four kinds of number systems used in computing and programming in particular and they include:

  • Decimal
  • Binary
  • Hexadecimal
  • Octal

Decimal number system

Many people are familiar with this number system. There are ten digits in the decimal number system, 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. The lowest digit is 0 while the highest digit is 9.

Decimal numbers are referred to as base 10 numbers.

Let’s take a look at this decimal number – 110.

This is the same thing as 100 + 10 + 0.

In essence, every digit in the number has a positional value.

In this case, the first digit by the left has the value of 100, the second 10 and the last 0.

You might as well express this in another way, using the powers of the base of the number, as shown below:

1×102 + 1×101 + 0x100 = 1100

= 110.

Take a look at the following examples:

Example 1: Express 98110 in the powers of its base.

= 9×102 + 8×101 + 1×100

= 9×100 + 8×10 + 1×0 = 98110

Example 2: Express 34 in the powers of its base.

= 3×101 + 4×100

= 3×10 + 4×1 = 3410

Binary number system

This is the most basic way of representing numbers. There are only two digits in the binary number system.  The lowest digit is 0 and the highest digit is 1. Binary digits are called bits while numbers in this system are referred to as base 2.

When counting in binaries, the first number is 0, followed by 1 and then 10. The table below shows decimal numbers and their equivalents in binaries.

binary and decimal
Decimal and binary numbers

Conversion from Binary to Decimal

The easiest way to express a binary number in a decimal number system is by expressing the digits in powers of base 2.

A binary number such as 1012 can be represented thus:

= 1×22 + 0x21 + 1×20

= 1×4 + 0x2 + 1×1 = 510

Also, the number 110112, which is a binary number is the same as the following in decimal.

= 1×24 + 1×23 + 0x22 + 1×21 + 1×20

= 1×16 + 1×8 + 0x4 + 1×2 + 1×1

This is equal to 2710

Converting from Decimal to Binary

To convert a decimal number to binary, the decimal number is divided repeatedly by 2 noting down the quotient and remainder until the quotient becomes 0.

The following illustrates how to convert decimal numbers to binary equivalents.

decimal to binary number

Binary Addition

The addition of binary numbers goes with the concept that 1 + 1 = 10. Now, let’s provide some illustrations.

adding binary numbers
Adding binary numbers

Binary subtraction

The following examples show how to perform subtractions on binary numbers.

subtracting binary numbers
subtracting binary numbers

Representing negative binary numbers

Representing negative binary numbers is not as straightforward as in decimal number systems. It is done using a concept known as the 2’s complement.

But first, you have to determine the 1’s complement. The one’s complement of a binary number is determined by reversing the bits in the number.

For instance, the 1’s complement of the number 101011 by reversing the bits. In this case, the digit 0 is changed to 1 and the digit 1 is changed to 0.

negative binaries

So, the 1’s complement of 101 is 010100.

Add 1 to the 1’s complement to get the 2’s complement.

This will give 010101.

To confirm that it is truly a negative number, you can add it up. Adding up the same number with opposite signs will give you 0.

If you 101011 and 010101 you will get 0.

The last digit on the left is considered the sign. 0 for positive and 1 for negative.

Signed Magnitudes

The signed magnitudes are a way of representing binary numbers where the most significant or last digit on the left represents the sign of the binary number. The number is positive if the last digit is 0 and negative if the last digit is 1.

For 4 bits, the first digit by the left is considered the most significant digit and denotes the sign, while the remaining 3 digits represent the number. In the 8 bits, the 7 digits denote the number while the first digit from the left denotes the sign.

signed magnitudes
signed magnitudes

Hexadecimal number systems

This number system is widely used in computers as an alternative to the binary number system. It has 16 digits including 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. Letters from A to F represent 10 to 15.

Numbers in the hexadecimal number system have a base of 16.

The largest digit of the hexadecimal number can be represented by 4 bits. The following table shows the corresponding values for decimal, binary, octal and hexadecimal numbers.

number systems in computer chart
chart for numbers

Conversion of the hexadecimal number to decimal

Hexadecimal numbers are converted to decimal by expressing the digits in the powers of base 16.

The example below shows how to convert the hexadecimal number 3A5 to decimal.

3×162 + 10×161 + 5×160

= 3×256 + 10×16 + 5×1 = 677

Conversion of decimal numbers to hexadecimal

To convert a decimal number to hexadecimal, the number is repeatedly divided by 16 taking note of the quotient and the remainder as shown below.

decimal to hexadecimal

Conversion from hexadecimal to binary

The easiest way to convert a binary number into hexadecimal is to group the digits into 4 bits and determine the hexadecimal equivalents of those numbers. This is done starting from the right.

To convert the binary number 0001101000001101 to hexadecimal, the digits are grouped into fours as shown below.

(0001)(1010)(0000)(1101)

Each of these groups represents a hexadecimal digit.

0001 represents 1 in hexadecimal

1010 represents A in hexadecimal

0000 represents 0 in hexadecimal

1101 represents D in hexadecimal

Hence, the binary number 0001101000001101 is the same as 1A0D in the hexadecimal number.

Octal number systems

The octal number system has 8 digits with 0 being the lowest digit and 7 being the highest digit. They are represented by 3 bits. Numbers in the octal number system have a base of 8.

Converting an octal number to decimal

Converting an octal number to a decimal is done by multiplying the digits with the powers of base 8.

To convert the number 4568 to base 10.

4×82 + 5×81 + 6×80

=4×16 + 5×8 + 6×1 = 64 + 40 + 6 = 11010

Converting from decimal to octal number

This is done through successive division of the number until the quotient reached 0.

The example below shows how to convert the decimal numbers 44 and 289 to octal numbers.

decimal to octal conversion

Converting binary numbers to octal numbers

This is done by grouping the digits into three bits and determining the corresponding octal values.

To convert the number 0111000012 to octal, the digits are grouped into 3s. The corresponding equivalents of these groups in the octal number system represent the individual digits.

(011)(100)(001)

Hence,  011100001 in base 2 is equal to that in base 8.

Converting from octal to hexadecimal number

The simplest way to convert an octal number to hexadecimal is by converting it to binary and then to hexadecimal.

To convert to binary, the digits are represented in 3 bits. The resulting number is then grouped into 4s, where each of these groups represents a hexadecimal digit.

Let’s convert the octal number 6538 to hexadecimal.

6 in octal =  110 in binary

5 in octal =  101 in binary

3 in octal =  011 in binary

This means that 653 in base 8 is the same as 110101011 in binary.

Grouping into 4s:

(1)(1010)(1011)

This is the same thing as (0001)(1010)(1011).

Then, find the equivalent digits in hexadecimal.

(0001) = 1 in hexadecimal

(1010) = A in hexadecimal

(1011) = B in hexadecimal

This means that 653 in octal is the same as 1AB in hexadecimal.

Conclusion

There are numerous ways of representing numbers including decimal, octal, hexadecimal and binary number systems. The binary number system is a number system comprising 1s and 0s.

Before any piece of information can be reproduced in digital formats, it has to be broken down into numbers in binary form. Learning how to use each of these systems is required when performing bits-related operations such as bitwise operations.

Leave a Reply

Your email address will not be published. Required fields are marked *