Unset least K significant bits of a given number

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

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