Toggle the last n bits

Here given code implementation process.

// C Program 
// Toggle the last n bits
#include <stdio.h>

// Change last n bits of a given number
void changeLNBits(int num, int n)
{
	// Display given number
	printf("\n Number : %d", num);
	// Change last n bit
	int result = num ^ (1 << n) - 1;
	// Display calculated result
	printf("\n After change last %d bits : %d\n", n, result);
}
int main()
{
	// (15) 1111 => 1000  n=3
	changeLNBits(15, 3);
	// (100) 1100100 => 1111011  n = 5
	changeLNBits(100, 5);
	// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	// (-10) 11110110 => 11110001 (n = 3)
	changeLNBits(-10, 3);
	return 0;
}

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
/*
  Java Program for
  Toggle the last n bits
*/
class ChangeBits
{
	// Change last n bits of a given number
	public void changeLNBits(int num, int n)
	{
		// Display given number
		System.out.print("\n Number : " + num);
		// Change last n bit
		int result = num ^ (1 << n) - 1;
		// Display calculated result
		System.out.print("\n After change last " + n + " bits : " + result + "\n");
	}
	public static void main(String[] args)
	{
		ChangeBits task = new ChangeBits();
		// (15) 1111 => 1000  n=3
		task.changeLNBits(15, 3);
		// (100) 1100100 => 1111011  n = 5
		task.changeLNBits(100, 5);
		// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
		// (-10) 11110110 => 11110001  (n = 3)
		task.changeLNBits(-10, 3);
	}
}

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Toggle the last n bits
*/
class ChangeBits
{
	public:
		// Change last n bits of a given number
		void changeLNBits(int num, int n)
		{
			// Display given number
			cout << "\n Number : " << num;
			// Change last n bit
			int result = num ^ (1 << n) - 1;
			// Display calculated result
			cout << "\n After change last " << n << " bits : " << result << "\n";
		}
};
int main()
{
	ChangeBits task = ChangeBits();
	// (15) 1111 => 1000  n=3
	task.changeLNBits(15, 3);
	// (100) 1100100 => 1111011  n = 5
	task.changeLNBits(100, 5);
	// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	// (-10) 11110110 => 11110001  (n = 3)
	task.changeLNBits(-10, 3);
	return 0;
}

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
// Include namespace system
using System;
/*
  C# Program for
  Toggle the last n bits
*/
public class ChangeBits
{
	// Change last n bits of a given number
	public void changeLNBits(int num, int n)
	{
		// Display given number
		Console.Write("\n Number : " + num);
		// Change last n bit
		int result = num ^ (1 << n) - 1;
		// Display calculated result
		Console.Write("\n After change last " + n + " bits : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		ChangeBits task = new ChangeBits();
		// (15) 1111 => 1000  n=3
		task.changeLNBits(15, 3);
		// (100) 1100100 => 1111011  n = 5
		task.changeLNBits(100, 5);
		// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
		// (-10) 11110110 => 11110001  (n = 3)
		task.changeLNBits(-10, 3);
	}
}

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
<?php
/*
  Php Program for
  Toggle the last n bits
*/
class ChangeBits
{
	// Change last n bits of a given number
	public	function changeLNBits($num, $n)
	{
		// Display given number
		echo "\n Number : ". $num;
		// Change last n bit
		$result = $num ^ (1 << $n) - 1;
		// Display calculated result
		echo "\n After change last ". $n ." bits : ". $result ."\n";
	}
}

function main()
{
	$task = new ChangeBits();
	// (15) 1111 => 1000  n=3
	$task->changeLNBits(15, 3);
	// (100) 1100100 => 1111011  n = 5
	$task->changeLNBits(100, 5);
	// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	// (-10) 11110110 => 11110001  (n = 3)
	$task->changeLNBits(-10, 3);
}
main();

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
/*
  Node Js Program for
  Toggle the last n bits
*/
class ChangeBits
{
	// Change last n bits of a given number
	changeLNBits(num, n)
	{
		// Display given number
		process.stdout.write("\n Number : " + num);
		// Change last n bit
		var result = num ^ (1 << n) - 1;
		// Display calculated result
		process.stdout.write("\n After change last " + n + " bits : " + result + "\n");
	}
}

function main()
{
	var task = new ChangeBits();
	// (15) 1111 => 1000  n=3
	task.changeLNBits(15, 3);
	// (100) 1100100 => 1111011  n = 5
	task.changeLNBits(100, 5);
	// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	// (-10) 11110110 => 11110001 (n = 3)
	task.changeLNBits(-10, 3);
}
main();

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
#   Python 3 Program for
#   Toggle the last n bits

class ChangeBits :
	#  Change last n bits of a given number
	def changeLNBits(self, num, n) :
		#  Display given number
		print("\n Number : ", num, end = "")
		#  Change last n bit
		result = num ^ (1 << n) - 1
		#  Display calculated result
		print("\n After change last ", n ," bits : ", result )
	

def main() :
	task = ChangeBits()
	#  (15) 1111 => 1000  n=3
	task.changeLNBits(15, 3)
	#  (100) 1100100 => 1111011  n = 5
	task.changeLNBits(100, 5)
	#  (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	#  (-10) 11110110 => 11110001  (n = 3)
	task.changeLNBits(-10, 3)

if __name__ == "__main__": main()

Output

 Number :  15
 After change last  3  bits :  8

 Number :  100
 After change last  5  bits :  123

 Number :  -10
 After change last  3  bits :  -15
#   Ruby Program for
#   Toggle the last n bits

class ChangeBits 
	#  Change last n bits of a given number
	def changeLNBits(num, n) 
		#  Display given number
		print("\n Number : ", num)
		#  Change last n bit
		result = num ^ (1 << n) - 1
		#  Display calculated result
		print("\n After change last ", n ," bits : ", result ,"\n")
	end

end

def main() 
	task = ChangeBits.new()
	#  (15) 1111 => 1000  n=3
	task.changeLNBits(15, 3)
	#  (100) 1100100 => 1111011  n = 5
	task.changeLNBits(100, 5)
	#  (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	#  (-10) 11110110 => 11110001  (n = 3)
	task.changeLNBits(-10, 3)
end

main()

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
/*
  Scala Program for
  Toggle the last n bits
*/
class ChangeBits
{
	// Change last n bits of a given number
	def changeLNBits(num: Int, n: Int): Unit = {
		// Display given number
		print("\n Number : " + num);
		// Change last n bit
		var result: Int = num ^ (1 << n) - 1;
		// Display calculated result
		print("\n After change last " + n + " bits : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ChangeBits = new ChangeBits();
		// (15) 1111 => 1000  n=3
		task.changeLNBits(15, 3);
		// (100) 1100100 => 1111011  n = 5
		task.changeLNBits(100, 5);
		// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
		// (-10) 11110110 => 11110001  (n = 3)
		task.changeLNBits(-10, 3);
	}
}

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15
/*
  Swift 4 Program for
  Toggle the last n bits
*/
class ChangeBits
{
	// Change last n bits of a given number
	func changeLNBits(_ num: Int, _ n: Int)
	{
		// Display given number
		print("\n Number : ", num, terminator: "");
		// Change last n bit
		let result: Int = num ^ ((1 << n) - 1);
		// Display calculated result
		print("\n After change last ", n ," bits : ", result );
	}
}
func main()
{
	let task: ChangeBits = ChangeBits();
	// (15) 1111 => 1000  n=3
	task.changeLNBits(15, 3);
	// (100) 1100100 => 1111011  n = 5
	task.changeLNBits(100, 5);
	// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	// (-10) 11110110 => 11110001 (n = 3)  
	task.changeLNBits(-10, 3);
}
main();

Output

 Number :  15
 After change last  3  bits :  8

 Number :  100
 After change last  5  bits :  123

 Number :  -10
 After change last  3  bits :  -15
/*
  Kotlin Program for
  Toggle the last n bits
*/
class ChangeBits
{
	// Change last n bits of a given number
	fun changeLNBits(num: Int, n: Int): Unit
	{
		// Display given number
		print("\n Number : " + num);
		// Change last n bit
		var result: Int = num xor (1 shl n) - 1;
		// Display calculated result
		print("\n After change last " + n + " bits : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: ChangeBits = ChangeBits();
	// (15) 1111 => 1000  n=3
	task.changeLNBits(15, 3);
	// (100) 1100100 => 1111011  n = 5
	task.changeLNBits(100, 5);
	// (10) 00001010  => 11110101 (1s) => 11110110 (2s)
	// (-10) 11110110 => 11110001 (n = 3)
	task.changeLNBits(-10, 3);
}

Output

 Number : 15
 After change last 3 bits : 8

 Number : 100
 After change last 5 bits : 123

 Number : -10
 After change last 3 bits : -15


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