Check whether all the bits are unset in the given range

Here given code implementation process.

// C Program
// Check whether all the bits are unset in the given range
#include <stdio.h>

// Determine that given bits in range of a number are set or inactive
void unsetBits(int num, int low, int high)
{
	if (low < 0 || high < 0)
	{
		// When bit position are not valid
		return;
	}
	else
	{
		// Display given number and range
		printf("\n Given Number : %d", num);
		printf("\n Given Ranges : (%d,%d) ", low, high);
		// Active all bits in range low to high
		if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) &num) == 0)
		{
			printf("\n Yes all bits are not set \n");
		}
		else
		{
			printf("\n No some bits are set \n");
		}
	}
}
int main(int argc, char const *argv[])
{
	// 69 => (1000101)
	// (1000101)
	//    ---- Range (2,5) bit position
	unsetBits(69, 2, 5);
	// 8 => (0001000)
	// (0001000)
	//  ---     Range (1,3) bit position
	unsetBits(8, 1, 3);
	// 555 => (1000101011)
	// (1000101011)
	//   ---     Range (7,9) bit position
	unsetBits(555, 7, 9);
	// 555 => (1000101011)
	// (1000101011)
	//           -- Range (1,2) bit position
	unsetBits(555, 1, 2);
	return 0;
}

Output

 Given Number : 69
 Given Ranges : (2,5)
 No some bits are set

 Given Number : 8
 Given Ranges : (1,3)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (7,9)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (1,2)
 No some bits are set
/*
  Java program
  Check whether all the bits are unset in the given range
*/
public class Activation
{
	// Determine that given bits in range of a number are set or inactive
	public void unsetBits(int num, int low, int high)
	{
		if (low < 0 || high < 0)
		{
			// When bit position are not valid
			return;
		}
		else
		{
			// Display given number and range
			System.out.print("\n Given Number : " + num);
			System.out.print("\n Given Ranges : (" + low + "," + high + ") ");
			// Active all bits in range low to high
			if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) & num) == 0)
			{
				System.out.print("\n Yes all bits are not set \n");
			}
			else
			{
				System.out.print("\n No some bits are set \n");
			}
		}
	}
	public static void main(String[] args)
	{
		Activation task = new Activation();
		// 69 => (1000101)
		// (1000101)
		//    ----  Range (2,5) bit position
		task.unsetBits(69, 2, 5);
		// 8 => (0001000)
		// (0001000)
		//  ---     Range (1,3) bit position
		task.unsetBits(8, 1, 3);
		// 555 => (1000101011)
		// (1000101011)
		//   ---       Range (7,9) bit position
		task.unsetBits(555, 7, 9);
		// 555 => (1000101011)
		// (1000101011)
		//          -- Range (1,2) bit position
		task.unsetBits(555, 1, 2);
	}
}

Output

 Given Number : 69
 Given Ranges : (2,5)
 No some bits are set

 Given Number : 8
 Given Ranges : (1,3)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (7,9)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (1,2)
 No some bits are set
// Include header file
#include <iostream>
using namespace std;

/*
  C++ program
  Check whether all the bits are unset in the given range
*/

class Activation
{
	public:
		// Determine that given bits in range of a number are set or inactive
		void unsetBits(int num, int low, int high)
		{
			if (low < 0 || high < 0)
			// When bit position are not valid
			{
				return;
			}
			else
			{
				// Display given number and range
				cout << "\n Given Number : " << num;
				cout << "\n Given Ranges : (" << low << "," << high << ") ";
				// Active all bits in range low to high
				if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) &num) == 0)
				{
					cout << "\n Yes all bits are not set \n";
				}
				else
				{
					cout << "\n No some bits are set \n";
				}
			}
		}
};
int main()
{
	Activation task = Activation();
	// 69 => (1000101)
	// (1000101)
	//    ----  Range (2,5) bit position
	task.unsetBits(69, 2, 5);
	// 8 => (0001000)
	// (0001000)
	//  ---     Range (1,3) bit position
	task.unsetBits(8, 1, 3);
	// 555 => (1000101011)
	// (1000101011)
	//   ---       Range (7,9) bit position
	task.unsetBits(555, 7, 9);
	// 555 => (1000101011)
	// (1000101011)
	//          -- Range (1,2) bit position
	task.unsetBits(555, 1, 2);
	return 0;
}

Output

 Given Number : 69
 Given Ranges : (2,5)
 No some bits are set

 Given Number : 8
 Given Ranges : (1,3)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (7,9)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (1,2)
 No some bits are set
// Include namespace system
using System;
/*
  C# program
  Check whether all the bits are unset in the given range
*/
public class Activation
{
	// Determine that given bits in range of a number are set or inactive
	public void unsetBits(int num, int low, int high)
	{
		if (low < 0 || high < 0)
		// When bit position are not valid
		{
			return;
		}
		else
		{
			// Display given number and range
			Console.Write("\n Given Number : " + num);
			Console.Write("\n Given Ranges : (" + low + "," + high + ") ");
			// Active all bits in range low to high
			if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) & num) == 0)
			{
				Console.Write("\n Yes all bits are not set \n");
			}
			else
			{
				Console.Write("\n No some bits are set \n");
			}
		}
	}
	public static void Main(String[] args)
	{
		Activation task = new Activation();
		// 69 => (1000101)
		// (1000101)
		//    ----  Range (2,5) bit position
		task.unsetBits(69, 2, 5);
		// 8 => (0001000)
		// (0001000)
		//  ---     Range (1,3) bit position
		task.unsetBits(8, 1, 3);
		// 555 => (1000101011)
		// (1000101011)
		//   ---       Range (7,9) bit position
		task.unsetBits(555, 7, 9);
		// 555 => (1000101011)
		// (1000101011)
		//          -- Range (1,2) bit position
		task.unsetBits(555, 1, 2);
	}
}

Output

 Given Number : 69
 Given Ranges : (2,5)
 No some bits are set

 Given Number : 8
 Given Ranges : (1,3)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (7,9)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (1,2)
 No some bits are set
<?php
/*
  Php program
  Check whether all the bits are unset in the given range
*/
class Activation
{
	// Determine that given bits in range of a number are set or inactive
	public	function unsetBits($num, $low, $high)
	{
		if ($low < 0 || $high < 0)
		// When bit position are not valid
		{
			return;
		}
		else
		{
			// Display given number and range
			echo "\n Given Number : ". $num;
			echo "\n Given Ranges : (". $low .",". $high .") ";
			// Active all bits in range low to high
			if (((((1 << $high) - 1) ^ ((1 << ($low - 1)) - 1)) & $num) == 0)
			{
				echo "\n Yes all bits are not set \n";
			}
			else
			{
				echo "\n No some bits are set \n";
			}
		}
	}
}

function main()
{
	$task = new Activation();
	// 69 => (1000101)
	// (1000101)
	//    ----  Range (2,5) bit position
	$task->unsetBits(69, 2, 5);
	// 8 => (0001000)
	// (0001000)
	//  ---     Range (1,3) bit position
	$task->unsetBits(8, 1, 3);
	// 555 => (1000101011)
	// (1000101011)
	//   ---       Range (7,9) bit position
	$task->unsetBits(555, 7, 9);
	// 555 => (1000101011)
	// (1000101011)
	//          -- Range (1,2) bit position
	$task->unsetBits(555, 1, 2);
}
main();

Output

 Given Number : 69
 Given Ranges : (2,5)
 No some bits are set

 Given Number : 8
 Given Ranges : (1,3)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (7,9)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (1,2)
 No some bits are set
/*
  Node Js program
  Check whether all the bits are unset in the given range
*/
class Activation
{
	// Determine that given bits in range of a number are set or inactive
	unsetBits(num, low, high)
	{
		if (low < 0 || high < 0)
		// When bit position are not valid
		{
			return;
		}
		else
		{
			// Display given number and range
			process.stdout.write("\n Given Number : " + num);
			process.stdout.write("\n Given Ranges : (" + low + "," + high + ") ");
			// Active all bits in range low to high
			if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) & num) == 0)
			{
				process.stdout.write("\n Yes all bits are not set \n");
			}
			else
			{
				process.stdout.write("\n No some bits are set \n");
			}
		}
	}
}

function main()
{
	var task = new Activation();
	// 69 => (1000101)
	// (1000101)
	//    ----  Range (2,5) bit position
	task.unsetBits(69, 2, 5);
	// 8 => (0001000)
	// (0001000)
	//  ---     Range (1,3) bit position
	task.unsetBits(8, 1, 3);
	// 555 => (1000101011)
	// (1000101011)
	//   ---       Range (7,9) bit position
	task.unsetBits(555, 7, 9);
	// 555 => (1000101011)
	// (1000101011)
	//          -- Range (1,2) bit position
	task.unsetBits(555, 1, 2);
}
main();

Output

 Given Number : 69
 Given Ranges : (2,5)
 No some bits are set

 Given Number : 8
 Given Ranges : (1,3)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (7,9)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (1,2)
 No some bits are set
#   Python 3 program
#   Check whether all the bits are unset in the given range

class Activation :
	#  Determine that given bits in range of a number are set or inactive
	def unsetBits(self, num, low, high) :
		if (low < 0 or high < 0) :
			#  When bit position are not valid
			return
		else :
			#  Display given number and range
			print("\n Given Number : ", num, end = "")
			print("\n Given Ranges : (", low ,",", high ,") ", end = "")
			#  Active all bits in range low to high
			if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) & num) == 0) :
				print("\n Yes all bits are not set ")
			else :
				print("\n No some bits are set ")
			
		
	

def main() :
	task = Activation()
	#  69 => (1000101)
	#  (1000101)
	#     ----  Range (2,5) bit position
	task.unsetBits(69, 2, 5)
	#  8 => (0001000)
	#  (0001000)
	#   ---     Range (1,3) bit position
	task.unsetBits(8, 1, 3)
	#  555 => (1000101011)
	#  (1000101011)
	#    ---       Range (7,9) bit position
	task.unsetBits(555, 7, 9)
	#  555 => (1000101011)
	#  (1000101011)
	#           -- Range (1,2) bit position
	task.unsetBits(555, 1, 2)

if __name__ == "__main__": main()

Output

 Given Number :  69
 Given Ranges : ( 2 , 5 )
 No some bits are set

 Given Number :  8
 Given Ranges : ( 1 , 3 )
 Yes all bits are not set

 Given Number :  555
 Given Ranges : ( 7 , 9 )
 Yes all bits are not set

 Given Number :  555
 Given Ranges : ( 1 , 2 )
 No some bits are set
#   Ruby program
#   Check whether all the bits are unset in the given range

class Activation 
	#  Determine that given bits in range of a number are set or inactive
	def unsetBits(num, low, high) 
		if (low < 0 || high < 0) 
			#  When bit position are not valid
			return
		else 
			#  Display given number and range
			print("\n Given Number : ", num)
			print("\n Given Ranges : (", low ,",", high ,") ")
			#  Active all bits in range low to high
			if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) & num) == 0) 
				print("\n Yes all bits are not set \n")
			else 
				print("\n No some bits are set \n")
			end

		end

	end

end

def main() 
	task = Activation.new()
	#  69 => (1000101)
	#  (1000101)
	#     ----  Range (2,5) bit position
	task.unsetBits(69, 2, 5)
	#  8 => (0001000)
	#  (0001000)
	#   ---     Range (1,3) bit position
	task.unsetBits(8, 1, 3)
	#  555 => (1000101011)
	#  (1000101011)
	#    ---       Range (7,9) bit position
	task.unsetBits(555, 7, 9)
	#  555 => (1000101011)
	#  (1000101011)
	#           -- Range (1,2) bit position
	task.unsetBits(555, 1, 2)
end

main()

Output

 Given Number : 69
 Given Ranges : (2,5) 
 No some bits are set 

 Given Number : 8
 Given Ranges : (1,3) 
 Yes all bits are not set 

 Given Number : 555
 Given Ranges : (7,9) 
 Yes all bits are not set 

 Given Number : 555
 Given Ranges : (1,2) 
 No some bits are set 
/*
  Scala program
  Check whether all the bits are unset in the given range
*/
class Activation
{
	// Determine that given bits in range of a number are set or inactive
	def unsetBits(num: Int, low: Int, high: Int): Unit = {
		if (low < 0 || high < 0)
		// When bit position are not valid
		{
			return;
		}
		else
		{
			// Display given number and range
			print("\n Given Number : " + num);
			print("\n Given Ranges : (" + low + "," + high + ") ");
			// Active all bits in range low to high
			if (((((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) & num) == 0)
			{
				print("\n Yes all bits are not set \n");
			}
			else
			{
				print("\n No some bits are set \n");
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Activation = new Activation();
		// 69 => (1000101)
		// (1000101)
		//    ----  Range (2,5) bit position
		task.unsetBits(69, 2, 5);
		// 8 => (0001000)
		// (0001000)
		//  ---     Range (1,3) bit position
		task.unsetBits(8, 1, 3);
		// 555 => (1000101011)
		// (1000101011)
		//   ---       Range (7,9) bit position
		task.unsetBits(555, 7, 9);
		// 555 => (1000101011)
		// (1000101011)
		//          -- Range (1,2) bit position
		task.unsetBits(555, 1, 2);
	}
}

Output

 Given Number : 69
 Given Ranges : (2,5)
 No some bits are set

 Given Number : 8
 Given Ranges : (1,3)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (7,9)
 Yes all bits are not set

 Given Number : 555
 Given Ranges : (1,2)
 No some bits are set
/*
  Swift 4 program
  Check whether all the bits are unset in the given range
*/
class Activation
{
	// Determine that given bits in range of a number are set or inactive
	func unsetBits(_ num: Int, _ low: Int, _ high: Int)
	{
		if (low < 0 || high < 0)
		// When bit position are not valid
		{
			return;
		}
		else
		{
			// Display given number and range
			print("\n Given Number : ", num, terminator: "");
			print("\n Given Ranges : (", low ,",", high ,") ", terminator: "");
			// Active all bits in range low to high
          	let s = ((1 << (low - 1)) - 1);
			if (((((1 << high) - 1) ^ s) & num) == 0)
			{
				print("\n Yes all bits are not set ");
			}
			else
			{
				print("\n No some bits are set ");
			}
		}
	}
}
func main()
{
	let task: Activation = Activation();
	// 69 => (1000101)
	// (1000101)
	//    ----  Range (2,5) bit position
	task.unsetBits(69, 2, 5);
	// 8 => (0001000)
	// (0001000)
	//  ---     Range (1,3) bit position
	task.unsetBits(8, 1, 3);
	// 555 => (1000101011)
	// (1000101011)
	//   ---       Range (7,9) bit position
	task.unsetBits(555, 7, 9);
	// 555 => (1000101011)
	// (1000101011)
	//          -- Range (1,2) bit position
	task.unsetBits(555, 1, 2);
}
main();

Output

 Given Number :  69
 Given Ranges : ( 2 , 5 )
 No some bits are set

 Given Number :  8
 Given Ranges : ( 1 , 3 )
 Yes all bits are not set

 Given Number :  555
 Given Ranges : ( 7 , 9 )
 Yes all bits are not set

 Given Number :  555
 Given Ranges : ( 1 , 2 )
 No some bits are set


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