Skip to main content

Turn off a particular bit in a number

The given problem is about turning off a particular bit in a given number. In binary representation, turning off a bit means setting it to 0. The problem statement specifies that we need to provide a solution to turn off a specified bit in a given number, given its position in the binary representation.

Explanation with Suitable Example

Let's understand the problem with a suitable example. Consider the number 12, which is represented in binary as 1100. We want to turn off the bit at position 3 (from the right, starting with position 1). After turning off the bit at position 3, the new binary representation will be 1000, which is equal to 8 in decimal.

Pseudocode

Before delving into the algorithm, let's write the pseudocode to understand the steps required to turn off a specific bit.

turn_off_bit(number, position)
    if position is not in the range 1 to 32
        return "Bit position is outside the range"
    
    mask = 1 << (position - 1)
    result = number & (~mask)
    return result

Algorithm Explanation

  1. The function turn_off_bit takes two parameters, number (the given number) and position (the bit position to be turned off).
  2. We check whether the position is within a valid range (1 to 32). This is because an integer in C typically has 32 bits, and we want to avoid invalid positions.
  3. We create a bitmask by shifting 1 to the left by (position - 1) places. This bitmask will have all zeros except for a 1 at the position we want to turn off.
  4. We use the bitwise NOT (~) operator to flip the bits of the bitmask. This will create a mask with all 1s except for a 0 at the position we want to turn off.
  5. By performing a bitwise AND operation between the number and the complement of the mask, we effectively turn off the bit at the specified position.
  6. The resulting value is returned as the output.

Now, let's go through the examples given in the code and check if the output matches our explanation.

  1. For num = 12 and position = 3:
turn_off_bit(12, 3)
    mask = 1 << (3 - 1) = 0100
    ~mask = 1011
    result = 1100 & 1011 = 1000 (in decimal: 8)
    
  1. For num = 59 and position = 6:
turn_off_bit(59, 6)
    mask = 1 << (6 - 1) = 00100000
    ~mask = 11011111
    result = 111011 & 11011111 = 011011 (in decimal: 27)
    
  1. For num = 45 and position = 2:
turn_off_bit(45, 2)
    mask = 1 << (2 - 1) = 00000010
    ~mask = 11111101
    result = 101101 & 11111101 = 101101 (in decimal: 45)
    

Code Solution

Here given code implementation process.

// C program 
// Turn off a particular bit in a number

#include <stdio.h>

// Turn off given bit of a number
void turn_off_bit(int num, int x)
{
    // Display given number
    printf("\n Given number : %d",num);
    printf("\n Bit position : %d",x);

    if(x > 0 && x < 32)
    {
        // Turn off given bit
        int result = num - (num & (1<<(x-1)));
        printf("\n Output :  %d",result);
    }
    else
    {
        printf("\n Bit is outside the range \n");
    }
}

int main(int argc, char const *argv[])
{
    
    int num = 12;
    // (12) = 1100
    // x    = 3
    // After turn off bit position 3  = (1000) => (8)
    turn_off_bit(num,3);

    
    num = 59 ;
    // (59) = (111011)
    // x = 6
    // After turn off bit position 6  = (011011) => (27)
    turn_off_bit(num,6);


    num = 45 ;
    // (45) = (101101)
    // x = 2
    // After turn off bit position 2  = (101101) => (45)
    // Note that bit already off
    turn_off_bit(num,2); 
   return 0;
}

Output

 Given number : 12
 Bit position : 3
 Output :  8
 Given number : 59
 Bit position : 6
 Output :  27
 Given number : 45
 Bit position : 2
 Output :  45
/*
  Java program
  Turn off a particular bit in a number
*/
public class BitManipulation
{
	// Turn off given bit of a number
	public void turnOffBit(int num, int x)
	{
		// Display given number
		System.out.print("\n Given number : " + num);
		System.out.print("\n Bit position : " + x);
		if (x > 0 && x < 32)
		{
			// Turn off given bit
			int result = num - (num & (1 << (x - 1)));
			System.out.print("\n Output : " + result);
		}
		else
		{
			System.out.print("\n Bit is outside the range \n");
		}
	}
	public static void main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		int num = 12;
		// (12) = 1100
		// x    = 3
		// After turn off bit position 3  = (1000) => (8)
		task.turnOffBit(num, 3);
		num = 59;
		// (59) = (111011)
		// x = 6
		// After turn off bit position 6  = (011011) => (27)
		task.turnOffBit(num, 6);
		num = 45;
		// (45) = (101101)
		// x = 2
		// After turn off bit position 2  = (101101) => (45)
		// Note that bit already off
		task.turnOffBit(num, 2);
	}
}

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45
// Include header file
#include <iostream>
using namespace std;

/*
  C++ program
  Turn off a particular bit in a number
*/

class BitManipulation
{
	public:
		// Turn off given bit of a number
		void turnOffBit(int num, int x)
		{
			// Display given number
			cout << "\n Given number : " << num;
			cout << "\n Bit position : " << x;
			if (x > 0 && x < 32)
			{
				// Turn off given bit
				int result = num - (num &(1 << (x - 1)));
				cout << "\n Output : " << result;
			}
			else
			{
				cout << "\n Bit is outside the range \n";
			}
		}
};
int main()
{
	BitManipulation task = BitManipulation();
	int num = 12;
	// (12) = 1100
	// x    = 3
	// After turn off bit position 3  = (1000) => (8)
	task.turnOffBit(num, 3);
	num = 59;
	// (59) = (111011)
	// x = 6
	// After turn off bit position 6  = (011011) => (27)
	task.turnOffBit(num, 6);
	num = 45;
	// (45) = (101101)
	// x = 2
	// After turn off bit position 2  = (101101) => (45)
	// Note that bit already off
	task.turnOffBit(num, 2);
	return 0;
}

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45
// Include namespace system
using System;
/*
  C# program
  Turn off a particular bit in a number
*/
public class BitManipulation
{
	// Turn off given bit of a number
	public void turnOffBit(int num, int x)
	{
		// Display given number
		Console.Write("\n Given number : " + num);
		Console.Write("\n Bit position : " + x);
		if (x > 0 && x < 32)
		{
			// Turn off given bit
			int result = num - (num & (1 << (x - 1)));
			Console.Write("\n Output : " + result);
		}
		else
		{
			Console.Write("\n Bit is outside the range \n");
		}
	}
	public static void Main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		int num = 12;
		// (12) = 1100
		// x    = 3
		// After turn off bit position 3  = (1000) => (8)
		task.turnOffBit(num, 3);
		num = 59;
		// (59) = (111011)
		// x = 6
		// After turn off bit position 6  = (011011) => (27)
		task.turnOffBit(num, 6);
		num = 45;
		// (45) = (101101)
		// x = 2
		// After turn off bit position 2  = (101101) => (45)
		// Note that bit already off
		task.turnOffBit(num, 2);
	}
}

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45
<?php
/*
  Php program
  Turn off a particular bit in a number
*/
class BitManipulation
{
	// Turn off given bit of a number
	public	function turnOffBit($num, $x)
	{
		// Display given number
		echo "\n Given number : ". $num;
		echo "\n Bit position : ". $x;
		if ($x > 0 && $x < 32)
		{
			// Turn off given bit
			$result = $num - ($num & (1 << ($x - 1)));
			echo "\n Output : ". $result;
		}
		else
		{
			echo "\n Bit is outside the range \n";
		}
	}
}

function main()
{
	$task = new BitManipulation();
	$num = 12;
	// (12) = 1100
	// x    = 3
	// After turn off bit position 3  = (1000) => (8)
	$task->turnOffBit($num, 3);
	$num = 59;
	// (59) = (111011)
	// x = 6
	// After turn off bit position 6  = (011011) => (27)
	$task->turnOffBit($num, 6);
	$num = 45;
	// (45) = (101101)
	// x = 2
	// After turn off bit position 2  = (101101) => (45)
	// Note that bit already off
	$task->turnOffBit($num, 2);
}
main();

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45
/*
  Node Js program
  Turn off a particular bit in a number
*/
class BitManipulation
{
	// Turn off given bit of a number
	turnOffBit(num, x)
	{
		// Display given number
		process.stdout.write("\n Given number : " + num);
		process.stdout.write("\n Bit position : " + x);
		if (x > 0 && x < 32)
		{
			// Turn off given bit
			var result = num - (num & (1 << (x - 1)));
			process.stdout.write("\n Output : " + result);
		}
		else
		{
			process.stdout.write("\n Bit is outside the range \n");
		}
	}
}

function main()
{
	var task = new BitManipulation();
	var num = 12;
	// (12) = 1100
	// x    = 3
	// After turn off bit position 3  = (1000) => (8)
	task.turnOffBit(num, 3);
	num = 59;
	// (59) = (111011)
	// x = 6
	// After turn off bit position 6  = (011011) => (27)
	task.turnOffBit(num, 6);
	num = 45;
	// (45) = (101101)
	// x = 2
	// After turn off bit position 2  = (101101) => (45)
	// Note that bit already off
	task.turnOffBit(num, 2);
}
main();

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45
#   Python 3 program
#   Turn off a particular bit in a number

class BitManipulation :
	#  Turn off given bit of a number
	def turnOffBit(self, num, x) :
		#  Display given number
		print("\n Given number : ", num, end = "")
		print("\n Bit position : ", x, end = "")
		if (x > 0 and x < 32) :
			#  Turn off given bit
			result = num - (num & (1 << (x - 1)))
			print("\n Output : ", result, end = "")
		else :
			print("\n Bit is outside the range ")
		
	

def main() :
	task = BitManipulation()
	num = 12
	#  (12) = 1100
	#  x    = 3
	#  After turn off bit position 3  = (1000) => (8)
	task.turnOffBit(num, 3)
	num = 59
	#  (59) = (111011)
	#  x = 6
	#  After turn off bit position 6  = (011011) => (27)
	task.turnOffBit(num, 6)
	num = 45
	#  (45) = (101101)
	#  x = 2
	#  After turn off bit position 2  = (101101) => (45)
	#  Note that bit already off
	task.turnOffBit(num, 2)

if __name__ == "__main__": main()

Output

 Given number :  12
 Bit position :  3
 Output :  8
 Given number :  59
 Bit position :  6
 Output :  27
 Given number :  45
 Bit position :  2
 Output :  45
#   Ruby program
#   Turn off a particular bit in a number

class BitManipulation 
	#  Turn off given bit of a number
	def turnOffBit(num, x) 
		#  Display given number
		print("\n Given number : ", num)
		print("\n Bit position : ", x)
		if (x > 0 && x < 32) 
			#  Turn off given bit
			result = num - (num & (1 << (x - 1)))
			print("\n Output : ", result)
		else 
			print("\n Bit is outside the range \n")
		end

	end

end

def main() 
	task = BitManipulation.new()
	num = 12
	#  (12) = 1100
	#  x    = 3
	#  After turn off bit position 3  = (1000) => (8)
	task.turnOffBit(num, 3)
	num = 59
	#  (59) = (111011)
	#  x = 6
	#  After turn off bit position 6  = (011011) => (27)
	task.turnOffBit(num, 6)
	num = 45
	#  (45) = (101101)
	#  x = 2
	#  After turn off bit position 2  = (101101) => (45)
	#  Note that bit already off
	task.turnOffBit(num, 2)
end

main()

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45
/*
  Scala program
  Turn off a particular bit in a number
*/
class BitManipulation
{
	// Turn off given bit of a number
	def turnOffBit(num: Int, x: Int): Unit = {
		// Display given number
		print("\n Given number : " + num);
		print("\n Bit position : " + x);
		if (x > 0 && x < 32)
		{
			// Turn off given bit
			var result: Int = num - (num & (1 << (x - 1)));
			print("\n Output : " + result);
		}
		else
		{
			print("\n Bit is outside the range \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitManipulation = new BitManipulation();
		var num: Int = 12;
		// (12) = 1100
		// x    = 3
		// After turn off bit position 3  = (1000) => (8)
		task.turnOffBit(num, 3);
		num = 59;
		// (59) = (111011)
		// x = 6
		// After turn off bit position 6  = (011011) => (27)
		task.turnOffBit(num, 6);
		num = 45;
		// (45) = (101101)
		// x = 2
		// After turn off bit position 2  = (101101) => (45)
		// Note that bit already off
		task.turnOffBit(num, 2);
	}
}

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45
/*
  Swift 4 program
  Turn off a particular bit in a number
*/
class BitManipulation
{
	// Turn off given bit of a number
	func turnOffBit(_ num: Int, _ x: Int)
	{
		// Display given number
		print("\n Given number : ", num, terminator: "");
		print("\n Bit position : ", x, terminator: "");
		if (x > 0 && x < 32)
		{
			// Turn off given bit
			let result: Int = num - (num & (1 << (x - 1)));
			print("\n Output : ", result, terminator: "");
		}
		else
		{
			print("\n Bit is outside the range ");
		}
	}
}
func main()
{
	let task: BitManipulation = BitManipulation();
	var num: Int = 12;
	// (12) = 1100
	// x    = 3
	// After turn off bit position 3  = (1000) => (8)
	task.turnOffBit(num, 3);
	num = 59;
	// (59) = (111011)
	// x = 6
	// After turn off bit position 6  = (011011) => (27)
	task.turnOffBit(num, 6);
	num = 45;
	// (45) = (101101)
	// x = 2
	// After turn off bit position 2  = (101101) => (45)
	// Note that bit already off
	task.turnOffBit(num, 2);
}
main();

Output

 Given number :  12
 Bit position :  3
 Output :  8
 Given number :  59
 Bit position :  6
 Output :  27
 Given number :  45
 Bit position :  2
 Output :  45
/*
  Kotlin program
  Turn off a particular bit in a number
*/
class BitManipulation
{
	// Turn off given bit of a number
	fun turnOffBit(num: Int, x: Int): Unit
	{
		// Display given number
		print("\n Given number : " + num);
		print("\n Bit position : " + x);
		if (x > 0 && x < 32)
		{
			// Turn off given bit
			var result: Int = num - (num and(1 shl(x - 1)));
			print("\n Output : " + result);
		}
		else
		{
			print("\n Bit is outside the range \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitManipulation = BitManipulation();
	var num: Int = 12;
	// (12) = 1100
	// x    = 3
	// After turn off bit position 3  = (1000) => (8)
	task.turnOffBit(num, 3);
	num = 59;
	// (59) = (111011)
	// x = 6
	// After turn off bit position 6  = (011011) => (27)
	task.turnOffBit(num, 6);
	num = 45;
	// (45) = (101101)
	// x = 2
	// After turn off bit position 2  = (101101) => (45)
	// Note that bit already off
	task.turnOffBit(num, 2);
}

Output

 Given number : 12
 Bit position : 3
 Output : 8
 Given number : 59
 Bit position : 6
 Output : 27
 Given number : 45
 Bit position : 2
 Output : 45

Resultant Output Explanation: As we can see from the given examples and the pseudocode implementation, the program correctly turns off the specified bit in the given numbers. The output matches the expected results, and the function handles cases when the specified bit is already turned off (as seen in the last example).

Time Complexity of the Code: The time complexity of the given code is constant (O(1)). It does not depend on the input number or position, but rather, it performs a fixed number of bitwise operations regardless of the size of the input. Therefore, the time complexity of the turn_off_bit function remains constant.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment