# Subtract two numbers using bitwise operators

Here given code implementation process.

``````// C Program
// Subtract two numbers using bitwise operators
#include <stdio.h>

// Perform subtract of two numbers
void subtractNum(int a, int b)
{
// Display given number
printf("\n Number a : %d", a);
printf("\n Number b : %d", b);
printf("\n Subtract ((%d)-(%d))", a, b);
int temp = 0;
// Subtract of two numbers
while (b != 0)
{
temp = ((~a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
printf("\n Output :  %d\n", a);
}
int main(int argc, char const *argv[])
{
// Test Cases
subtractNum(12, -31);
subtractNum(3, 4);
subtractNum(-1, -4);
subtractNum(12, 4);
return 0;
}``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output :  43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output :  -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output :  3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output :  8``````
``````/*
Java Program
Subtract two numbers using bitwise operators
*/
public class Subtraction
{
// Perform subtract of two numbers
public void subtractNum(int a, int b)
{
// Display given number
System.out.print("\n Number a : " + a);
System.out.print("\n Number b : " + b);
System.out.print("\n Subtract ((" + a + ")-(" + b + "))");
int temp = 0;
// Subtract of two numbers
while (b != 0)
{
temp = ((~a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
System.out.print("\n Output : " + a + "\n");
}
public static void main(String[] args)
{
Subtraction task = new Subtraction();
// Test Cases
task.subtractNum(12, -31);
task.subtractNum(3, 4);
task.subtractNum(-1, -4);
task.subtractNum(12, 4);
}
}``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Subtract two numbers using bitwise operators
*/

class Subtraction
{
public:
// Perform subtract of two numbers
void subtractNum(int a, int b)
{
// Display given number
cout << "\n Number a : " << a;
cout << "\n Number b : " << b;
cout << "\n Subtract ((" << a << ")-(" << b << "))";
int temp = 0;
// Subtract of two numbers
while (b != 0)
{
temp = ((~a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
cout << "\n Output : " << a << "\n";
}
};
int main()
{
Subtraction task = Subtraction();
// Test Cases
task.subtractNum(12, -31);
task.subtractNum(3, 4);
task.subtractNum(-1, -4);
task.subtractNum(12, 4);
return 0;
}``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````
``````// Include namespace system
using System;
/*
C# Program
Subtract two numbers using bitwise operators
*/
public class Subtraction
{
// Perform subtract of two numbers
public void subtractNum(int a, int b)
{
// Display given number
Console.Write("\n Number a : " + a);
Console.Write("\n Number b : " + b);
Console.Write("\n Subtract ((" + a + ")-(" + b + "))");
int temp = 0;
// Subtract of two numbers
while (b != 0)
{
temp = ((~a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
Console.Write("\n Output : " + a + "\n");
}
public static void Main(String[] args)
{
Subtraction task = new Subtraction();
// Test Cases
task.subtractNum(12, -31);
task.subtractNum(3, 4);
task.subtractNum(-1, -4);
task.subtractNum(12, 4);
}
}``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````
``````<?php
/*
Php Program
Subtract two numbers using bitwise operators
*/
class Subtraction
{
// Perform subtract of two numbers
public	function subtractNum(\$a, \$b)
{
// Display given number
echo "\n Number a : ". \$a;
echo "\n Number b : ". \$b;
echo "\n Subtract ((". \$a .")-(". \$b ."))";
\$temp = 0;
// Subtract of two numbers
while (\$b != 0)
{
\$temp = ((~\$a) & \$b) << 1;
\$a = \$a ^ \$b;
\$b = \$temp;
}
// Display calculated result
echo "\n Output : ". \$a ."\n";
}
}

function main()
{
\$task = new Subtraction();
// Test Cases
\$task->subtractNum(12, -31);
\$task->subtractNum(3, 4);
\$task->subtractNum(-1, -4);
\$task->subtractNum(12, 4);
}
main();``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````
``````/*
Node Js Program
Subtract two numbers using bitwise operators
*/
class Subtraction
{
// Perform subtract of two numbers
subtractNum(a, b)
{
// Display given number
process.stdout.write("\n Number a : " + a);
process.stdout.write("\n Number b : " + b);
process.stdout.write("\n Subtract ((" + a + ")-(" + b + "))");
var temp = 0;
// Subtract of two numbers
while (b != 0)
{
temp = ((~a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
process.stdout.write("\n Output : " + a + "\n");
}
}

function main()
{
var task = new Subtraction();
// Test Cases
task.subtractNum(12, -31);
task.subtractNum(3, 4);
task.subtractNum(-1, -4);
task.subtractNum(12, 4);
}
main();``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````
``````/*
Scala Program
Subtract two numbers using bitwise operators
*/
class Subtraction
{
// Perform subtract of two numbers
def subtractNum(n1: Int, n2: Int): Unit = {
var a = n1;
var b = n2;
// Display given number
print("\n Number a : " + a);
print("\n Number b : " + b);
print("\n Subtract ((" + a + ")-(" + b + "))");
var temp: Int = 0;
// Subtract of two numbers
while (b != 0)
{
temp = ((~a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
print("\n Output : " + a + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Subtraction = new Subtraction();
// Test Cases
task.subtractNum(12, -31);
task.subtractNum(3, 4);
task.subtractNum(-1, -4);
task.subtractNum(12, 4);
}
}``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````
``````/*
Swift 4 Program
Subtract two numbers using bitwise operators
*/
class Subtraction
{
// Perform subtract of two numbers
func subtractNum(_ n1: Int, _ n2:  Int)
{
var a = n1;
var b = n2;
// Display given number
print("\n Number a : ", a, terminator: "");
print("\n Number b : ", b, terminator: "");
print("\n Subtract ((", a ,")-(", b ,"))", terminator: "");
var temp: Int = 0;
// Subtract of two numbers
while (b  != 0)
{
temp = ((~a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
print("\n Output : ", a );
}
}
func main()
{
let task: Subtraction = Subtraction();
// Test Cases
task.subtractNum(12, -31);
task.subtractNum(3, 4);
task.subtractNum(-1, -4);
task.subtractNum(12, 4);
}
main();``````

#### Output

`````` Number a :  12
Number b :  -31
Subtract (( 12 )-( -31 ))
Output :  43

Number a :  3
Number b :  4
Subtract (( 3 )-( 4 ))
Output :  -1

Number a :  -1
Number b :  -4
Subtract (( -1 )-( -4 ))
Output :  3

Number a :  12
Number b :  4
Subtract (( 12 )-( 4 ))
Output :  8``````
``````/*
Kotlin Program
Subtract two numbers using bitwise operators
*/
class Subtraction
{
// Perform subtract of two numbers
fun subtractNum(n1: Int, n2: Int): Unit
{
var a = n1;
var b = n2;
// Display given number
print("\n Number a : " + a);
print("\n Number b : " + b);
print("\n Subtract ((" + a + ")-(" + b + "))");
var temp: Int ;
// Subtract of two numbers
while (b != 0)
{
temp = ((a.inv()) and b) shl 1;
a = a xor b;
b = temp;
}
// Display calculated result
print("\n Output : " + a + "\n");
}
}
fun main(args: Array <String> ): Unit
{
var task: Subtraction = Subtraction();
// Test Cases
task.subtractNum(12, -31);
task.subtractNum(3, 4);
task.subtractNum(-1, -4);
task.subtractNum(12, 4);
}``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````
``````fn main()
{
// Test Cases
subtract_num(12, -31);
subtract_num(3, 4);
subtract_num(-1, -4);
subtract_num(12, 4);
}
fn subtract_num(n1: i32, n2: i32)
{
let mut a = n1;
let mut b = n2;
// Display given number
print!("\n Number a : {}", a);
print!("\n Number b : {}", b);
print!("\n Subtract (({})-({}))", a, b);
let mut temp: i32 ;
// Subtract of two numbers
while b != 0
{
temp = ((!a) & b) << 1;
a = a ^ b;
b = temp;
}
// Display calculated result
print!("\n Output : {}\n", a);
}``````

#### Output

`````` Number a : 12
Number b : -31
Subtract ((12)-(-31))
Output : 43

Number a : 3
Number b : 4
Subtract ((3)-(4))
Output : -1

Number a : -1
Number b : -4
Subtract ((-1)-(-4))
Output : 3

Number a : 12
Number b : 4
Subtract ((12)-(4))
Output : 8``````

## 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

© 2022, kalkicode.com, All rights reserved