📜  以b为基数的数字的补码

📅  最后修改于: 2021-04-29 04:45:28             🧑  作者: Mango

  • 一个整数的补码
  • 二进制数的1和2的补码

在这篇文章中,找到了一个任意碱基互补的一般方法b讨论。

找到(b-1)的补码的步骤:要找到(b-1)的补码,

  • 用底数从数字系统中的最大数字中减去数字的每个数字b
  • 例如,如果数字是以9为底的三位数,则从888中减去该数字,因为8是以9为底的数字系统中的最大数字。
  • 获得的结果是(b-1)的补码(8的补码)。

查找b的补码的步骤:要查找b的补码,只需在计算出的(b-1)的补码上加1。

现在,这对于存在的数字系统中的任何基数都成立。可以使用1和2的补码作为基础进行测试。

范例

Let the number be 10111 base 2 (b=2)
Then, 1's complement will be 01000 (b-1)
2's complement will be 01001 (b)

Taking a number with Octal base:
Let the number be -456.
Then 7's compliment will be 321
and 8's compliment will be 322

下面是上述想法的实现:

C++
// CPP program to find complement of a 
// number with any base b
#include
#include
  
using namespace std;
  
// Function to find (b-1)'s complement
int prevComplement(int n, int b)
{
    int maxDigit, maxNum = 0, digits = 0, num = n;
      
    // Calculate number of digits 
    // in the given number
    while(n!=0)
    {
        digits++;
        n = n/10;
    }
      
    // Largest digit in the number
    // system with base b
    maxDigit = b-1;
      
    // Largest number in the number
    // system with base b
    while(digits--)
    {
        maxNum = maxNum*10 + maxDigit;
    }
      
    // return Complement
    return maxNum - num;
}
  
// Function to find b's complement
int complement(int n, int b)
{   
    // b's complement = (b-1)'s complement + 1
    return prevComplement(n,b) + 1;
}
  
// Driver code
int main()
{
    cout << prevComplement(25, 7)<


Java
// Java program to find complement 
// of a number with any base b
class GFG
{
  
// Function to find (b-1)'s complement
static int prevComplement(int n, int b)
{
    int maxDigit, maxNum = 0, 
        digits = 0, num = n;
      
    // Calculate number of digits 
    // in the given number
    while(n != 0)
    {
        digits++;
        n = n / 10;
    }
      
    // Largest digit in the number
    // system with base b
    maxDigit = b - 1;
      
    // Largest number in the number
    // system with base b
    while((digits--) > 0)
    {
        maxNum = maxNum * 10 + maxDigit;
    }
      
    // return Complement
    return maxNum - num;
}
  
// Function to find b's complement
static int complement(int n, int b)
{ 
    // b's complement = (b-1)'s
    // complement + 1
    return prevComplement(n, b) + 1;
}
  
// Driver code
public static void main(String args[])
{
    System.out.println(prevComplement(25, 7));
      
    System.out.println(complement(25, 7));
}
}
  
// This code is contributed
// by Kirti_Mangal


Python 3
# Python 3 program to find 
# complement of a number
# with any base b
  
# Function to find 
# (b-1)'s complement 
def prevComplement(n, b) :
    maxNum, digits, num = 0, 0, n
  
    # Calculate number of digits 
    # in the given number 
    while n > 1 :
        digits += 1
        n = n // 10
  
    # Largest digit in the number 
    # system with base b 
    maxDigit = b - 1
  
    # Largest number in the number 
    # system with base b 
    while digits :
        maxNum = maxNum * 10 + maxDigit
        digits -= 1
          
    # return Complement 
    return maxNum - num
  
# Function to find b's complement
def complement(n, b) :
  
    # b's complement = (b-1)'s
    # complement + 1 
    return prevComplement(n, b) + 1
  
# Driver code
if __name__ == "__main__" :
      
    # Function calling
    print(prevComplement(25, 7))
    print(complement(25, 7))
  
# This code is contributed 
# by ANKITRAI1


C#
// C# program to find complement 
// of a number with any base b
class GFG
{
  
// Function to find (b-1)'s complement
static int prevComplement(int n, int b)
{
    int maxDigit, maxNum = 0, 
        digits = 0, num = n;
      
    // Calculate number of digits 
    // in the given number
    while(n != 0)
    {
        digits++;
        n = n / 10;
    }
      
    // Largest digit in the number
    // system with base b
    maxDigit = b - 1;
      
    // Largest number in the number
    // system with base b
    while((digits--) > 0)
    {
        maxNum = maxNum * 10 + maxDigit;
    }
      
    // return Complement
    return maxNum - num;
}
  
// Function to find b's complement
static int complement(int n, int b)
{ 
    // b's complement = (b-1)'s
    // complement + 1
    return prevComplement(n, b) + 1;
}
  
// Driver code
public static void Main()
{
    System.Console.WriteLine(prevComplement(25, 7));
      
    System.Console.WriteLine(complement(25, 7));
}
}
  
// This code is contributed
// by mits


PHP


输出:
41
42