Posted on by Kalkicode
Code Bit Logic

Check whether all the bits are unset in the given range

The given problem is to check whether all the bits in a specified range of a given number are unset (0) or not. The problem requires us to determine whether the bits in the given range are all zeros or not. The range is specified by two positions, low and high, where low represents the least significant bit position, and high represents the most significant bit position. We need to find a solution that checks if all the bits within the specified range are unset or not, and then display the result.

Explanation with Suitable Example: Let's understand the problem with an example. Consider the number 69, represented in binary as 1000101. Suppose we want to check whether all the bits in the range from 2 to 5 (inclusive) are unset or not. So, the bits in this range are 00010. Since there is at least one set bit (1) in this range, the result should be "No, some bits are set."

Pseudocode: Before diving into the detailed explanation and algorithm, let's first present a simple pseudocode for the given problem:

unsetBits(num, low, high):
    if low < 0 or high < 0:
        // Invalid bit positions
        return
    else:
        range_bits = num & ((1 << (high - low + 1)) - 1) << low
        if range_bits == 0:
            print "Yes, all bits are not set"
        else:
            print "No, some bits are set"

Algorithm with Explanation:

  1. Start with the unsetBits function that takes three parameters: num (the given number), low (the least significant bit position of the range), and high (the most significant bit position of the range).

  2. Check if low or high is less than 0. If either of them is negative, it means the bit positions are not valid, and we return from the function.

  3. Otherwise, calculate the bits within the specified range. To do this, we perform the following steps:

    • We create a bitmask with ones in the range of bits we are interested in (from low to high) and zeros elsewhere.
    • We then use bitwise AND with the given number (num) to extract the bits within the specified range.
  4. Check if the bits within the range are equal to 0. If they are, then all the bits in the specified range are unset, and we print "Yes, all bits are not set." Otherwise, if there is at least one set bit in the range, we print "No, some bits are set."

Resultant Output Explanation: Let's go through the output of the given code for each test case:

  1. unsetBits(69, 2, 5);

    • Given Number: 69 (1000101 in binary)
    • Given Ranges: (2, 5)
    • Range bits: 00010
    • Since there is at least one set bit (1) in the range, the output is "No, some bits are set."
  2. unsetBits(8, 1, 3);

    • Given Number: 8 (0001000 in binary)
    • Given Ranges: (1, 3)
    • Range bits: 000
    • Since all bits in the range are unset (0), the output is "Yes, all bits are not set."
  3. unsetBits(555, 7, 9);

    • Given Number: 555 (1000101011 in binary)
    • Given Ranges: (7, 9)
    • Range bits: 000
    • Again, all bits in the range are unset (0), so the output is "Yes, all bits are not set."
  4. unsetBits(555, 1, 2);

    • Given Number: 555 (1000101011 in binary)
    • Given Ranges: (1, 2)
    • Range bits: 10
    • There is at least one set bit (1) in the range, resulting in the output "No, some bits are set."

Code Solution

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

Time Complexity of the Code: The time complexity of the given code is constant, O(1), because the operations performed (bitwise AND, bitwise XOR, etc.) do not depend on the size of the input number or the range. The number of iterations remains constant, so the time complexity does not grow with the input size.

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