Bit Manipulation || Java + DSA course || Blog 8

What is Binary Language / Binary Number System? Why is it used in computational programming?

Circuit flows through our Laptops and mobile phones. When the current is on the higher state, it represents it as 1 while 0 for lower state. This is Binary Number system.It converts numbers, characters, strings to Binary language consisting of only 0s and 1s.

A bit (short for binary digit) is the smallest unit of data in a computer. A bit has a single binary value, either 0 or 1.

Bit Manipulation is used  to overcome time complexity of algorithms . So it is used for computational programming.

We learned left shift and right shift operators in our last blog.

For quick recap:

You ask user how much bits you want to shift. If user says 1, you shift the binary number left side by 1 space. At the end the space is 0 by default.

Exactly opposite for right shift operator

For a quick Overview:

  • Get Bit-to know if it is 0 or 1 at a certain index.
  • Set Bit- to change index from 0 to 1. if 1 then keep it
  • Clear Bit- to change index from 1 to 0. if 0 then keep it
  • Update Bit- to change from 0 to 1. from 1 to 0

In Bit Manipulation , indexes start from right to left.

Get Bit

Input:

Taking number . examples. N = 1010

Asking which index/position you want to know.i = 1

Solving:

Given:

N(number) = 1010

Operation : AND(we will understand it logically after)

i(position) = 1

We right left shift operator as n<<i.

So, 1010<<1    (shifting number by 1 in left side)

Step 1:1(0001)<<i

 





So the number will be 0010.

Step 2: Doing AND operation on 0010 and N.

 



Here if all are zero then , then bit was 0

If anyone of them is one, then bit is one. One will be only at single place.

If the answer would have one, then it is non zero number.

Here, one is present so it means bit is one.

Logic: 1(0001)is the number. When we shift it with position times, it comes parallel to the space of the position, if the position number contains 1 ,it will print non zero number. Since 1 is already shifted from(0001 to 0010).

Code in java:

public class Main{
public static void main(String args[]){

int n = 10;//1010

int i = 1;//1 is there at index 1 of 1010
int bitmask = 1 << i;

if((bitmask & n) == 0){
System.out.println("zero");

}else{
System.out.println("one");
}
}
}

 

It will print one for n=10.

Set bit

n = 1010

Operation = OR

Position(i) = 2

Step 1:

 1<<i;

0001<<2

=0100

Step 2:

 

So the number is changed at position2 from 0 to 1.

Logic: by 1<<position, 1 came perpendicular to the place where you wanted to change the digit to 1. Even if there is 0, due to OR operator it will become 1. If it was already one, it will remain the same.


public class Main{
public static void main(String args[]){

int n = 10;//1010

int i = 2;//0 is there at index 2 of 1010
int bitmask = 1 << i;

int newnumber = bitmask | n;
System.out.println(newnumber);
}
}

 

Answer will be 14(1110)

Clear Bit

It is based on XOR operator. if you haven't checked out it yet , visit : 

It returns similar pairs of 1 to 0 and and different to 1

0 0 = 0

1 1 = 0

1 0 = 1

0 1 = 1 


public class Main{
public static void main(String args[]){

int n = 10;//1010

int i = 1;//1 is there at index 1 of 1010
int bitmask = 1 << i;

int newnumber = bitmask ^ n;
System.out.println(newnumber);
}
}

 

It will return 8(1000). It has changed 1010 index 1 to 0.

Update Bit

n = 1010

If 0 is there, use XOR operation(used in clear bit)

If 1 is there, use Or operation(used in set bit)

 

public class Main{
public static void main(String args[]){

int n = 10;//1010
int i = 2;//1 is there at index 1 of 1010
int bitmask = 1 << i;
if((bitmask & n) == 0){
int newnumber = bitmask | n;
System.out.println(newnumber);
} else{
int newnum = bitmask^n;
System.out.println(newnum);
}
}
}


1010 at position 1 = 1000

1010 at position 2 = 1110

___________________________________________________________________________________

Cheout our Blogs:https://javaanddsa.blogspot.com/

Comments

Post a Comment

Popular posts from this blog

LinkedList Problems for Beginners DSA || Java and DSA course || Blog 17

Introduction to Java || Java + DSA course || Blog 1

LinkedList in DSA || Java and DSA course || Blog 16