0

我想出了一个使用按位运算的循环,产生一个每隔一个位打开的数字(即在 8 位的情况下,01010101)。

从理论上讲,我的循环应该可以正常工作,并且可以正常使用uint32and uint64,但不能正常使用uint8or uint16。我想知道为什么...

这是代码:

@autoreleasepool {
    // a = 00000000
    uint32 a = 0;
    // b = 11111111
    uint32 b = ~a;
    // a = 11111111
    a = ~a;

    // if (a = 01010101) ~a = 10101010, a << 1 = 10101010
    while (~a != (a << 1)) {
        // 1st time: a << 1 = 11111110 = a
        // 2nd time: a << 1 = 11111010 = a
        a = a << 1;
        // 1st time: ~a = 00000001 = a
        // 2nd time: ~a = 00000101 = a
        a = ~a;
        // 1st time: a << 1 = 00000010 = a
        // 2nd time: a << 1 = 00001010 = a
        a = a << 1;
        // 1st time: b ^ a = 11111101 = a
        // 2nd time: b ^ a = 11110101 = a
        a = b ^ a;
    }

    NSLog(@"%x", a);
    NSLog(@"%u", b);



    // Apply the same loop to a bigger scale
    uint64 x = 0x0;
    uint64 y = ~x;
    x = ~x;

    while (~x != (x << 1)) {
        x = x << 1;
        x = ~x;
        x = x << 1;
        x = y ^ x;
    }

    NSLog(@"%llx", x);
    NSLog(@"%llu", x);
}
return 0;
4

1 回答 1

1

通过“小于 int” starblue 表示sizeof(a) < sizeof(int)。由于整数提升规则,比 int 窄的类型总是在执行操作之前提升为 int。有关更多信息,请阅读隐式类型提升规则

因此,如果 a 是uint8uint16则 的最高位~a将始终为 1并且永远不会等于a << 1

例如,如果 a 是uint16,运行几次迭代我们有 a = 0x5555。在那之后

(int)a = 0x00005555
    ~a = 0xFFFFAAAA
a << 1 = 0x0000AAAA

如您所见,~a != (a << 1)程序将永远循环

于 2014-06-23T06:29:19.167 回答