Scala - Program to swap two numbers using bitwise XOR operator Code Example

The code for Program to swap two numbers using bitwise XOR operator

// Scala program to swap two number 
// using bitwise XOR operator

object Sample{  
  def main(args:Array[String]){  
    var num1:Int = 10;
    var num2:Int = 20;
    
    println("Numbers before swapping:")
    printf("\tNum1:%d\n",num1)
    printf("\tNum2:%d\n",num2)
    
    num1 = num1 ^ num2
    num2 = num1 ^ num2
    num1 = num1 ^ num2
    
    println("Numbers after swapping:")
    printf("\tNum1:%d\n",num1)
    printf("\tNum2:%d\n",num2)
  }  
}


/*
Output:
Numbers before swapping:
	Num1:10
	Num2:20
Numbers after swapping:
	Num1:20
	Num2:10
*/
Code by IncludeHelp, on August 12, 2022 23:03

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.