BigInteger.OnesComplement(BigInteger) 演算子

定義

BigInteger 値のビットごとの 1 の補数を返します。Returns the bitwise one's complement of a BigInteger value.

public:
 static System::Numerics::BigInteger operator ~(System::Numerics::BigInteger value);
public static System.Numerics.BigInteger operator ~ (System.Numerics.BigInteger value);
static member op_OnesComplement : System.Numerics.BigInteger -> System.Numerics.BigInteger
Public Shared Operator Not (value As BigInteger) As BigInteger

パラメーター

value
BigInteger

整数値です。An integer value.

戻り値

value のビットごとの 1 の補数。The bitwise one's complement of value.

注釈

OnesComplement メソッドは、BigInteger 値のビットごとの1の補数演算子の演算を定義します。The OnesComplement method defines the operation of the bitwise one's complement operator for BigInteger values. ビットごとの1の補数演算子は、数値の各ビットを反転します。The bitwise one's complement operator reverses each bit in a numeric value. つまり、結果で0に設定された value のビットは1に設定され、結果では1であるビットが0に設定されます。That is, bits in value that are 0 are set to 1 in the result, and bits that are 1 are set to 0 in the result. OnesComplement メソッドは、次のようなコードを有効にします。The OnesComplement method enables code such as the following:

using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      BigInteger value, complement;
      
      value = BigInteger.Multiply(BigInteger.One, 9);
      complement = ~value;
      
      Console.WriteLine("{0,5} -- {1,-32}", value, DisplayInBinary(value));
      Console.WriteLine("{0,5} -- {1,-32}\n", complement, DisplayInBinary(complement));
   
      value = BigInteger.MinusOne * SByte.MaxValue;
      complement = ~value;
      
      Console.WriteLine("{0,5} -- {1,-32}", value, DisplayInBinary(value));
      Console.WriteLine("{0,5} -- {1,-32}\n", complement, DisplayInBinary(complement));
   } 

   private static string DisplayInBinary(BigInteger number)
   {
      byte[] bytes = number.ToByteArray();  
      string binaryString = string.Empty;
      foreach (byte byteValue in bytes)
      {
         string byteString = Convert.ToString(byteValue, 2).Trim();
         binaryString += byteString.Insert(0, new string('0', 8 - byteString.Length));
      }
      return binaryString;    
   }
}
// The example displays the following output:
//           9 -- 00001001
//         -10 -- 11110110
//       
//        -127 -- 10000001
//         126 -- 01111110
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim value, complement As bigInteger
      
      value = BigInteger.Multiply(BigInteger.One, 9)
      complement = Not value
      
      Console.WriteLine("{0,5} -- {1,-32}", value, DisplayInBinary(value))
      Console.WriteLine("{0,5} -- {1,-32}", complement, DisplayInBinary(complement))
      Console.WriteLine()
   
      value = BigInteger.MinusOne * SByte.MaxValue
      complement = BigInteger.op_OnesComplement(value)
      
      Console.WriteLine("{0,5} -- {1,-32}", value, DisplayInBinary(value))
      Console.WriteLine("{0,5} -- {1,-32}", complement, DisplayInBinary(complement))
      Console.WriteLine()
   End Sub

   Private Function DisplayInBinary(number As BigInteger) As String
      Dim bytes() As Byte = number.ToByteArray()  
      Dim binaryString As String = String.Empty
      For Each byteValue As Byte In bytes
         Dim byteString As String = Convert.ToString(byteValue, 2).Trim()
         binaryString += byteString.Insert(0, New String("0"c, 8 - byteString.Length))
      Next
      Return binaryString    
   End Function
End Module
' The example displays the following output:
'           9 -- 00001001
'         -10 -- 11110110
'       
'        -127 -- 10000001
'         126 -- 01111110

カスタム演算子をサポートしない言語では、OnesComplement メソッドを直接呼び出して、ビットごとの1の補数演算を実行できる場合があります。Languages that do not support custom operators may be able to call the OnesComplement method directly to perform a bitwise one's complement operation. 例 : For example:

Imports System.Numerics

Module Example
   Public Sub Main()
      Dim value, complement As bigInteger
      
      value = BigInteger.Multiply(BigInteger.One, 9)
      complement = BigInteger.op_OnesComplement(value)
      
      Console.WriteLine("{0,5} -- {1,-32}", value, DisplayInBinary(value))
      Console.WriteLine("{0,5} -- {1,-32}", complement, DisplayInBinary(complement))
      Console.WriteLine()
   
      value = BigInteger.MinusOne * SByte.MaxValue
      complement = BigInteger.op_OnesComplement(value)
      
      Console.WriteLine("{0,5} -- {1,-32}", value, DisplayInBinary(value))
      Console.WriteLine("{0,5} -- {1,-32}", complement, DisplayInBinary(complement))
      Console.WriteLine()
   End Sub

   Private Function DisplayInBinary(number As BigInteger) As String
      Dim bytes() As Byte = number.ToByteArray()  
      Dim binaryString As String = String.Empty
      For Each byteValue As Byte In bytes
         Dim byteString As String = Convert.ToString(byteValue, 2).Trim()
         binaryString += byteString.Insert(0, New String("0"c, 8 - byteString.Length))
      Next
      Return binaryString    
   End Function
End Module
' The example displays the following output:
'           9 -- 00001001
'         -10 -- 11110110
'       
'        -127 -- 10000001
'         126 -- 01111110

適用対象