Generate largest number with given x active and y inactive bits

Here given code implementation process.

// C Program
// Generate largest number with given x active and y inactive bits
#include <stdio.h>

// Constructs a large number using given active and inactive bits
void largestNum(int x, int y)
{
	if (x < 0 || y < 0 || (x + y) > 31)
	{
		// x and y not in valid range
		return;
	}
	// Calculate large number
	// Here ((1<<(x+y))-1)  this is generates all x+y active bits
	// and ((1<<y)-1) this is generates last y active bits
	// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
	int result = ((1 << (x + y)) - 1) ^ ((1 << y) - 1);
	// Display given bits
	printf("  Given x : %d, y : %d", x, y);
	// Display calculated result
	printf("\n Largest Number is : %d\n", result);
}
int main()
{
	// x = 5, y = 2 => 1111100 (124)
	largestNum(5, 2);
	// x = 1, y = 2 => 100 (4)
	largestNum(1, 2);
	// x = 4, y = 4 => 11110000 (240)
	largestNum(4, 4);
	return 0;
}

Output

  Given x : 5, y : 2
 Largest Number is : 124
  Given x : 1, y : 2
 Largest Number is : 4
  Given x : 4, y : 4
 Largest Number is : 240
/*
   Java Program
   Generate largest number with given x active and y inactive bits
*/
public class Maximize
{
	// Constructs a large number using given active and inactive bits
	public void largestNum(int x, int y)
	{
		if (x < 0 || y < 0 || (x + y) > 31)
		{
			// x and y not in valid range
			return;
		}
		// Calculate large number
		// Here ((1<<(x+y))-1)  this is generates all x+y active bits
		// and ((1<<y)-1) this is generates last y active bits
		// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		int result = ((1 << (x + y)) - 1) ^ ((1 << y) - 1);
		// Display given bits
		System.out.print(" Given x : " + x + ", y : " + y);
		// Display calculated result
		System.out.print("\n Largest Number is : " + result + "\n");
	}
	public static void main(String[] args)
	{
		Maximize task = new Maximize();
		// x = 5, y = 2 => 1111100 (124)
		task.largestNum(5, 2);
		// x = 1, y = 2 => 100 (4)
		task.largestNum(1, 2);
		// x = 4, y = 4 => 11110000 (240)
		task.largestNum(4, 4);
	}
}

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240
// Include header file
#include <iostream>
using namespace std;

/*
   C++ Program
   Generate largest number with given x active and y inactive bits
*/

class Maximize
{
	public:
		// Constructs a large number using given active and inactive bits
		void largestNum(int x, int y)
		{
			// x and y not in valid range
			if (x < 0 || y < 0 || (x + y) > 31)
			{
				return;
			}
			// Calculate large number
			// Here ((1<<x+y))-1)  this is generates all x+y active bits
			// and ((1<<y)-1) this is generates last y active bits
			// ((1<<x+y))-1) ^ ((1<<y)-1) change last y bits
			int result = ((1 << (x + y)) - 1) ^ ((1 << y) - 1);
			// Display given bits
			cout << " Given x : " << x << ", y : " << y;
			// Display calculated result
			cout << "\n Largest Number is : " << result << "\n";
		}
};
int main()
{
	Maximize task = Maximize();
	// x = 5, y = 2 => 1111100 (124)
	task.largestNum(5, 2);
	// x = 1, y = 2 => 100 (4)
	task.largestNum(1, 2);
	// x = 4, y = 4 => 11110000 (240)
	task.largestNum(4, 4);
	return 0;
}

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240
// Include namespace system
using System;
/*
   C# Program
   Generate largest number with given x active and y inactive bits
*/
public class Maximize
{
	// Constructs a large number using given active and inactive bits
	public void largestNum(int x, int y)
	{
		// x and y not in valid range
		if (x < 0 || y < 0 || (x + y) > 31)
		{
			return;
		}
		// Calculate large number
		// Here ((1<<(x+y))-1)  this is generates all x+y active bits
		// and ((1<<y)-1) this is generates last y active bits
		// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		int result = ((1 << (x + y)) - 1) ^ ((1 << y) - 1);
		// Display given bits
		Console.Write(" Given x : " + x + ", y : " + y);
		// Display calculated result
		Console.Write("\n Largest Number is : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		Maximize task = new Maximize();
		// x = 5, y = 2 => 1111100 (124)
		task.largestNum(5, 2);
		// x = 1, y = 2 => 100 (4)
		task.largestNum(1, 2);
		// x = 4, y = 4 => 11110000 (240)
		task.largestNum(4, 4);
	}
}

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240
<?php
/*
   Php Program
   Generate largest number with given x active and y inactive bits
*/
class Maximize
{
	// Constructs a large number using given active and inactive bits
	public	function largestNum($x, $y)
	{
		// x and y not in valid range
		if ($x < 0 || $y < 0 || ($x + $y) > 31)
		{
			return;
		}
		// Calculate large number
		// Here ((1<<(x+y))-1)  this is generates all x+y active bits
		// and ((1<<y)-1) this is generates last y active bits
		// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		$result = ((1 << ($x + $y)) - 1) ^ ((1 << $y) - 1);
		// Display given bits
		echo " Given x : ". $x .", y : ". $y;
		// Display calculated result
		echo "\n Largest Number is : ". $result ."\n";
	}
}

function main()
{
	$task = new Maximize();
	// x = 5, y = 2 => 1111100 (124)
	$task->largestNum(5, 2);
	// x = 1, y = 2 => 100 (4)
	$task->largestNum(1, 2);
	// x = 4, y = 4 => 11110000 (240)
	$task->largestNum(4, 4);
}
main();

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240
/*
   Node Js Program
   Generate largest number with given x active and y inactive bits
*/
class Maximize
{
	// Constructs a large number using given active and inactive bits
	largestNum(x, y)
	{
		// x and y not in valid range
		if (x < 0 || y < 0 || (x + y) > 31)
		{
			return;
		}
		// Calculate large number
		// Here ((1<<(x+y))-1)  this is generates all x+y active bits
		// and ((1<<y)-1) this is generates last y active bits
		// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		var result = ((1 << (x + y)) - 1) ^ ((1 << y) - 1);
		// Display given bits
		process.stdout.write(" Given x : " + x + ", y : " + y);
		// Display calculated result
		process.stdout.write("\n Largest Number is : " + result + "\n");
	}
}

function main()
{
	var task = new Maximize();
	// x = 5, y = 2 => 1111100 (124)
	task.largestNum(5, 2);
	// x = 1, y = 2 => 100 (4)
	task.largestNum(1, 2);
	// x = 4, y = 4 => 11110000 (240)
	task.largestNum(4, 4);
}
main();

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240
#    Python 3 Program
#    Generate largest number with given x active and y inactive bits

class Maximize :
	#  Constructs a large number using given active and inactive bits
	def largestNum(self, x, y) :
		if (x < 0 or y < 0 or(x + y) > 31) :
			#  x and y not in valid range
			return
		
		#  Calculate large number
		#  Here ((1<<(x+y))-1)  this is generates all x+y active bits
		#  and ((1<<y)-1) this is generates last y active bits
		#  ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		result = ((1 << (x + y)) - 1) ^ ((1 << y) - 1)
		#  Display given bits
		print(" Given x : ", x ,", y : ", y, end = "")
		#  Display calculated result
		print("\n Largest Number is : ", result )
	

def main() :
	task = Maximize()
	#  x = 5, y = 2 => 1111100 (124)
	task.largestNum(5, 2)
	#  x = 1, y = 2 => 100 (4)
	task.largestNum(1, 2)
	#  x = 4, y = 4 => 11110000 (240)
	task.largestNum(4, 4)

if __name__ == "__main__": main()

Output

 Given x :  5 , y :  2
 Largest Number is :  124
 Given x :  1 , y :  2
 Largest Number is :  4
 Given x :  4 , y :  4
 Largest Number is :  240
#    Ruby Program
#    Generate largest number with given x active and y inactive bits

class Maximize 
	#  Constructs a large number using given active and inactive bits
	def largestNum(x, y) 
		if (x < 0 || y < 0 || (x + y) > 31) 
			#  x and y not in valid range
			return
		end

		#  Calculate large number
		#  Here ((1<<(x+y))-1)  this is generates all x+y active bits
		#  and ((1<<y)-1) this is generates last y active bits
		#  ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		result = ((1 << (x + y)) - 1) ^ ((1 << y) - 1)
		#  Display given bits
		print(" Given x : ", x ,", y : ", y)
		#  Display calculated result
		print("\n Largest Number is : ", result ,"\n")
	end

end

def main() 
	task = Maximize.new()
	#  x = 5, y = 2 => 1111100 (124)
	task.largestNum(5, 2)
	#  x = 1, y = 2 => 100 (4)
	task.largestNum(1, 2)
	#  x = 4, y = 4 => 11110000 (240)
	task.largestNum(4, 4)
end

main()

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240
/*
   Scala Program
   Generate largest number with given x active and y inactive bits
*/
class Maximize
{
	// Constructs a large number using given active and inactive bits
	def largestNum(x: Int, y: Int): Unit = {
		// x and y not in valid range
		if (x < 0 || y < 0 || (x + y) > 31)
		{
			return;
		}
		// Calculate large number
		// Here ((1<<(x+y))-1)  this is generates all x+y active bits
		// and ((1<<y)-1) this is generates last y active bits
		// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		var result: Int = ((1 << (x + y)) - 1) ^ ((1 << y) - 1);
		// Display given bits
		print(" Given x : " + x + ", y : " + y);
		// Display calculated result
		print("\n Largest Number is : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Maximize = new Maximize();
		// x = 5, y = 2 => 1111100 (124)
		task.largestNum(5, 2);
		// x = 1, y = 2 => 100 (4)
		task.largestNum(1, 2);
		// x = 4, y = 4 => 11110000 (240)
		task.largestNum(4, 4);
	}
}

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240
/*
   Swift 4 Program
   Generate largest number with given x active and y inactive bits
*/
class Maximize
{
	// Constructs a large number using given active and inactive bits
	func largestNum(_ x: Int, _ y: Int)
	{
		// x and y not in valid range
		if (x < 0 || y < 0 || (x + y) > 31)
		{
			return;
		}
		// Calculate large number
		// Here ((1<<(x+y))-1)  this is generates all x+y active bits
		// and ((1<<y)-1) this is generates last y active bits
		// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		let result: Int = ((1 << (x + y)) - 1) ^ ((1 << y) - 1);
		// Display given bits
		print(" Given x : ", x ,", y : ", y, terminator: "");
		// Display calculated result
		print("\n Largest Number is : ", result );
	}
}
func main()
{
	let task: Maximize = Maximize();
	// x = 5, y = 2 => 1111100 (124)
	task.largestNum(5, 2);
	// x = 1, y = 2 => 100 (4)
	task.largestNum(1, 2);
	// x = 4, y = 4 => 11110000 (240)
	task.largestNum(4, 4);
}
main();

Output

 Given x :  5 , y :  2
 Largest Number is :  124
 Given x :  1 , y :  2
 Largest Number is :  4
 Given x :  4 , y :  4
 Largest Number is :  240
/*
   Kotlin Program
   Generate largest number with given x active and y inactive bits
*/
class Maximize
{
	// Constructs a large number using given active and inactive bits
	fun largestNum(x: Int, y: Int): Unit
	{
		// x and y not in valid range
		if (x < 0 || y < 0 || (x + y) > 31)
		{
			return;
		}
		// Calculate large number
		// Here ((1<<(x+y))-1)  this is generates all x+y active bits
		// and ((1<<y)-1) this is generates last y active bits
		// ((1<<(x+y))-1) ^ ((1<<y)-1) change last y bits
		var result: Int = ((1 shl(x + y)) - 1) xor((1 shl y) - 1);
		// Display given bits
		print(" Given x : " + x + ", y : " + y);
		// Display calculated result
		print("\n Largest Number is : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Maximize = Maximize();
	// x = 5, y = 2 => 1111100 (124)
	task.largestNum(5, 2);
	// x = 1, y = 2 => 100 (4)
	task.largestNum(1, 2);
	// x = 4, y = 4 => 11110000 (240)
	task.largestNum(4, 4);
}

Output

 Given x : 5, y : 2
 Largest Number is : 124
 Given x : 1, y : 2
 Largest Number is : 4
 Given x : 4, y : 4
 Largest Number is : 240

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