Turn off a particular bit in a number

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


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







© 2021, kalkicode.com, All rights reserved