给定两个数字a和b 。任务是使用2的补数方法从a中减去b 。
注意:负数表示为2的正数补码。
例如,-5可以二进制形式表示为2的补数5。请看下面的图片:
例子:
Input : a = 2, b = 3
Output : -1
Input : a = 9, b = 7
Output : 2
从a中减去b 。将表达式(ab)编写为:
(a - b) = a + (-b)
现在(-b)可以写为(b的2的补码)。因此,上面的表达式现在可以写成:
(a - b) = a + (2's complement of b)
因此,问题现在简化为“将a添加到b的2的补码中”。下图说明了上述第一个示例的减法,其中a = 2和b = 3。
下面是上述方法的实现:
C++
#include
using namespace std;
// function to subtract two values
// using 2's complement method
int Subtract(int a, int b)
{
int c;
// ~b is the 1's Complement of b
// adding 1 to it make it 2's Complement
c = a + (~b + 1);
return c;
}
// Driver code
int main()
{
int a = 2, b = 3;
cout << Subtract(a, b)<
Java
class GFG
{
// function to subtract two values
// using 2's complement method
static int Subtract(int a, int b)
{
int c;
// ~b is the 1's Complement
// of b adding 1 to it make
// it 2's Complement
c = a + (~b + 1);
return c;
}
// Driver code
public static void main(String[] args)
{
int a = 2, b = 3;
System.out.println(Subtract(a, b));
a = 9; b = 7;
System.out.println(Subtract(a, b));
}
}
// This code is contributed
// by ChitraNayal
Python3
# python3 program of subtraction of
# two numbers using 2's complement .
# function to subtract two values
# using 2's complement method
def Subtract(a,b):
# ~b is the 1's Complement of b
# adding 1 to it make it 2's Complement
c = a + (~b + 1)
return c
# Driver code
if __name__ == "__main__" :
# multiple assignments
a,b = 2,3
print(Subtract(a,b))
a,b = 9,7
print(Subtract(a,b))
C#
// C# program of subtraction of
// two numbers using 2's complement
using System;
class GFG
{
// function to subtract two values
// using 2's complement method
static int Subtract(int a, int b)
{
int c;
// ~b is the 1's Complement
// of b adding 1 to it make
// it 2's Complement
c = a + (~b + 1);
return c;
}
// Driver code
static void Main()
{
int a = 2, b = 3;
Console.WriteLine(Subtract(a, b));
a = 9; b = 7;
Console.WriteLine(Subtract(a, b));
}
}
// This code is contributed
// by mits
PHP
Javascript
C++
//C++ code for above approach
#include
#include
using namespace std;
// Program to substract
void Subtract(int n, int a[],
int b[])
{
// 1's Complement
for(int i = 0; i < n; i++)
{
//Replace 1 by 0
if(b[i] == 1)
{
b[i] = 0;
}
//Replace 0 by 1
else
{
b[i] = 1;
}
}
//Add 1 at end to get 2's Compliment
for(int i = n - 1; i >= 0; i--)
{
if(b[i] == 0)
{
b[i] = 1;
break;
}
else
{
b[i] = 0;
}
}
// Represents carry
int t = 0;
for(int i = n - 1; i >= 0; i--)
{
// Add a, b and carry
a[i] = a[i] + b[i] + t;
// If a[i] is 2
if(a[i] == 2)
{
a[i] = 0;
t = 1;
}
// If a[i] is 3
else if(a[i] == 3)
{
a[i] = 1;
t = 1;
}
else
t = 0;
}
cout << endl;
// If carry is generated
// discard the carry
if(t==1)
{
// print the result
for(int i = 0; i < n; i++)
{
// Print the result
cout<= 0; i--)
{
if(a[i] == 0)
{
a[i] = 1;
break;
}
else
a[i] = 0;
}
// Add -ve sign to represnt
cout << "-";
// -ve result
// Print the resultant array
for(int i = 0; i < n; i++)
{
cout << a[i];
}
}
}
// Driver Code
int main()
{
int n;
n = 5;
int a[] = {1, 0, 1, 0, 1},
b[] = {1, 1, 0, 1, 0};
Subtract(n,a,b);
return 0;
}
Java
// Java code for above approach
import java.io.*;
class GFG{
// Program to substract
static void Subtract(int n, int a[], int b[])
{
// 1's Complement
for(int i = 0; i < n; i++)
{
// Replace 1 by 0
if (b[i] == 1)
{
b[i] = 0;
}
// Replace 0 by 1
else
{
b[i] = 1;
}
}
// Add 1 at end to get 2's Compliment
for(int i = n - 1; i >= 0; i--)
{
if (b[i] == 0)
{
b[i] = 1;
break;
}
else
{
b[i] = 0;
}
}
// Represents carry
int t = 0;
for(int i = n - 1; i >= 0; i--)
{
// Add a, b and carry
a[i] = a[i] + b[i] + t;
// If a[i] is 2
if (a[i] == 2)
{
a[i] = 0;
t = 1;
}
// If a[i] is 3
else if (a[i] == 3)
{
a[i] = 1;
t = 1;
}
else
t = 0;
}
System.out.println();
// If carry is generated
// discard the carry
if (t == 1)
{
// Print the result
for(int i = 0; i < n; i++)
{
// Print the result
System.out.print(a[i]);
}
}
// If carry is not generated
else
{
// Calculate 2's Compliment
// of the obtained result
for(int i = 0; i < n; i++)
{
if (a[i] == 1)
a[i] = 0;
else
a[i] = 1;
}
for(int i = n - 1; i >= 0; i--)
{
if (a[i] == 0)
{
a[i] = 1;
break;
}
else
a[i] = 0;
}
// Add -ve sign to represnt
System.out.print("-");
// -ve result
// Print the resultant array
for(int i = 0; i < n; i++)
{
System.out.print(a[i]);
}
}
}
// Driver Code
public static void main (String[] args)
{
int n;
n = 5;
int a[] = {1, 0, 1, 0, 1};
int b[] = {1, 1, 0, 1, 0};
Subtract(n, a, b);
}
}
// This code is contributed by avanitrachhadiya2155
C#
// C# code for above approach
using System;
class GFG{
// Program to substract
static void Subtract(int n, int[] a, int[] b)
{
// 1's Complement
for(int i = 0; i < n; i++)
{
// Replace 1 by 0
if (b[i] == 1)
{
b[i] = 0;
}
// Replace 0 by 1
else
{
b[i] = 1;
}
}
// Add 1 at end to get 2's Compliment
for(int i = n - 1; i >= 0; i--)
{
if (b[i] == 0)
{
b[i] = 1;
break;
}
else
{
b[i] = 0;
}
}
// Represents carry
int t = 0;
for(int i = n - 1; i >= 0; i--)
{
// Add a, b and carry
a[i] = a[i] + b[i] + t;
// If a[i] is 2
if (a[i] == 2)
{
a[i] = 0;
t = 1;
}
// If a[i] is 3
else if (a[i] == 3)
{
a[i] = 1;
t = 1;
}
else
t = 0;
}
Console.WriteLine();
// If carry is generated
// discard the carry
if (t == 1)
{
// Print the result
for(int i = 0; i < n; i++)
{
// Print the result
Console.Write(a[i]);
}
}
// If carry is not generated
else
{
// Calculate 2's Compliment
// of the obtained result
for(int i = 0; i < n; i++)
{
if (a[i] == 1)
a[i] = 0;
else
a[i] = 1;
}
for(int i = n - 1; i >= 0; i--)
{
if (a[i] == 0)
{
a[i] = 1;
break;
}
else
a[i] = 0;
}
// Add -ve sign to represnt
Console.Write("-");
// -ve result
// Print the resultant array
for(int i = 0; i < n; i++)
{
Console.Write(a[i]);
}
}
}
// Driver Code
static public void Main()
{
int n;
n = 5;
int[] a = {1, 0, 1, 0, 1};
int[] b = {1, 1, 0, 1, 0};
Subtract(n, a, b);
}
}
// This code is contributed by rag2127
输出
-1
2
方法2:基本方法或蛮力方法
减去两个二进制数,使用2的补数法减去两个二进制数。
步骤1:找到子差的2的补码。
步骤2:添加减数的第一个数字和2的补码。
步骤3:如果产生了进位套,请丢弃进位套。如果没有进位,则取结果的2的补码。
下面是上述方法的实现。
C++
//C++ code for above approach
#include
#include
using namespace std;
// Program to substract
void Subtract(int n, int a[],
int b[])
{
// 1's Complement
for(int i = 0; i < n; i++)
{
//Replace 1 by 0
if(b[i] == 1)
{
b[i] = 0;
}
//Replace 0 by 1
else
{
b[i] = 1;
}
}
//Add 1 at end to get 2's Compliment
for(int i = n - 1; i >= 0; i--)
{
if(b[i] == 0)
{
b[i] = 1;
break;
}
else
{
b[i] = 0;
}
}
// Represents carry
int t = 0;
for(int i = n - 1; i >= 0; i--)
{
// Add a, b and carry
a[i] = a[i] + b[i] + t;
// If a[i] is 2
if(a[i] == 2)
{
a[i] = 0;
t = 1;
}
// If a[i] is 3
else if(a[i] == 3)
{
a[i] = 1;
t = 1;
}
else
t = 0;
}
cout << endl;
// If carry is generated
// discard the carry
if(t==1)
{
// print the result
for(int i = 0; i < n; i++)
{
// Print the result
cout<= 0; i--)
{
if(a[i] == 0)
{
a[i] = 1;
break;
}
else
a[i] = 0;
}
// Add -ve sign to represnt
cout << "-";
// -ve result
// Print the resultant array
for(int i = 0; i < n; i++)
{
cout << a[i];
}
}
}
// Driver Code
int main()
{
int n;
n = 5;
int a[] = {1, 0, 1, 0, 1},
b[] = {1, 1, 0, 1, 0};
Subtract(n,a,b);
return 0;
}
Java
// Java code for above approach
import java.io.*;
class GFG{
// Program to substract
static void Subtract(int n, int a[], int b[])
{
// 1's Complement
for(int i = 0; i < n; i++)
{
// Replace 1 by 0
if (b[i] == 1)
{
b[i] = 0;
}
// Replace 0 by 1
else
{
b[i] = 1;
}
}
// Add 1 at end to get 2's Compliment
for(int i = n - 1; i >= 0; i--)
{
if (b[i] == 0)
{
b[i] = 1;
break;
}
else
{
b[i] = 0;
}
}
// Represents carry
int t = 0;
for(int i = n - 1; i >= 0; i--)
{
// Add a, b and carry
a[i] = a[i] + b[i] + t;
// If a[i] is 2
if (a[i] == 2)
{
a[i] = 0;
t = 1;
}
// If a[i] is 3
else if (a[i] == 3)
{
a[i] = 1;
t = 1;
}
else
t = 0;
}
System.out.println();
// If carry is generated
// discard the carry
if (t == 1)
{
// Print the result
for(int i = 0; i < n; i++)
{
// Print the result
System.out.print(a[i]);
}
}
// If carry is not generated
else
{
// Calculate 2's Compliment
// of the obtained result
for(int i = 0; i < n; i++)
{
if (a[i] == 1)
a[i] = 0;
else
a[i] = 1;
}
for(int i = n - 1; i >= 0; i--)
{
if (a[i] == 0)
{
a[i] = 1;
break;
}
else
a[i] = 0;
}
// Add -ve sign to represnt
System.out.print("-");
// -ve result
// Print the resultant array
for(int i = 0; i < n; i++)
{
System.out.print(a[i]);
}
}
}
// Driver Code
public static void main (String[] args)
{
int n;
n = 5;
int a[] = {1, 0, 1, 0, 1};
int b[] = {1, 1, 0, 1, 0};
Subtract(n, a, b);
}
}
// This code is contributed by avanitrachhadiya2155
C#
// C# code for above approach
using System;
class GFG{
// Program to substract
static void Subtract(int n, int[] a, int[] b)
{
// 1's Complement
for(int i = 0; i < n; i++)
{
// Replace 1 by 0
if (b[i] == 1)
{
b[i] = 0;
}
// Replace 0 by 1
else
{
b[i] = 1;
}
}
// Add 1 at end to get 2's Compliment
for(int i = n - 1; i >= 0; i--)
{
if (b[i] == 0)
{
b[i] = 1;
break;
}
else
{
b[i] = 0;
}
}
// Represents carry
int t = 0;
for(int i = n - 1; i >= 0; i--)
{
// Add a, b and carry
a[i] = a[i] + b[i] + t;
// If a[i] is 2
if (a[i] == 2)
{
a[i] = 0;
t = 1;
}
// If a[i] is 3
else if (a[i] == 3)
{
a[i] = 1;
t = 1;
}
else
t = 0;
}
Console.WriteLine();
// If carry is generated
// discard the carry
if (t == 1)
{
// Print the result
for(int i = 0; i < n; i++)
{
// Print the result
Console.Write(a[i]);
}
}
// If carry is not generated
else
{
// Calculate 2's Compliment
// of the obtained result
for(int i = 0; i < n; i++)
{
if (a[i] == 1)
a[i] = 0;
else
a[i] = 1;
}
for(int i = n - 1; i >= 0; i--)
{
if (a[i] == 0)
{
a[i] = 1;
break;
}
else
a[i] = 0;
}
// Add -ve sign to represnt
Console.Write("-");
// -ve result
// Print the resultant array
for(int i = 0; i < n; i++)
{
Console.Write(a[i]);
}
}
}
// Driver Code
static public void Main()
{
int n;
n = 5;
int[] a = {1, 0, 1, 0, 1};
int[] b = {1, 1, 0, 1, 0};
Subtract(n, a, b);
}
}
// This code is contributed by rag2127
输出
-00101
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。