Skip to main content

Number Systems (Binary and Decimal Number Systems).

We are surrounded with Numbers. 

For example a human being has 2 ears, 2 legs(generally) and one nose. These all are nothing but numbers.

Now, one might ask why do we need to study numbers for c++ programming.

Let me answer this one first.

In the previous blog I discussed that every computer works on machine language i.e. 0s and 1s, everyone know 0 and 1 are numbers.

Ok, done, But what the word "Systems" doing here? What is meant by number systems.

Let's see.

We all have studied about the numbers from 0 to 9 i.e. 0,1,2,3,4,5,6,7,9 and we very well know that every other number can be derived from these number except infinity(no one knows what's that). for example one thousand twenty two is 1022. 

This number system that we have studied is called Decimal Number System. Why, Decimal?

Because this number system contains 10 different symbols. 

(In Greek Deca or Deka means 10 and this words is derived from Deca/Deka).

Ok, Now we know what is decimal number system and why we call it decimal number system.

Now, I would like to show you some more number system that are more useful in computer science rather than decimal number systems.

  1. Binary number system.
  2. Octal number system.
  3. Hexadecimal number system.
(Extra thing to remember - The number system I showed above including decimal are weighted number systems (I will come to that), there are non-weighted number systems too like Roman numbers but that are not useful for programming purposes)

Binary Number system

The word Binary means 2. Now, think of the point where I have said that machine language contains only 0s and 1s, 0 for off circuit and 1 for on circuit, aren't these only two distinct characters i.e. 0 and 1 only? Yes, that's right, Binary number system is nothing but the number system that computer uses.
Now, If you have used computer(If not you should have used it, it's a cool thing), You must have written something in english or any other language, Now, when I was a learner I were confused with the doubt, if computer can only understand 0s and 1s, then how it could process all the English alphabets?
Hope you don't have this doubt, If you have it's ok, This doubt will be cleared in this post.

Now let's see the step by step process of conversion of characters to machine language. 
As I have already said in this very particular blog that Binary numbers are weighted, it's time to clear what weighted means.

Remember your childhood day When you try to convert the mathematical number into its english form like 89 to eighty nine. you count from right to left like - ones, tens, hundreds, thousands.... and so on. The thing you were counting is nothing but the weights of the number.
Similarly, binary number system have weights associated according to its position.

Now, take the above number itself again. How will you write the above number in binary form.
Binary form of 89(decimal number) - 1011001. Don't worry about how I get to this.

First, let me tell how 89 in decimal is equal to 1011001 jn binary. 


Here, the weights of each of the binary digit is written above it, So, from this we can conclude the following things.
  • Converting Binary Number to its decimal formWe know decimal(ten distinct number) number system has weight position(from left to right) ones, tens, hundreds... which is like 10^0, 10^1, 10^2.. it's like a GP (geometric progression series) similarly Binary (two distinct characters) Number system has weights like 2^0, 2^1, 2^2.... and so on. So, from here we can say that if we are given a number in any number system, we can convert it to decimal just by using above approach, for sake of simplicity lets try to form a formula. For ith digit of a number represented in any number system say 'n'(n distinct numbers). we can say that ith digit has weight n^(i-1)(if we take first digit as count 1) and n^(i)(if we take first digit as count 0).
  • Converting Decimal Number to its binary form - This is very easy task 👀 to convert a decimal number to its binary equivalent. We use double dabble method for doing so, The Algorithm(steps) are as follows -> 
    1. Continuously divide the number integrally by 2(that is don't divide like 89/2 = 44.5) and write down the remainder we got until the number becomes 0.
    2. After, the number becomes zero reverse the whole list of number that you wrote while dividing the number by 2 and tada! you will get the binary equivalent of that number. 
Conversion from decimal to binary.


Now, As I said previously that I will clear your doubt on how English alphabets are understood by the the computer? It's perfect time for that.
Till now we have seen how computer could convert the decimal numbers to the binary system to understand those numbers.
Understanding alphabets is a similar thing. Every English alphabet or rather say everything present on your computer's keyboard has some integer value associated with it. when we press any key, It's not the character which is processed but the integer value that is associated with it. Those values are called ASCII (American Standard Code for Information Interchange) values. Some common range of character has values like this. (Remember these now, if you want).
  • a-z is equivalent to 97 - 122.
  • A - Z is equivalent to 65 - 90.
  • 0 - 9 is equivalent to 48 - 57.
Extra things to remember - 
  • if you know the ascii value of any small letter of english alphabets you can find the ascii value for it's capital english equivalent by subtracting 32 and vice versa.
  • 32 is the Ascii value of blankspace " ".
When you pass a character like if you write 'A' first we will need it's ASCII value and then we have to convert it to its binary representation 1000001, after which computer can operate on this data.

Here we should argue that if computer can only understand binary and we are familiar with decimal, it make sense to have these two number systems, but why octal and hexadecimal number systems exist.
The answer is when we write a number in binary number system even for representing a number like 127 we need atleast 8 bits(in decimal it is often called number of digits), which is messy task, and think of the case when the number is very large, then we will require unbelievable amount of bits and computer can't handle those. So, for the ease of this task we define octal and hexadecimal.
But why only octal and hexadecimal?
Let' see.
Octal means 8 and hexadecimal means 16, can you see pattern? These two numbers are powers of 2 
Octal is 2 to the power 3 and hexadecimal is 2 to the power 4, which give us a hint that to represent a 3 digit/bit binary number we require only 1 bit of octal digit and for 4 bit/digit of binary number we can use 1 bit/digit hexadecimal number. (isn't Cool AF).

Detailed version  of Octal and Hexadecimal number systems are discussed here.

Stay tuned for the upcoming content. For any query leave a comment below.

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).

Comments

  1. Great work. You explain every thing in good way. keep it up.

    ReplyDelete

Post a Comment

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

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

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