Posted on by Kalkicode
Code Bit Logic

Turn off the rightmost set bit in scala

Scala program for Turn off the rightmost set bit. Here mentioned other language solution.

/*
  Scala program for
  Turn off the rightmost set bit
*/
class BitLogic()
{
	// Handles the request of unset the 
	// active rightmost bit of a number.
	def changeActiveRightMost(num: Int): Unit = {
		println("\n Number : " + num);
		// After unset rightmost bit
		println(" Result : " + (num & (num - 1)));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitLogic = new BitLogic();
		// Test A (num = 10)
		// 1010
		//   ↑   [Right most set bit]
		// --------------------------
		// 1000  After unset (8)
		task.changeActiveRightMost(10);
		// Test B (num = 15)
		// 1111
		//    ↑  [Right most set bit]
		// --------------------------
		// 1110  After unset (14)
		task.changeActiveRightMost(15);
		// Test C (num = 320) 
		// 101000000
		//   ↑   [Right most set bit]
		// --------------------------
		// 100000000  After unset (256)
		task.changeActiveRightMost(320);
	}
}

Output

 Number : 10
 Result : 8

 Number : 15
 Result : 14

 Number : 320
 Result : 256

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