Skip to main content

Number Systems(Octal and Hexadecimal).

Let's start from the point where we've left in the previous slide.
I previously said that Octal and Hexadecimal are useful to represent very large number, when binary number system fails to represent those in an efficient way.
First we will discuss about Octal Number System.

Octal Number System

I have already given you the advantage of octal number system. Now let's look, what the octal number system actually is?.
Octal means 8 (just like binary means 2 and decimal means 10). It means that in octal number system we have 8 distinct value i.e. 0,1,2,3,4,5,6,7. 
Ok, now we know advantage of octal system and what distinct values it contains. Now, let's see how we utilize that advantage which it has over the binary number system.

Octal and Binary
Comparison.

Converting Binary to Octal number:

You can clearly see that where we require 3 bits in binary form we can represent that number easily in 1 bit of octal form. Now you might be thinking that if the number of bits is greater than 3 like in case of 65 we were having 1000001 then what should we do.
The conversion from binary to octal is very easy you have to make pairs from binary digits where each pair contains 3 binary digits, from right to left, and if there are less number of digits on the left side then add successive zeroes to the left.
like after pairing binary representation of 65 we get following pairs (001)(000)(001). Now simply look at the table above (remembering it will be better too) and write down it's octal equivalent i.e.
(001)(000)(001) in binary is equivalent to 101 in octal.
Isn't this damn easy.

Converting Octal to Binary number:

Ok, now lets see how we can convert an octal number to its binary equivalent.
It's more simpler than converting binary to octal, we just have to write the three digit equivalent of binary in place of every digit of octal number take 101 we calculated above just replace every digit by its 3 digit binary equivalent and you will get your answer.

Converting Decimal to Octal number: 

We can convert Decimal to octal by first converting decimal to binary than binary to octal. But, If you don't want to do this in two steps You can use double dabble method but the only change is we repeatedly divide the decimal number with 8 instead of 2, others steps are same as decimal to binary conversion.

Converting Octal to Decimal number:

This task is also fairly easy perform the same procedure like we did in converting binary to decimal system but replace the weights of the ith digits from 2^i to 8^i or 2^(i-1) to 8^(i-1). It's as simple as that.

Now we are done with octal number system, But, If anyone wants to learn Octal arithmetic do let me know in the comment section. I will make a separate post on that.


Hexadecimal Number System

I have talked little bit about hexadecimal number systems in the previous post and in this post also, but in this section let's clear the concepts with sandpaper😳.

Hexadecimal means 16, which simply means that there are 16 distinct values in hexadecimal, but one might be thinking that if we have 0 - 15 numbers than how can we think of this number 115, Is this number 1 and 15 or 11 and 5 or 1 and 1 and 5 whatever the confusion maybe there. But, that's where our intelligent researchers play their roles. and they didn't assigned the number from 0-15 rather they used digits from 0-9 and English alphabets A-F (Researchers are COOl AF). 

Now, some of us would ask (assuming so 😬) that why there is not any number system used with radix(base) 32, then it would be easy to represent even more large numbers with just small number of bits. You should read these answers.

Let me write the whole list for your reference.

0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.

(sorry, but if you haven't noticed there must be 0 and 0000 too in this table.)

Hexadecimal and Binary
Comparison.

Converting Binary to Octal number:

You can clearly see that where we require 4 bits in binary form we can represent that number easily in 1 bit of hexadecimal form. Now you might be thinking that if the number of bits is greater than 4 like in case of 65 we were having 1000001 then what should we do.
The conversion from binary to octal is very easy you have to make pairs from binary digits where each pair contains 4 bits, from right to left, and if there are less number of digits on the left side then add successive zeroes to the left.
like after pairing binary representation of 65 we get following pairs (0100)(0001). Now simply look at the table above (remembering it will be better too) and write down it's octal equivalent i.e.
(0100)(0001) in binary is equivalent to 41.

Converting Octal to Binary number:

Ok, now lets see how we can convert a hexadecimal number to its binary equivalent.
It's more simpler than converting binary to octal, we just have to write the three digit equivalent of binary in place of every digit of octal number take 41 we calculated above just replace every digit by its 4 digit binary equivalent and you will get your answer.

Converting Decimal to Octal number: 

We can convert Decimal to hexadecimal by first converting decimal to binary than binary to hexadecimal. But, If you don't want to do this in two steps You can use double dabble method but the only change is we repeatedly divide the decimal number with 16 instead of 2, others steps are same as decimal to binary conversion. ( Important note - avoid doing mistakes of writing 10 for ten etc due the reasons I gave you before.)

Converting Octal to Decimal number:

This task is also fairly easy perform the same procedure like we did in converting binary to decimal system but replace the weights of the ith digits from 2^i to 16^i or 2^(i-1) to 16^(i-1). It's as simple as that.

Now we are done with hexadecimal number system, But, If anyone wants to learn hexadecimal arithmetic do let me know in the comment section. I will make a separate post on that.

Click the Subscribe button at the top, to follow my every post regarding c++.

References -
  • Wikipedia.
  • GeeksForGeeks.
  • Lippman, Stanley B. c++ primer 3rd edition(april 2, 1998).
  • quora.

Comments

Popular posts from this blog

Introduction to Computer Science.

 Before directly jumping deeply in c++, let's first start by creating the roots of computer science. In this post we will answer the following question. What is Computer and How it works? What is Program and Operating system? Low Level vs High Level Language? Compiler v/s Interpreter? By Knowing All these basic concepts You will be able to understand the upcoming more complex concepts easily. Let's start answering the above questions. What is Computer and How it works? What is computer? If I answer briefly,  what is computer?  then it is just a calculator used for doing simple calculations. If I have to answer where is computer used?, then I could probably say everywhere. We are surrounded by computer, for example mobile, smartwatches, and personal computer (obviously).  The below image is the first mechanical computer that we used for calculation.                                        Abacus How computer works? The computer's internal functionality is a whole separate bra

C-style strings vs String Class.

Here, we are going to learn about, what are C-Style Strings and what is String class . So, let's start by giving you the introduction. C-Style String is nothing but, an array of characters, and from the term array we can surely assume that these are static in size i.e. the size of these C-Style strings cannot be increased or decreased. String Class is a Built-in class that provide us much more functionality than C-Style String. C-STYLE STRINGS Before learning the String class, which is full-fledged feature of C++, we should rather start by taking a look at some important things about C-Style String. C-Style Strings are actually array of characters that are terminated by a null character "\0". If you are confused with, why null character? The reason is that, It helps us to define, upto which index we have some useful data present in our character array, and after null character there may or may not be some garbage values of characters. Let's first start by showing a

Algorithms, Pseduocodes and FlowCharts.

Some Basic Knowledge. C++ is a general purpose programming language, created by Bjarne Stroustrup and was firstly released in  1985. It is an extension to C language, Reason for that is if you are familiar with C language than you must be familiar with the fact that there are no classes, templates etc. present in C language. Due to which when classes are added to C language it is named as "C with Classes", but C++ is considered as default name now.  Extra thing to remember - In "C++" the symbol "++" after C is post increment symbol and therefore, "C++" means that In C additional features are added. Now, let's define, what are algorithms? Algorithms are nothing but set of rules that must be followed to accomplish some task. (Now, What that mean?😕) let's take an example of swapping two numbers, i.e. if a = 10 and b = 20 after swapping a = 20 and b = 10. How will you write the procedure for this?  (There are many such algorithms). Accordin