Sunday, December 4, 2022
HomeData Structures and AlgorithmSimple explanation of Bit Manipulation for DSA in JavaScript | Number System

Simple explanation of Bit Manipulation for DSA in JavaScript | Number System

So we have arrived now at our next stop in the series of “Understanding Data Structure and Algorithms with JavaScript 🚀”, which is the Bit Manipulation. Bit manipulation is one of the most critical topics in competitive programming competitions point of view because using BIT methods can decrease your submission time, directly increasing your rank among other developers.

In this article before moving forward with actual bit masking and bit manipulation, we will learn different number systems in maths, types, conversion, and addition and subtraction of binary numbers.

What is Number System in Maths?

A Number System is a mathematical system of writing to represent numbers. It is a process of arranging and expressing numbers of a given by using digits or other symbols in a systematic manner.

The value of any digits in the number is determined by the:-

  • The digit itself
  • Digit position in the number
  • The base of the number system

The number system facilitates us to perform arithmetic operations like additions, subtraction, multiplications, and division.

Also read, What is Big O Notation – Space and Time Complexity

Types of Number Systems

There are four most common number system types:-

number systems

Decimal number systems (Base – 10)

The decimal number system or the number system you use has a base of 10 and uses numbers between 0 and 9. In decimal numbers, the digit on most left successive position represents units, then tens, hundreds, thousands, and so on. Example, 1, 200, 543, 1457.

Example

A decimal number 1457 can be represented as,

(1457)10 = (1 X 103) + (4 X 102) + (5 X 101) + (7 X 100)

Also read, Understanding Array, String, and Object Data Structure with JavaScript

Binary number systems (Base – 2)

A binary number system has a base of 2 and only uses two digits 0 and 1. Example, 10, 110, 1011. The binary system is mostly used by computers for its calculations and operations.

Example

A binary number can be represented as,

(5)10 = (101)2

Octal number systems (Base – 8)

An Octal number system has a base of 8 and it uses numbers between 0 to 7. The octal number system is generally used in computer applications.

Example

An octal number can be represented as,

(458)10 = (712)8

Also read, Simple Explanation on Searching Algorithms with JavaScript

Hexadecimal number systems (Base – 16)

A Hexadecimal number system has a base of 16, representing two sets, first just like the decimal system, from 0 to 9 and other using the alphabet from A to F.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 A B C D E F

Example

A Hexadecimal number can be represented as,

(458)10 = (1CA)16

Also read, Introduction to Graph Data Structure

Number system conversions

We can convert a number with any base to another number system using different rules. Let us see how these are done:-

Conversion of Decimal to Binary Number System

In order to convert a Decimal number to a Binary number system, we can follow the following rules,

  • Step 1 – Divide the given number with binary base 2 and note down the quotient and remainder we get at each step.
  • Step 2 – Continue repeating the process till we reach 0 or less than the base, which in this case is 2.
  • Step 3 – Finally, reverse the last quotient with all the readings of remainders at each step, which will be your Binary number.

Conversion of Decimal to Binary Number System

Also read, Simple Explanation on Sorting Algorithms with JavaScript | Part 1

Conversion of Binary to Decimal Number System

In order to convert a Binary number to a Decimal number system, we can follow the following rules,

  • Step 1 – Arrange the numbers drawn below in the figure.
  • Step 2 – From the extreme right, start multiplying each digit with the base of Binary 2, and as slide forward from right to left, keep on increasing the value of the power of the base of Binary 2.
  • Step 3 – Finally, add each number and you will get your number converted into a Decimal Number.

Conversion of Binary to Decimal Number System

Note – To convert a number from any number system like binary, octal, or Hexa, to another system, for ease, first convert the number into a decimal number system then convert it into any other desired system.

Also read, Simple Explanation of Stack and Queue with JavaScript

Conversion of Decimal to Octal Number System

In order to convert a Decimal number to an Octal number system, we can follow the following rules,

  • Step 1 – Divide the given number with Octal base 8 and note down the quotient and remainder we get at each step.
  • Step 2 – Continue repeating the process till we reach 0 or less than the base, which in this case is 8.
  • Step 3 – Finally, reverse the last quotient with all the readings of remainders at each step, which will be your Octal number.

Conversion of Decimal to Octal Number System

Conversion of Octal to Decimal Number System

In order to convert an Octal number to a Decimal number system, we can follow the following rules,

  • Step 1 – Arrange the numbers drawn below in the figure.
  • Step 2 – From the extreme right, start multiplying each digit with the base of Octal 8, and as slide forward from right to left, keep on increasing the value of the power of the base of Octal 8.
  • Step 3 – Finally, add each number and you will get your number converted into a Decimal Number.

Conversion of Octal to Decimal Number System

Also read, Introduction to Tree Data Structure

Conversion of Decimal to Hexadecimal Number System

In order to convert a Decimal number to a Hexadecimal number system, we can follow the following rules,

  • Step 1 – Divide the given number with Hexa base 16 and note down the quotient and remainder we get at each step.
  • Step 2 – Continue repeating the process till we reach 0 or less than the base, which in this case is 16.
  • Step 3 – Finally, reverse the last quotient with all the readings of remainders at each step, which will be your Hexadecimal number.

Conversion of Decimal to Hexadecimal Number System

Conversion of Hexadecimal to Decimal Number System

In order to convert a Hexadecimal number to a Decimal number system, we can follow the following rules,

  • Step 1 – Arrange the numbers drawn below in the figure.
  • Step 2 – From the extreme right, start multiplying each digit with the base of Hexa 16, and as slide forward from right to left, keep on increasing the value of the power of the base of Hexa 16.
  • Step 3 – Finally, add each number and you will get your number converted into a Decimal Number.

Conversion of Hexadecimal to Decimal Number System

Also read, Understanding Binary Search Trees with Js

Binary Numbers Arithmetic Operations

A Binary number system is one of the most important number systems on which all computers and electronic systems are based. All the operations from addition, subtraction, or even sharing the data over the internet/cloud are done using logic gates based on the Binary system.

Note – A single digit in the Binary number system is called a “Bit”.

Before moving forward to understand logic gates, we first need to understand how does, basic addition, subtraction, multiplication, and division work in Binary number systems.

Addition of Binary Numbers

Adding two binary numbers is a straightforward process, we have to follow below rules in order to add two Binary Numbers:-

Binary Number 1 Binary Number 2 Addition Value
0 0 0
0 1 1
1 0 1
1 1 0 (1 gets carryover)

Example

Addition of Binary Numbers

 

Also read, What is the InfiniteNature-Zero AI Model?

Subtraction of Binary Numbers

Adding two binary numbers is a little tough process than addition, we have to follow the below rules in order to subtract two Binary Numbers:-

Binary Number 1 Binary Number 2 Subtraction Value
0 0 0
0 1 1 (1 gets borrowed)
1 0 1
1 1 0

Example

Subtraction of Binary Numbers

Also read, Simple Explanation of Tree traversal Operations with JavaScript

Multiplication of Binary Numbers

The multiplication process in the Binary number system is very similar to the numerals multiplication process.

Binary Number 1 Binary Number 2 Multiplication Value
0 0 0
0 1 0
1 0 0
1 1 1

Example

Multiplication of Binary Numbers

Also read, Simple Explanation on BFS and DFS Graph Traversal Methods

Division of Binary Numbers

The division process in the Binary number system is very similar to the numerals division process.

Binary Number 1 Binary Number 2 Division Value
0 0 0
0 1 1 (1 gets borrowed)
1 0 1
1 1 0

Example

Division of Binary Numbers

Also read, Introduction to Recursion – Learn In The Best Way with JavaScript | Part 1

Final Words

Bit Manipulation is one important topic in the computer science branch. I hope you like my article, also do share this article with your friends, family, and colleagues. Do bookmark or subscribe to never miss any article. Also check out our amazing articles on various topics like Java, Data structure and Algorithms, Javascript or React.

Aaquib Ahmedhttps://aaquib.netlify.app
Hi, I'm Aaquib Ahmed, a passionate self-taught frontEnd web developer based in Bangalore, India. I tend to make use of modern web technologies to build websites that look great, feels fantastic, and functions correctly. I am especially focusing on Reactjs.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments