MATH for DSA, Bitwise operator, Bit manipulation, Number system and conversion.

MATH for DSA, Bitwise operator, Bit manipulation, Number system and conversion.

Hii, I am Ghulam Rabbani after 2 days I came back with my second article, I hope it will help you and you will enjoy it.

A couple of weeks back I got a 30 days DSA plan from coding ninja’s which seems good to me, so I doubled the days and made a 70 days DSA plan. Since as per the plan the first thing I had to learn is Math for DSA i.e bit manipulation, number system and conversion etc. So I started learning it recently, let's see how much I can get out of it.

I decided to share it with you guys because learning in public is something that can help you to build a network and also land you a high paying job , this is the thing that I learnt from a youtuber Kunal Kushwaha. Before proceeding further I would like to thank all the people I mentioned here and others who guided me through my career.

I think its enough of superfluous words 🙂🙂🙂, Now let’s start with the tutorial,

Why do we need math and number system in DSA,

When we write int a = 5, char b = c, and String name = “myname” etc, whatever we do in our computer, the servers that are running, these all things are bunch of 0’s and 1’s because we have been learning since childhood that computer understands only 0’s and 1’s and it is called binary number system.

Since our childhood we have been learning decimal number system but in mathematics there also other number systems exists,

Types of number system

1. Decimal number system: In this number system it holds digit from 0 to 9 which is 10 digits and is called base 10.

Note: The base b represents the number of digits in a particular number system.

Example : 1, 2, 4, 11, 19, 54 etc.

2. Binary number system : It is what the computer understands and it holds only 0’s and 1’s and is called base 2.

Example : The above number can be represented in binary as

1 = 1 as binary number system has 1, 2 = 10, 4 = 100, 11 = 1011, 19 = 10011 and 54 = 110110

3. Octal number system: As the name suggests It contains the digit from 0 to 7 which is 8 digit and is called base 8.

Example : The above number can be represented in octal as,

1 = 1, 2 = 2 , 4 = 4, 11 = 11, 19 = 23 (it can't contains 9 in it) and 54 = 66

4. Hexadecimal number system: In this system it contains sixteen digits that is from 0 to 9 and for 10-15 it is represented as A,B,C,D, E and F respectively.

Example : The above number can be represented in hexadecimal as

1 = 1, 2 = 2, 4 = 4, 11 = B, 19 = 13 and 54 =36

Conversion of number system

There can be many conversions like decimal to binary, decimal to octal, decimal to hexadecimal and its vice versa, or there can also be as octal to binary, octal to hexadecimal etc.

But in this tutorial we will learn two types of conversion and that covers all the conversion I mentioned above,

  1. Decimal to base b conversion

  2. Base b to decimal conversion

1. Decimal to base b: In this system you have to keep dividing the number by base and take its remainder and write it in the opposite direction.

Example:

WhatsApp Image 2022-04-05 at 14.57.11.jpeg

2. Base b to decimal: It is opposite of above, here we will multiply. Multiply and add the power of base with digits,

Example:

WhatsApp Image 2022-04-05 at 15.04.40.jpeg

Note: If you are given to convert directly binary to octal or octal to binary then first convert it to the decimal and then convert it to the desired base.

As we discussed above, that computer works in a binary system and stores binary digits in bits, so in order to deal with some mathematical calculations we have a Bitwise operator.

Types of bitwise operator

1. AND ( &) operator: & operators means if all the numbers and input should be true then only the entire expression will be true. If one of the inputs is false the entire expression will be false.

Note: In computer 1 represents true and 0 represent false.

Example: alltrue&.png

Note: In binary when you & any number with 1 the digit remains the same

2. OR ( | ) operator: If any one of the numbers and inputs are true then the entire expression will be true,

Example: oroperator.png

3. XOR ( ^ ) : It is exclusive OR, If you have two inputs and only one of them should be true then the entire expression will be true otherwise entire expression will be false.

Example: xor.png

Some special observation of XOR operator are,

  • If you ^ any number with 1 it gives compliment of that number.

Ex: 0 ^ 1 = 1 and 1 ^ 1 = 0

  • If you ^ any number with 0 the digit remains the same.

Ex: 1 ^ 0 = 1 and 0 ^ 0 = 0

  • If you ^ any number with the number itself it will always give 0.

4. Not operator( complement of a number or ~): If you not(!) any number it gives complement of that number.

Note: Compliment means if you have zero then it wil return 1 and vice versa.

5. Left shift operator ( << ) : It shifts all the bits towards the left by the given number, after it shifts left by n it requires n , extra’s number and that number should be 0,

Example: 1010 << 1 = 10100

Note: Here we are shifting by 1 then one 0 is being added here.

Conclusion: If you want to double the number left shift it by 1 and if you left shift a number b times the number will become (a * base to the power b).

6. Right shift operator ( >> ) : It is opposite of left shift operator, when you right shift a number b times the number will return (a / base to the power b) and after shifting the last digit should be discarded from the number.

Example: 0011001 >> 1 = 001100

Note: Like in the decimal number system 000123 = 123, this concept is same in all the number system, the leading zeroes are ignored.

Some questions on Bit manipulation

  1. Find if the number is even or odd ?

How do we approach the problem : As we have discussed above that when we & 1 with any number the digit remains the same,

Take 19 as an example, its binary will be 10011, if we ignore the last digit the rest of the digit is power of 2 hence it will be even, so the answer will be dependent on the last digit. If the last digit is 1 the number will be odd else the number will be even

Here if we & 10011 with 00001 it will give me 1 hence the number is odd.

//TO check whether a number is odd or not
    static boolean isOdd(int n){
        //& any number with 1 it gives 1
        if((n & 1) == 1){
            return true;
        }
        return false;
    }
  1. Find the unique number in the given array where every number appears twice.

Approach: As we know if we ^ a number itself and 0 it gives the same number, so we take 0 as unique and ^ it with every number in the array, at last it will give you unique number

//TO check unique number in an array
    private static int unique(int[] arr){

        int unik = 0;
        for(int num:arr){
            unik ^= num;
        }
        return unik;
    }

If you want to make good grasp over Bit manipulation, I would recommend to read conversion and bitwise operator again.

That's for this article. Take a look at My other article

Follow me on Twitter, LinkedIn

Did you find this article valuable?

Support ghulamrabbani by becoming a sponsor. Any amount is appreciated!