Turning off a particular bit in a number in Java

In this tutorial, we will learn to turn off a bit of a number. We can use bitwise operators to carry out turning off a bit. However, if you wish to skip these operators you can manually code it as well. As a result, your code will get a little longer but it would surely enhance your logical aptitude.

In this tutorial, I would be implementing the problem without any bitwise operators. Now we will see how to carry out this function.

Java Program for turning off a bit in a number

First of all, we have to import the Java scanner class. It has all the essential packages for printing and taking user input. We can take the user input by creating an object of scanner class.

Since I will be doing the whole process manually, first I need to resolve the number into its binary equivalent. Hence, I have used a simple while loop to carry out this function. I have stored the result in a 1-D array.

After this, we will take the user input of the bit to be turned off. Next, we can carry out turning off the bit. Once we have the bit number, all we need to is turn it off. Traverse to that number bit and with the help of assignment operator, assign 0 to that array index. The only trick here is that we do not get confused with the indexing and bit to be turned off. We have to keep in mind that the bits are always read from the right and return in reverse order. So, if you need to turn off the bit 2, it will mean the second bit from the right.

Finally, we will simply change our binary into the corresponding integer. I have done this by using the Math.pow() function in the scanner class. After this, you can just output your result in the way you like it.

import java.util.Scanner;

class turnoff_bit
{
  public static void main(String[] args)
  {
    Scanner ob = new Scanner(System.in);
    System.out.println("Enter number");
    int n = ob.nextInt();
    int a[]=new int[8];
    int x = n;
    int k=7;

    //changing integer to binary

    while(n>0)
    {
      if(n%2==0)
      {
        a[k] = 0;
        k--;
        n=n/2;
      }
      else
      {
        a[k] = 1;
        k--;
        n=n/2;
      }
    }

    System.out.print("Binary of input number: ");
   
    //output initial binary number
    for(int i=0;i<8;i++)
    {
      System.out.print(a[i] + " ");
    }

    System.out.println(" ");

    System.out.println("Enter bit to be turned off ");
    int z = ob.nextInt();

    //turning off the bit
    a[8-z] = 0;

    System.out.print("Binary bit turned off:  ");
    for(int i=0;i<8;i++)
    {
      System.out.print(a[i] + " ");
    }

    double add = 0.0;
    
    //changing back into integer
    for(int i=0;i<8;i++)
    {
      double c = 7-i;
      add = add + a[i]*Math.pow(2.0, c);
    }

    System.out.println(" ");
    System.out.println("Final Result: " + add);
  }
}

 

Input:

We can input the values of a number on which operation is to be carried out and the bit to be turned off.

Output:

Enter number
25
Binary of input number: 0 0 0 1 1 0 0 1
Enter bit to be turned off
5
Binary bit turned off:  0 0 0 0 1 0 0 1
Final Result: 9.0

Leave a Reply

Your email address will not be published. Required fields are marked *