Change all bits after most significant bit

Here given code implementation process.

// C Program 
// Change all bits after most significant bit
#include <stdio.h>

// Change bits of given numbers
void changeBit(int num)
{
    int n = num;
    int start = 1;

    while(n >= start)
    {
        // Change bits
        n = start ^ n;

        // shift bits to left by one bit
        start = start << 1;
    }
    // Display calculated result
    printf("\n Number : %d",num);
    printf("\n After Change Bits : %d",n);


}
int main()
{
    // 1000 => 0111
    changeBit(16);

    // 1111011 => 0000100
    changeBit(123);
    
    return 0;
}

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
/*
   Java Program for
   Change all bits after most significant bit
*/
class SwitchBit
{
	// Change bits of given numbers
	public void changeBit(int num)
	{
		int n = num;
		int start = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// shift bits to left by one bit
			start = start << 1;
		}
		// Display calculated result
		System.out.print("\n Number : " + num);
		System.out.print("\n After Change Bits : " + n);
	}
	public static void main(String[] args)
	{
		SwitchBit task = new SwitchBit();
		// 1000 => 0111
		task.changeBit(16);
		// 1111011 => 0000100
		task.changeBit(123);
	}
}

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
// Include header file
#include <iostream>
using namespace std;

/*
   C++ Program for
   Change all bits after most significant bit
*/

class SwitchBit
{
	public:
		// Change bits of given numbers
		void changeBit(int num)
		{
			int n = num;
			int start = 1;
			while (n >= start)
			{
				// Change bits
				n = start ^ n;
				// shift bits to left by one bit
				start = start << 1;
			}
			// Display calculated result
			cout << "\n Number : " << num;
			cout << "\n After Change Bits : " << n;
		}
};
int main()
{
	SwitchBit task = SwitchBit();
	// 1000 => 0111
	task.changeBit(16);
	// 1111011 => 0000100
	task.changeBit(123);
	return 0;
}

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
// Include namespace system
using System;
/*
   C# Program for
   Change all bits after most significant bit
*/
public class SwitchBit
{
	// Change bits of given numbers
	public void changeBit(int num)
	{
		int n = num;
		int start = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// shift bits to left by one bit
			start = start << 1;
		}
		// Display calculated result
		Console.Write("\n Number : " + num);
		Console.Write("\n After Change Bits : " + n);
	}
	public static void Main(String[] args)
	{
		SwitchBit task = new SwitchBit();
		// 1000 => 0111
		task.changeBit(16);
		// 1111011 => 0000100
		task.changeBit(123);
	}
}

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
<?php
/*
   Php Program for
   Change all bits after most significant bit
*/
class SwitchBit
{
	// Change bits of given numbers
	public	function changeBit($num)
	{
		$n = $num;
		$start = 1;
		while ($n >= $start)
		{
			// Change bits
			$n = $start ^ $n;
			// shift bits to left by one bit
			$start = $start << 1;
		}
		// Display calculated result
		echo "\n Number : ". $num;
		echo "\n After Change Bits : ". $n;
	}
}

function main()
{
	$task = new SwitchBit();
	// 1000 => 0111
	$task->changeBit(16);
	// 1111011 => 0000100
	$task->changeBit(123);
}
main();

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
/*
   Node Js Program for
   Change all bits after most significant bit
*/
class SwitchBit
{
	// Change bits of given numbers
	changeBit(num)
	{
		var n = num;
		var start = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// shift bits to left by one bit
			start = start << 1;
		}
		// Display calculated result
		process.stdout.write("\n Number : " + num);
		process.stdout.write("\n After Change Bits : " + n);
	}
}

function main()
{
	var task = new SwitchBit();
	// 1000 => 0111
	task.changeBit(16);
	// 1111011 => 0000100
	task.changeBit(123);
}
main();

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
#  Python 3 Program for
#  Change all bits after most significant bit

class SwitchBit :
	#  Change bits of given numbers
	def changeBit(self, num) :
		n = num
		start = 1
		while (n >= start) :
			#  Change bits
			n = start ^ n
			#  shift bits to left by one bit
			start = start << 1
		
		#  Display calculated result
		print("\n Number : ", num, end = "")
		print("\n After Change Bits : ", n, end = "")
	

def main() :
	task = SwitchBit()
	#  1000 => 0111
	task.changeBit(16)
	#  1111011 => 0000100
	task.changeBit(123)

if __name__ == "__main__": main()

Output

 Number :  16
 After Change Bits :  15
 Number :  123
 After Change Bits :  4
#    Ruby Program for
#    Change all bits after most significant bit

class SwitchBit 
	#  Change bits of given numbers
	def changeBit(num) 
		n = num
		start = 1
		while (n >= start) 
			#  Change bits
			n = start ^ n
			#  Shift bits to left by one bit
			start = start << 1
		end

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

end

def main() 
	task = SwitchBit.new()
	#  1000 => 0111
	task.changeBit(16)
	#  1111011 => 0000100
	task.changeBit(123)
end

main()

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
/*
   Scala Program for
   Change all bits after most significant bit
*/
class SwitchBit
{
	// Change bits of given numbers
	def changeBit(num: Int): Unit = {
		var n: Int = num;
		var start: Int = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// Shift bits to left by one bit
			start = start << 1;
		}
		// Display calculated result
		print("\n Number : " + num);
		print("\n After Change Bits : " + n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SwitchBit = new SwitchBit();
		// 1000 => 0111
		task.changeBit(16);
		// 1111011 => 0000100
		task.changeBit(123);
	}
}

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4
/*
   Swift 4 Program for
   Change all bits after most significant bit
*/
class SwitchBit
{
	// Change bits of given numbers
	func changeBit(_ num: Int)
	{
		var n: Int = num;
		var start: Int = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// Shift bits to left by one bit
			start = start << 1;
		}
		// Display calculated result
		print("\n Number : ", num, terminator: "");
		print("\n After Change Bits : ", n, terminator: "");
	}
}
func main()
{
	let task: SwitchBit = SwitchBit();
	// 1000 => 0111
	task.changeBit(16);
	// 1111011 => 0000100
	task.changeBit(123);
}
main();

Output

 Number :  16
 After Change Bits :  15
 Number :  123
 After Change Bits :  4
/*
   Kotlin Program for
   Change all bits after most significant bit
*/
class SwitchBit
{
	// Change bits of given numbers
	fun changeBit(num: Int): Unit
	{
		var n: Int = num;
		var start: Int = 1;
		while (n >= start)
		{
			// Change bits
			n = start xor n;
			// Shift bits to left by one bit
			start = start shl 1;
		}
		// Display calculated result
		print("\n Number : " + num);
		print("\n After Change Bits : " + n);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: SwitchBit = SwitchBit();
	// 1000 => 0111
	task.changeBit(16);
	// 1111011 => 0000100
	task.changeBit(123);
}

Output

 Number : 16
 After Change Bits : 15
 Number : 123
 After Change Bits : 4


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