Posted on by Kalkicode
Code Bit Logic

Unset least K significant bits of a given number

The problem at hand is to unset the least K significant bits of a given integer number. In binary representation, the least significant bits are the rightmost bits, and "unsetting" means converting those bits to 0 while keeping the rest of the bits unchanged. The task is to perform this operation efficiently and obtain the resulting number after unsetting the specified bits.

Example

Let's consider a simple example to understand the problem. Suppose we have the number 123, which is represented in binary as 1111011. Now, if we want to unset the 4 least significant bits, the binary representation would be 1111000, which corresponds to the number 112.

Pseudocode

unsetLeastKBits(number, k):
    if k < 1:
        return
    result = number & (-1 << k)
    return result

Algorithm and Explanation

  1. The function unsetLeastKBits takes two inputs: number, the given integer, and k, the number of least significant bits to unset.
  2. The first check ensures that the value of k is valid (greater than or equal to 1). If k is less than 1, the function simply returns without performing any operation.
  3. The key step is the bitwise operation: result = number & (-1 << k). Here's how it works:
    • (-1 << k) creates a bit mask with k least significant bits set to 0 and the rest set to 1. For example, if k is 4, (-1 << 4) results in 11110000.
    • The & (bitwise AND) operation between number and the bit mask (-1 << k) effectively unsets the k least significant bits in number.
  4. The function returns the resulting number after unsetting the specified bits.

Code Solution

Here given code implementation process.

// C program for 
// Unset least K significant bits of a given number
#include <stdio.h>

void unsetLeastKBits(int number, int k)
{
	if (k < 1)
	{
		return;
	}
	// unset k least significant bits
	int result = number & (-1 << k);
	// Display calculated result
	printf(" Number  : %d\n", number);
	printf(" K       : %d\n", k);
	printf(" Result  : %d\n\n", result);
}
int main(int argc, char
	const *argv[])
{
	// Example A
	// number = 123  binary (1 1 1 1 0 1 1)
	// When k = 4
	// (1 1 1 1 0 1 1)
	//        –––––––  [ last 4 bits]
	// After unset active bits
	// (1 1 1 0 0 0 0)
	//        –––––––
	//  Result = 112
	unsetLeastKBits(123, 4);
	// Example B
	// number = 7  binary ( 0 0 1 1 1 )
	// When k = 3
	// ( 0 0 1 1 1)
	//       –––––  [ last 3 bits]
	// After unset active bits
	// (0 0 0 0 0)
	//      ––––– 
	//  Result = 0
	unsetLeastKBits(7, 3);
	// Example C
	// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	// When k = 7
	// ( 1 0 0 1 0 1 1 0 0 0 1 1)
	//             ––––––––––––– [ last 7 bits]
	// After unset active bits
	// ( 1 0 0 1 0 0 0 0 0 0 0 0)
	//             ––––––––––––– 
	//  Result = 2304
	unsetLeastKBits(2403, 7);
	// Example D
	// number = 9  binary ( 1 0 0 1 )
	// When k = 1
	// ( 1 0 0 1 )
	//         – [ last 1 bits]
	// After unset active bits
	// ( 1 0 0 0 )
	//         –
	//  Result = 8
	unsetLeastKBits(9, 1);
	return 0;
}

input

 Number  : 123
 K       : 4
 Result  : 112

 Number  : 7
 K       : 3
 Result  : 0

 Number  : 2403
 K       : 7
 Result  : 2304

 Number  : 9
 K       : 1
 Result  : 8
/*
  Java Program for 
  Unset least K significant bits of a given number
*/
public class Manipulation
{
    public void unsetLeastKBits(int number, int k)
{
    if (k < 1)
    {
        return;
    }
    // unset k least significant bits
    int result = number & (-1 << k);
    // Display calculated result
    System.out.println(" Number : " + number );
    System.out.println(" K : " + k );
    System.out.println(" Result : " + result + "\n");
}
    public static void main(String[] args)
    {
        Manipulation task = new Manipulation();
        // Test Cases
        // Example A
        // number = 123  binary (1 1 1 1 0 1 1)
        // When k = 4
        // (1 1 1 1 0 1 1)
        //        –––––––  [ last 4 bits]
        // After unset active bits
        // (1 1 1 0 0 0 0)
        //        –––––––
        //  Result = 112
        task.unsetLeastKBits(123, 4);
        // Example B
        // number = 7  binary ( 0 0 1 1 1 )
        // When k = 3
        // ( 0 0 1 1 1)
        //       –––––  [ last 3 bits]
        // After unset active bits
        // (0 0 0 0 0)
        //      ––––– 
        //  Result = 0
        task.unsetLeastKBits(7  , 3);   
        // Example C
        // number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
        // When k = 7
        // ( 1 0 0 1 0 1 1 0 0 0 1 1)
        //             ––––––––––––– [ last 7 bits]
        // After unset active bits
        // ( 1 0 0 1 0 0 0 0 0 0 0 0)
        //             ––––––––––––– 
        //  Result = 2304
        task.unsetLeastKBits(2403  , 7);   
        
        // Example D
        // number = 9  binary ( 1 0 0 1 )
        // When k = 1
        // ( 1 0 0 1 )
        //         – [ last 1 bits]
        // After unset active bits
        // ( 1 0 0 0 )
        //         –
        //  Result = 8
        task.unsetLeastKBits(9  , 1);  

    }
}

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for 
  Unset least K significant bits of a given number
*/
class Manipulation
{
	public: void unsetLeastKBits(int number, int k)
	{
		if (k < 1)
		{
			return;
		}
		// unset k least significant bits
		int result = number &(-1 << k);
		// Display calculated result
		cout << " Number : " << number << endl;
		cout << " K : " << k << endl;
		cout << " Result : " << result << "\n" << endl;
	}
};
int main()
{
	Manipulation *task = new Manipulation();
	// Test Cases
	// Example A
	// number = 123  binary (1 1 1 1 0 1 1)
	// When k = 4
	// (1 1 1 1 0 1 1)
	//        –––––––  [ last 4 bits]
	// After unset active bits
	// (1 1 1 0 0 0 0)
	//        –––––––
	//  Result = 112
	task->unsetLeastKBits(123, 4);
	// Example B
	// number = 7  binary ( 0 0 1 1 1 )
	// When k = 3
	// ( 0 0 1 1 1)
	//       –––––  [ last 3 bits]
	// After unset active bits
	// (0 0 0 0 0)
	//      ––––– 
	//  Result = 0
	task->unsetLeastKBits(7, 3);
	// Example C
	// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	// When k = 7
	// ( 1 0 0 1 0 1 1 0 0 0 1 1)
	//             ––––––––––––– [ last 7 bits]
	// After unset active bits
	// ( 1 0 0 1 0 0 0 0 0 0 0 0)
	//             ––––––––––––– 
	//  Result = 2304
	task->unsetLeastKBits(2403, 7);
	// Example D
	// number = 9  binary ( 1 0 0 1 )
	// When k = 1
	// ( 1 0 0 1 )
	//         – [ last 1 bits]
	// After unset active bits
	// ( 1 0 0 0 )
	//         –
	//  Result = 8
	task->unsetLeastKBits(9, 1);
	return 0;
}

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8
// Include namespace system
using System;
/*
  Csharp Program for 
  Unset least K significant bits of a given number
*/
public class Manipulation
{
	public void unsetLeastKBits(int number, int k)
	{
		if (k < 1)
		{
			return;
		}
		// unset k least significant bits
		int result = number & (-1 << k);
		// Display calculated result
		Console.WriteLine(" Number : " + number);
		Console.WriteLine(" K : " + k);
		Console.WriteLine(" Result : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		Manipulation task = new Manipulation();
		// Test Cases
		// Example A
		// number = 123  binary (1 1 1 1 0 1 1)
		// When k = 4
		// (1 1 1 1 0 1 1)
		//        –––––––  [ last 4 bits]
		// After unset active bits
		// (1 1 1 0 0 0 0)
		//        –––––––
		//  Result = 112
		task.unsetLeastKBits(123, 4);
		// Example B
		// number = 7  binary ( 0 0 1 1 1 )
		// When k = 3
		// ( 0 0 1 1 1)
		//       –––––  [ last 3 bits]
		// After unset active bits
		// (0 0 0 0 0)
		//      ––––– 
		//  Result = 0
		task.unsetLeastKBits(7, 3);
		// Example C
		// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
		// When k = 7
		// ( 1 0 0 1 0 1 1 0 0 0 1 1)
		//             ––––––––––––– [ last 7 bits]
		// After unset active bits
		// ( 1 0 0 1 0 0 0 0 0 0 0 0)
		//             ––––––––––––– 
		//  Result = 2304
		task.unsetLeastKBits(2403, 7);
		// Example D
		// number = 9  binary ( 1 0 0 1 )
		// When k = 1
		// ( 1 0 0 1 )
		//         – [ last 1 bits]
		// After unset active bits
		// ( 1 0 0 0 )
		//         –
		//  Result = 8
		task.unsetLeastKBits(9, 1);
	}
}

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8
<?php
/*
  Php Program for 
  Unset least K significant bits of a given number
*/
class Manipulation
{
	public	function unsetLeastKBits($number, $k)
	{
		if ($k < 1)
		{
			return;
		}
		// unset k least significant bits
		$result = $number & (-1 << $k);
		// Display calculated result
		echo " Number : ".$number.
		"\n";
		echo " K : ".$k.
		"\n";
		echo " Result : ".$result.
		"\n".
		"\n";
	}
}

function main()
{
	$task = new Manipulation();
	// Test Cases
	// Example A
	// number = 123  binary (1 1 1 1 0 1 1)
	// When k = 4
	// (1 1 1 1 0 1 1)
	//        –––––––  [ last 4 bits]
	// After unset active bits
	// (1 1 1 0 0 0 0)
	//        –––––––
	//  Result = 112
	$task->unsetLeastKBits(123, 4);
	// Example B
	// number = 7  binary ( 0 0 1 1 1 )
	// When k = 3
	// ( 0 0 1 1 1)
	//       –––––  [ last 3 bits]
	// After unset active bits
	// (0 0 0 0 0)
	//      ––––– 
	//  Result = 0
	$task->unsetLeastKBits(7, 3);
	// Example C
	// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	// When k = 7
	// ( 1 0 0 1 0 1 1 0 0 0 1 1)
	//             ––––––––––––– [ last 7 bits]
	// After unset active bits
	// ( 1 0 0 1 0 0 0 0 0 0 0 0)
	//             ––––––––––––– 
	//  Result = 2304
	$task->unsetLeastKBits(2403, 7);
	// Example D
	// number = 9  binary ( 1 0 0 1 )
	// When k = 1
	// ( 1 0 0 1 )
	//         – [ last 1 bits]
	// After unset active bits
	// ( 1 0 0 0 )
	//         –
	//  Result = 8
	$task->unsetLeastKBits(9, 1);
}
main();

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8
/*
  Node JS Program for 
  Unset least K significant bits of a given number
*/
class Manipulation
{
	unsetLeastKBits(number, k)
	{
		if (k < 1)
		{
			return;
		}
		// unset k least significant bits
		var result = number & (-1 << k);
		// Display calculated result
		console.log(" Number : " + number);
		console.log(" K : " + k);
		console.log(" Result : " + result + "\n");
	}
}

function main()
{
	var task = new Manipulation();
	// Test Cases
	// Example A
	// number = 123  binary (1 1 1 1 0 1 1)
	// When k = 4
	// (1 1 1 1 0 1 1)
	//        –––––––  [ last 4 bits]
	// After unset active bits
	// (1 1 1 0 0 0 0)
	//        –––––––
	//  Result = 112
	task.unsetLeastKBits(123, 4);
	// Example B
	// number = 7  binary ( 0 0 1 1 1 )
	// When k = 3
	// ( 0 0 1 1 1)
	//       –––––  [ last 3 bits]
	// After unset active bits
	// (0 0 0 0 0)
	//      ––––– 
	//  Result = 0
	task.unsetLeastKBits(7, 3);
	// Example C
	// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	// When k = 7
	// ( 1 0 0 1 0 1 1 0 0 0 1 1)
	//             ––––––––––––– [ last 7 bits]
	// After unset active bits
	// ( 1 0 0 1 0 0 0 0 0 0 0 0)
	//             ––––––––––––– 
	//  Result = 2304
	task.unsetLeastKBits(2403, 7);
	// Example D
	// number = 9  binary ( 1 0 0 1 )
	// When k = 1
	// ( 1 0 0 1 )
	//         – [ last 1 bits]
	// After unset active bits
	// ( 1 0 0 0 )
	//         –
	//  Result = 8
	task.unsetLeastKBits(9, 1);
}
main();

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8
#  Python 3 Program for 
#  Unset least K significant bits of a given number
class Manipulation :
	def unsetLeastKBits(self, number, k) :
		if (k < 1) :
			return
		
		result = number & (-1 << k)
		#  Display calculated result
		print(" Number : ", number)
		print(" K : ", k)
		print(" Result : ", result ,"\n")
	

def main() :
	task = Manipulation()
	#  Test Cases
	#  Example A
	#  number = 123  binary (1 1 1 1 0 1 1)
	#  When k = 4
	#  (1 1 1 1 0 1 1)
	#         –––––––  [ last 4 bits]
	#  After unset active bits
	#  (1 1 1 0 0 0 0)
	#         –––––––
	#   Result = 112
	task.unsetLeastKBits(123, 4)
	#  Example B
	#  number = 7  binary ( 0 0 1 1 1 )
	#  When k = 3
	#  ( 0 0 1 1 1)
	#        –––––  [ last 3 bits]
	#  After unset active bits
	#  (0 0 0 0 0)
	#       ––––– 
	#   Result = 0
	task.unsetLeastKBits(7, 3)
	#  Example C
	#  number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	#  When k = 7
	#  ( 1 0 0 1 0 1 1 0 0 0 1 1)
	#              ––––––––––––– [ last 7 bits]
	#  After unset active bits
	#  ( 1 0 0 1 0 0 0 0 0 0 0 0)
	#              ––––––––––––– 
	#   Result = 2304
	task.unsetLeastKBits(2403, 7)
	#  Example D
	#  number = 9  binary ( 1 0 0 1 )
	#  When k = 1
	#  ( 1 0 0 1 )
	#          – [ last 1 bits]
	#  After unset active bits
	#  ( 1 0 0 0 )
	#          –
	#   Result = 8
	task.unsetLeastKBits(9, 1)

if __name__ == "__main__": main()

input

 Number :  123
 K :  4
 Result :  112

 Number :  7
 K :  3
 Result :  0

 Number :  2403
 K :  7
 Result :  2304

 Number :  9
 K :  1
 Result :  8
#  Ruby Program for 
#  Unset least K significant bits of a given number
class Manipulation 
	def unsetLeastKBits(number, k) 
		if (k < 1) 
			return
		end

		#  unset k least significant bits
		result = number & (-1 << k)
		#  Display calculated result
		print(" Number : ", number, "\n")
		print(" K : ", k, "\n")
		print(" Result : ", result ,"\n", "\n")
	end

end

def main() 
	task = Manipulation.new()
	#  Test Cases
	#  Example A
	#  number = 123  binary (1 1 1 1 0 1 1)
	#  When k = 4
	#  (1 1 1 1 0 1 1)
	#         –––––––  [ last 4 bits]
	#  After unset active bits
	#  (1 1 1 0 0 0 0)
	#         –––––––
	#   Result = 112
	task.unsetLeastKBits(123, 4)
	#  Example B
	#  number = 7  binary ( 0 0 1 1 1 )
	#  When k = 3
	#  ( 0 0 1 1 1)
	#        –––––  [ last 3 bits]
	#  After unset active bits
	#  (0 0 0 0 0)
	#       ––––– 
	#   Result = 0
	task.unsetLeastKBits(7, 3)
	#  Example C
	#  number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	#  When k = 7
	#  ( 1 0 0 1 0 1 1 0 0 0 1 1)
	#              ––––––––––––– [ last 7 bits]
	#  After unset active bits
	#  ( 1 0 0 1 0 0 0 0 0 0 0 0)
	#              ––––––––––––– 
	#   Result = 2304
	task.unsetLeastKBits(2403, 7)
	#  Example D
	#  number = 9  binary ( 1 0 0 1 )
	#  When k = 1
	#  ( 1 0 0 1 )
	#          – [ last 1 bits]
	#  After unset active bits
	#  ( 1 0 0 0 )
	#          –
	#   Result = 8
	task.unsetLeastKBits(9, 1)
end

main()

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8

/*
  Scala Program for 
  Unset least K significant bits of a given number
*/
class Manipulation()
{
	def unsetLeastKBits(number: Int, k: Int): Unit = {
		if (k < 1)
		{
			return;
		}
		// unset k least significant bits
		var result: Int = number & (-1 << k);
		// Display calculated result
		println(" Number : " + number);
		println(" K : " + k);
		println(" Result : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Manipulation = new Manipulation();
		// Test Cases
		// Example A
		// number = 123  binary (1 1 1 1 0 1 1)
		// When k = 4
		// (1 1 1 1 0 1 1)
		//        –––––––  [ last 4 bits]
		// After unset active bits
		// (1 1 1 0 0 0 0)
		//        –––––––
		//  Result = 112
		task.unsetLeastKBits(123, 4);
		// Example B
		// number = 7  binary ( 0 0 1 1 1 )
		// When k = 3
		// ( 0 0 1 1 1)
		//       –––––  [ last 3 bits]
		// After unset active bits
		// (0 0 0 0 0)
		//      ––––– 
		//  Result = 0
		task.unsetLeastKBits(7, 3);
		// Example C
		// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
		// When k = 7
		// ( 1 0 0 1 0 1 1 0 0 0 1 1)
		//             ––––––––––––– [ last 7 bits]
		// After unset active bits
		// ( 1 0 0 1 0 0 0 0 0 0 0 0)
		//             ––––––––––––– 
		//  Result = 2304
		task.unsetLeastKBits(2403, 7);
		// Example D
		// number = 9  binary ( 1 0 0 1 )
		// When k = 1
		// ( 1 0 0 1 )
		//         – [ last 1 bits]
		// After unset active bits
		// ( 1 0 0 0 )
		//         –
		//  Result = 8
		task.unsetLeastKBits(9, 1);
	}
}

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8
/*
  Swift 4 Program for 
  Unset least K significant bits of a given number
*/
class Manipulation
{
	func unsetLeastKBits(_ number: Int, _ k: Int)
	{
		if (k < 1)
		{
			return;
		}
		// unset k least significant bits
		let result: Int = number & (-1 << k);
		// Display calculated result
		print(" Number : ", number);
		print(" K : ", k);
		print(" Result : ", result ,"\n");
	}
}
func main()
{
	let task: Manipulation = Manipulation();
	// Test Cases
	// Example A
	// number = 123  binary (1 1 1 1 0 1 1)
	// When k = 4
	// (1 1 1 1 0 1 1)
	//        –––––––  [ last 4 bits]
	// After unset active bits
	// (1 1 1 0 0 0 0)
	//        –––––––
	//  Result = 112
	task.unsetLeastKBits(123, 4);
	// Example B
	// number = 7  binary ( 0 0 1 1 1 )
	// When k = 3
	// ( 0 0 1 1 1)
	//       –––––  [ last 3 bits]
	// After unset active bits
	// (0 0 0 0 0)
	//      ––––– 
	//  Result = 0
	task.unsetLeastKBits(7, 3);
	// Example C
	// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	// When k = 7
	// ( 1 0 0 1 0 1 1 0 0 0 1 1)
	//             ––––––––––––– [ last 7 bits]
	// After unset active bits
	// ( 1 0 0 1 0 0 0 0 0 0 0 0)
	//             ––––––––––––– 
	//  Result = 2304
	task.unsetLeastKBits(2403, 7);
	// Example D
	// number = 9  binary ( 1 0 0 1 )
	// When k = 1
	// ( 1 0 0 1 )
	//         – [ last 1 bits]
	// After unset active bits
	// ( 1 0 0 0 )
	//         –
	//  Result = 8
	task.unsetLeastKBits(9, 1);
}
main();

input

 Number :  123
 K :  4
 Result :  112

 Number :  7
 K :  3
 Result :  0

 Number :  2403
 K :  7
 Result :  2304

 Number :  9
 K :  1
 Result :  8
/*
  Kotlin Program for 
  Unset least K significant bits of a given number
*/
class Manipulation
{
	fun unsetLeastKBits(number: Int, k: Int): Unit
	{
		if (k < 1)
		{
			return;
		}
		// unset k least significant bits
		val result: Int = number and (-1 shl k);
		// Display calculated result
		println(" Number : " + number);
		println(" K : " + k);
		println(" Result : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Manipulation = Manipulation();
	// Test Cases
	// Example A
	// number = 123  binary (1 1 1 1 0 1 1)
	// When k = 4
	// (1 1 1 1 0 1 1)
	//        –––––––  [ last 4 bits]
	// After unset active bits
	// (1 1 1 0 0 0 0)
	//        –––––––
	//  Result = 112
	task.unsetLeastKBits(123, 4);
	// Example B
	// number = 7  binary ( 0 0 1 1 1 )
	// When k = 3
	// ( 0 0 1 1 1)
	//       –––––  [ last 3 bits]
	// After unset active bits
	// (0 0 0 0 0)
	//      ––––– 
	//  Result = 0
	task.unsetLeastKBits(7, 3);
	// Example C
	// number = 2403  binary ( 1 0 0 1 0 1 1 0 0 0 1 1 )
	// When k = 7
	// ( 1 0 0 1 0 1 1 0 0 0 1 1)
	//             ––––––––––––– [ last 7 bits]
	// After unset active bits
	// ( 1 0 0 1 0 0 0 0 0 0 0 0)
	//             ––––––––––––– 
	//  Result = 2304
	task.unsetLeastKBits(2403, 7);
	// Example D
	// number = 9  binary ( 1 0 0 1 )
	// When k = 1
	// ( 1 0 0 1 )
	//         – [ last 1 bits]
	// After unset active bits
	// ( 1 0 0 0 )
	//         –
	//  Result = 8
	task.unsetLeastKBits(9, 1);
}

input

 Number : 123
 K : 4
 Result : 112

 Number : 7
 K : 3
 Result : 0

 Number : 2403
 K : 7
 Result : 2304

 Number : 9
 K : 1
 Result : 8

Output Explanation

  1. For input number = 123 and k = 4, the output is Result = 112, as explained in the example above.
  2. For input number = 7 and k = 3, the output is Result = 0. In binary representation, 7 is 0111, and after unsetting the 3 least significant bits, we get 0000, which corresponds to 0.
  3. For input number = 2403 and k = 7, the output is Result = 2304. In binary representation, 2403 is 1001011000011, and after unsetting the 7 least significant bits, we get 1001000000000, which corresponds to 2304.
  4. For input number = 9 and k = 1, the output is Result = 8. In binary representation, 9 is 1001, and after unsetting the least significant bit, we get 1000, which corresponds to 8.

Time Complexity

The time complexity of the given code is O(1) because it performs a fixed number of bitwise operations, regardless of the magnitude of the input number. The number of operations remains constant, so the time complexity is constant or O(1).

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