Skip to main content

Change first and last bits of a number

Here given code implementation process.

// C Program 
// Change first and last bits of a number
#include <stdio.h>

// Active all the bits of present in a number
int activeAllBits(int num)
{
	int n = num;
	n = n | n >> 1;
	n = n | n >> 2;
	n = n | n >> 4;
	n = n | n >> 8;
	n = n | n >> 16;
	return n;
}
// Change the first and last bits of given number
void changeFirstLast(int num)
{
	int n = 0;
	if (num > 1)
	{
		n = activeAllBits(num);
		// Change first and last bits
		n = num ^ ((n + 1) >> 1) + 1;
	}
	// Display calculated result
	printf(" Number : %d", num);
	printf("\n After Active : %d\n", n);
}
int main()
{
	// (16) 10000 => 00001 (1)
	changeFirstLast(16);
	// (45) 101101 => 001100 (12) 
	changeFirstLast(45);
	// (100) 1100100 => (37)   0100101
	changeFirstLast(100);
	return 0;
}

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
/*
  Java Program for
  Change first and last bits of a number
*/
public class SwitchBits
{
	// Active all the bits of present in a number
	public int activeAllBits(int num)
	{
		int n = num;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Change the first and last bits of given number
	public void changeFirstLast(int num)
	{
		int n = 0;
		if (num > 1)
		{
			n = activeAllBits(num);
			// Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1;
		}
		// Display calculated result
		System.out.print(" Number : " + num);
		System.out.print("\n After Active : " + n + "\n");
	}
	public static void main(String[] args)
	{
		SwitchBits task = new SwitchBits();
		// (16) 10000 => 00001 (1)
		task.changeFirstLast(16);
		// (45) 101101 => 001100 (12) 
		task.changeFirstLast(45);
		// (100) 1100100 => (37)   0100101
		task.changeFirstLast(100);
	}
}

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Change first and last bits of a number
*/
class SwitchBits
{
	public:
		// Active all the bits of present in a number
		int activeAllBits(int num)
		{
			int n = num;
			n = n | n >> 1;
			n = n | n >> 2;
			n = n | n >> 4;
			n = n | n >> 8;
			n = n | n >> 16;
			return n;
		}
	// Change the first and last bits of given number
	void changeFirstLast(int num)
	{
		int n = 0;
		if (num > 1)
		{
			n = this->activeAllBits(num);
			// Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1;
		}
		// Display calculated result
		cout << " Number : " << num;
		cout << "\n After Active : " << n << "\n";
	}
};
int main()
{
	SwitchBits task = SwitchBits();
	// (16) 10000 => 00001 (1)
	task.changeFirstLast(16);
	// (45) 101101 => 001100 (12)
	task.changeFirstLast(45);
	// (100) 1100100 => (37)   0100101
	task.changeFirstLast(100);
	return 0;
}

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
// Include namespace system
using System;
/*
  C# Program for
  Change first and last bits of a number
*/
public class SwitchBits
{
	// Active all the bits of present in a number
	public int activeAllBits(int num)
	{
		int n = num;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Change the first and last bits of given number
	public void changeFirstLast(int num)
	{
		int n = 0;
		if (num > 1)
		{
			n = activeAllBits(num);
			// Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1;
		}
		// Display calculated result
		Console.Write(" Number : " + num);
		Console.Write("\n After Active : " + n + "\n");
	}
	public static void Main(String[] args)
	{
		SwitchBits task = new SwitchBits();
		// (16) 10000 => 00001 (1)
		task.changeFirstLast(16);
		// (45) 101101 => 001100 (12)
		task.changeFirstLast(45);
		// (100) 1100100 => (37)   0100101
		task.changeFirstLast(100);
	}
}

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
<?php
/*
  Php Program for
  Change first and last bits of a number
*/
class SwitchBits
{
	// Active all the bits of present in a number
	public	function activeAllBits($num)
	{
		$n = $num;
		$n = $n | $n >> 1;
		$n = $n | $n >> 2;
		$n = $n | $n >> 4;
		$n = $n | $n >> 8;
		$n = $n | $n >> 16;
		return $n;
	}
	// Change the first and last bits of given number
	public	function changeFirstLast($num)
	{
		$n = 0;
		if ($num > 1)
		{
			$n = $this->activeAllBits($num);
			// Change first and last bits
			$n = $num ^ (($n + 1) >> 1) + 1;
		}
		// Display calculated result
		echo " Number : ". $num;
		echo "\n After Active : ". $n ."\n";
	}
}

function main()
{
	$task = new SwitchBits();
	// (16) 10000 => 00001 (1)
	$task->changeFirstLast(16);
	// (45) 101101 => 001100 (12)
	$task->changeFirstLast(45);
	// (100) 1100100 => (37)   0100101
	$task->changeFirstLast(100);
}
main();

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
/*
  Node Js Program for
  Change first and last bits of a number
*/
class SwitchBits
{
	// Active all the bits of present in a number
	activeAllBits(num)
	{
		var n = num;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Change the first and last bits of given number
	changeFirstLast(num)
	{
		var n = 0;
		if (num > 1)
		{
			n = this.activeAllBits(num);
			// Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1;
		}
		// Display calculated result
		process.stdout.write(" Number : " + num);
		process.stdout.write("\n After Active : " + n + "\n");
	}
}

function main()
{
	var task = new SwitchBits();
	// (16) 10000 => 00001 (1)
	task.changeFirstLast(16);
	// (45) 101101 => 001100 (12)
	task.changeFirstLast(45);
	// (100) 1100100 => (37)   0100101
	task.changeFirstLast(100);
}
main();

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
#   Python 3 Program for
#   Change first and last bits of a number

class SwitchBits :
	#  Active all the bits of present in a number
	def activeAllBits(self, num) :
		n = num
		n = n | n >> 1
		n = n | n >> 2
		n = n | n >> 4
		n = n | n >> 8
		n = n | n >> 16
		return n
	
	#  Change the first and last bits of given number
	def changeFirstLast(self, num) :
		n = 0
		if (num > 1) :
			n = self.activeAllBits(num)
			#  Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1
		
		#  Display calculated result
		print(" Number : ", num, end = "")
		print("\n After Active : ", n )
	

def main() :
	task = SwitchBits()
	#  (16) 10000 => 00001 (1)
	task.changeFirstLast(16)
	#  (45) 101101 => 001100 (12) 
	task.changeFirstLast(45)
	#  (100) 1100100 => (37)   0100101
	task.changeFirstLast(100)

if __name__ == "__main__": main()

Output

 Number :  16
 After Active :  1
 Number :  45
 After Active :  12
 Number :  100
 After Active :  37
#   Ruby Program for
#   Change first and last bits of a number

class SwitchBits 
	#  Active all the bits of present in a number
	def activeAllBits(num) 
		n = num
		n = n | n >> 1
		n = n | n >> 2
		n = n | n >> 4
		n = n | n >> 8
		n = n | n >> 16
		return n
	end

	#  Change the first and last bits of given number
	def changeFirstLast(num) 
		n = 0
		if (num > 1) 
			n = self.activeAllBits(num)
			#  Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1
		end

		#  Display calculated result
		print(" Number : ", num)
		print("\n After Active : ", n ,"\n")
	end

end

def main() 
	task = SwitchBits.new()
	#  (16) 10000 => 00001 (1)
	task.changeFirstLast(16)
	#  (45) 101101 => 001100 (12) 
	task.changeFirstLast(45)
	#  (100) 1100100 => (37)   0100101
	task.changeFirstLast(100)
end

main()

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
/*
  Scala Program for
  Change first and last bits of a number
*/
class SwitchBits
{
	// Active all the bits of present in a number
	def activeAllBits(num: Int): Int = {
		var n: Int = num;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Change the first and last bits of given number
	def changeFirstLast(num: Int): Unit = {
		var n: Int = 0;
		if (num > 1)
		{
			n = this.activeAllBits(num);
			// Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1;
		}
		// Display calculated result
		print(" Number : " + num);
		print("\n After Active : " + n + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SwitchBits = new SwitchBits();
		// (16) 10000 => 00001 (1)
		task.changeFirstLast(16);
		// (45) 101101 => 001100 (12)
		task.changeFirstLast(45);
		// (100) 1100100 => (37)   0100101
		task.changeFirstLast(100);
	}
}

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37
/*
  Swift 4 Program for
  Change first and last bits of a number
*/
class SwitchBits
{
	// Active all the bits of present in a number
	func activeAllBits(_ num: Int)->Int
	{
		var n: Int = num;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Change the first and last bits of given number
	func changeFirstLast(_ num: Int)
	{
		var n: Int = 0;
		if (num > 1)
		{
			n = self.activeAllBits(num);
			// Change first and last bits
			n = num ^ ((n + 1) >> 1) + 1;
		}
		// Display calculated result
		print(" Number : ", num, terminator: "");
		print("\n After Active : ", n );
	}
}
func main()
{
	let task: SwitchBits = SwitchBits();
	// (16) 10000 => 00001 (1)
	task.changeFirstLast(16);
	// (45) 101101 => 001100 (12)
	task.changeFirstLast(45);
	// (100) 1100100 => (37)   0100101
	task.changeFirstLast(100);
}
main();

Output

 Number :  16
 After Active :  1
 Number :  45
 After Active :  14
 Number :  100
 After Active :  37
/*
  Kotlin Program for
  Change first and last bits of a number
*/
class SwitchBits
{
	// Active all the bits of present in a number
	fun activeAllBits(num: Int): Int
	{
		var n: Int = num;
		n = n or (n shr 1);
        n = n or (n shr 2);
		n = n or (n shr 4);
		n = n or (n shr 8);
		n = n or (n shr 16);
		return n;
	}
	// Change the first and last bits of given number
	fun changeFirstLast(num: Int): Unit
	{
		var n: Int = 0;
		if (num > 1)
		{
			n = this.activeAllBits(num);
			// Change first and last bits
			n = num xor((n + 1) shr 1) + 1;
		}
		// Display calculated result
		print(" Number : " + num);
		print("\n After Active : " + n + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: SwitchBits = SwitchBits();
	// (16) 10000 => 00001 (1)
	task.changeFirstLast(16);
	// (45) 101101 => 001100 (12)
	task.changeFirstLast(45);
	// (100) 1100100 => (37)   0100101
	task.changeFirstLast(100);
}

Output

 Number : 16
 After Active : 1
 Number : 45
 After Active : 12
 Number : 100
 After Active : 37




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