BigInteger.OnesComplement(BigInteger) 运算符
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回 BigInteger 值的按位二进制反码。
public:
static System::Numerics::BigInteger operator ~(System::Numerics::BigInteger value);
public:
static System::Numerics::BigInteger operator ~(System::Numerics::BigInteger value) = System::Numerics::IBitwiseOperators<System::Numerics::BigInteger, System::Numerics::BigInteger, System::Numerics::BigInteger>::op_OnesComplement;
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
一个整数值。
返回
value
的按位二进制反码。
实现
注解
方法 OnesComplement 定义值的按位补补运算符的 BigInteger 运算。 按位补运算符反转数值中的每个位。 也就是说,结果中 value
为 0 的位设置为 1,在结果中将 1 的位设置为 0。 方法 OnesComplement 启用如下代码:
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 方法以执行按位补码运算。 例如:
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
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈