using System;  
class Test  
{ public static void Main() {
    byte a = 10;
    sbyte b = 10;
    short c = 10;  
    ushort d = 10;  
    int e = 10;  
    uint f = 10;
    long g = 10;
    ulong h = 10;
    Console.WriteLine("byte: {0}",~a);  
    Console.WriteLine("sbyte: {0}",~b);  
    Console.WriteLine("short: {0}",~c);  
    Console.WriteLine("ushort: {0}",~d);
    Console.WriteLine("int: {0}", ~e);
    Console.WriteLine("uint: {0}", ~f);
    Console.WriteLine("long: {0}", ~g);
    Console.WriteLine("ulong: {0}", ~h); 

 } 
 
运算结果为:
-11
-11
-11
-11
-11
4294967285
-11
18446744073709551605为什么是这个结果,为什么??????

解决方案 »

  1.   

    ~应该是求反运算符吧,虽然msdn中翻译为求补,但是给出的例子是
    ~0x00000111 = 0xfffffeee,这应该是求反码。前5个是一样的,按位求反运算符是为 int、uint、long 和 ulong 类型预定义的。而且存在 sbyte、byte、short、ushort 到int的隐式转换,所以前5个都是对 int a = 10的求反。
    10 = 0x0000000A, ~10 = 0xFFFFFFF5,因为对于负数计算机是采用补码表示(取反+1),11二进制表示为0x0B(.... 0000 1011),取反后(.... 1111 0100),+1 后(.... 1111 0101),正是0xFFFFFFF5uint就更好理解了,因为是无符号,则十进制的值根据二进制递增。long、ulong和int、uint类似。