Xor of two numbers without using XOR operator
Here given code implementation process.
// C Program
// Xor of two numbers without using XOR operator
#include <stdio.h>
// Perform xor operation of two integers
void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x) & y) | (x & (~y));
// Display calculated result
printf(" ((%d) ^ (%d)) : %d \n", x, y, result);
}
int main()
{
// Test cases
xorOperation(2, 5);
xorOperation(6, 4);
xorOperation(8, 3);
return 0;
}
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
/*
Java Program
Xor of two numbers without using XOR operator
*/
public class Operation
{
// Perform xor operation of two integers
public void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x ) & y) | ( x & (~y) ) ;
// Display calculated result
System.out.print(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
public static void main(String[] args)
{
Operation task = new Operation();
// Test cases
task.xorOperation(2,5);
task.xorOperation(6,4);
task.xorOperation(8,3);
}
}
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Xor of two numbers without using XOR operator
*/
class Operation
{
public:
// Perform xor operation of two integers
void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x) & y) | (x & (~y));
// Display calculated result
cout << " ((" << x << ") ^ (" << y << ")) : " << result << " \n";
}
};
int main()
{
Operation task = Operation();
// Test cases
task.xorOperation(2, 5);
task.xorOperation(6, 4);
task.xorOperation(8, 3);
return 0;
}
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
// Include namespace system
using System;
/*
C# Program
Xor of two numbers without using XOR operator
*/
public class Operation
{
// Perform xor operation of two integers
public void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x) & y) | (x & (~y));
// Display calculated result
Console.Write(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
public static void Main(String[] args)
{
Operation task = new Operation();
// Test cases
task.xorOperation(2, 5);
task.xorOperation(6, 4);
task.xorOperation(8, 3);
}
}
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
<?php
/*
Php Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
public function xorOperation($x, $y)
{
// Calculate XOR
$result = ((~$x) & $y) | ($x & (~$y));
// Display calculated result
echo " ((". $x .") ^ (". $y .")) : ". $result ." \n";
}
}
function main()
{
$task = new Operation();
// Test cases
$task->xorOperation(2, 5);
$task->xorOperation(6, 4);
$task->xorOperation(8, 3);
}
main();
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
/*
Node Js Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
xorOperation(x, y)
{
// Calculate XOR
var result = ((~x) & y) | (x & (~y));
// Display calculated result
process.stdout.write(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
}
function main()
{
var task = new Operation();
// Test cases
task.xorOperation(2, 5);
task.xorOperation(6, 4);
task.xorOperation(8, 3);
}
main();
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
# Python 3 Program
# Xor of two numbers without using XOR operator
class Operation :
# Perform xor operation of two integers
def xorOperation(self, x, y) :
# Calculate XOR
result = ((~x) & y) | (x & (~y))
# Display calculated result
print(" ((", x ,") ^ (", y ,")) : ", result )
def main() :
task = Operation()
# Test cases
task.xorOperation(2, 5)
task.xorOperation(6, 4)
task.xorOperation(8, 3)
if __name__ == "__main__": main()
Output
(( 2 ) ^ ( 5 )) : 7
(( 6 ) ^ ( 4 )) : 2
(( 8 ) ^ ( 3 )) : 11
# Ruby Program
# Xor of two numbers without using XOR operator
class Operation
# Perform xor operation of two integers
def xorOperation(x, y)
# Calculate XOR
result = ((~x) & y) | (x & (~y))
# Display calculated result
print(" ((", x ,") ^ (", y ,")) : ", result ," \n")
end
end
def main()
task = Operation.new()
# Test cases
task.xorOperation(2, 5)
task.xorOperation(6, 4)
task.xorOperation(8, 3)
end
main()
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
/*
Scala Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
def xorOperation(x: Int, y: Int): Unit = {
// Calculate XOR
var result: Int = ((~x) & y) | (x & (~y));
// Display calculated result
print(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Operation = new Operation();
// Test cases
task.xorOperation(2, 5);
task.xorOperation(6, 4);
task.xorOperation(8, 3);
}
}
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
/*
Swift 4 Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
func xorOperation(_ x: Int, _ y: Int)
{
// Calculate XOR
let result: Int = ((~x) & y) | (x & (~y));
// Display calculated result
print(" ((", x ,") ^ (", y ,")) : ", result ," ");
}
}
func main()
{
let task: Operation = Operation();
// Test cases
task.xorOperation(2, 5);
task.xorOperation(6, 4);
task.xorOperation(8, 3);
}
main();
Output
(( 2 ) ^ ( 5 )) : 7
(( 6 ) ^ ( 4 )) : 2
(( 8 ) ^ ( 3 )) : 11
/*
Kotlin Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
fun xorOperation(x: Int, y: Int): Unit
{
// Calculate XOR
var result: Int = ((x.inv()) and y) or(x and(y.inv()));
// Display calculated result
print(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
}
fun main(args: Array < String > ): Unit
{
var task: Operation = Operation();
// Test cases
task.xorOperation(2, 5);
task.xorOperation(6, 4);
task.xorOperation(8, 3);
}
Output
((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
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