是否与我期待的相反?

时间:2021-08-19 16:52:12

I did some tests with enums and bitwise operators today and to verify the functionality with Xcode I created a series of tests. Here's the full Gist which I will keep public.

我今天用枚举和按位运算符做了一些测试,并用Xcode验证了功能,我创建了一系列测试。这是我将继续公开的完整要点。

https://gist.github.com/brennanMKE/ede1f685018e953da8a4

For the enum I have set bit shifted values so that I can use bitwise operators to check values. Here is the typedef for the enum for MyState.

对于枚举,我设置了位移值,以便我可以使用按位运算符来检查值。这是MyState枚举的typedef。

typedef NS_OPTIONS(NSUInteger, MyState) {
    MyStateNone                 = 0,            // (0000 = 0)
    MyStateOn                   = (1 << 0),     // (1000 = 1)
    MyStateOff                  = (1 << 1),     // (0100 = 2)
    MyStateEnabled              = (1 << 2),     // (0010 = 4)
    MyStateDisabled             = (1 << 3),     // (0001 = 8)
    MyStateOnAndEnabled         = MyStateOn | MyStateEnabled,   // 5
    MyStateOffAndDisabled       = MyStateOff | MyStateDisabled  // 10
};

You can see that the on, off, enabled and disabled state are in sequence which would allow for shifting the values to change states from On and Enabled to Off and Disabled. The bits would go from 1010 to 0101.

您可以看到开启,关闭,启用和禁用状态是顺序的,这将允许移动值以将状态从“开”和“启用”更改为“关闭”和“禁用”。这些位将从1010变为0101。

I believe that should be a right shift (>>) but in my tests it is a left shift which works. Below are the test functions. Why is it working with the opposite shift operators from what I am expecting? Maybe my understanding is opposite of what it does. That seems to be the case.

我相信这应该是一个正确的转变(>>),但在我的测试中,它是一个左移,它起作用。以下是测试功能。为什么它与我期望的相反的班次操作员合作?也许我的理解与它的作用相反。情况似乎如此。

Left shift seems to put a 0 on the left side, moving all bits to the right. I thought it meant that left shift meant all bits moved left. Could someone clarify? Thanks.

左移似乎在左侧放置0,将所有位向右移动。我认为这意味着左移意味着所有位都向左移动。有人可以澄清吗?谢谢。

- (void)testBitShiftingToOnAndEnabled {
    // Note: shifting enum values is ill advised but useful academically

    MyState state = MyStateOffAndDisabled; // 0101 (10)
    NSLog(@"state: %lu", (unsigned long)state);
    // if both values are shifted 1 to the right it becomes off and disabled
    state >>= 1; // 1010 (5)
    NSLog(@"state: %lu", (unsigned long)state);

    XCTAssert(state == MyStateOnAndEnabled, @"Pass");
    XCTAssert(state == 5, @"Pass");
}

- (void)testBitShiftingToOffAndDisabled {
    // Note: shifting enum values is ill adviced but useful academically

    MyState state = MyStateOnAndEnabled; // 1010 (5)
    NSLog(@"state: %lu", (unsigned long)state);
    // if both values are shifted 1 to the left it becomes off and disabled
    state <<= 1; // 0101 (10)
    NSLog(@"state: %lu", (unsigned long)state);

    XCTAssert(state == MyStateOffAndDisabled, @"Pass");
    XCTAssert(state == 10, @"Pass");
}

1 个解决方案

#1


The expression 1<<2 means to shift the bits for the value 1 to the left 2 places. Think of the << as arrows pointing left.

表达式1 << 2表示将值1的位移位到左侧2位。想想< <左箭头指向左边。< p>

The value 1 in binary (8-bits):

二进制值(8位)中的值1:

00000001

Now shift the bits to the left 2 places:

现在将位移到左侧2位:

00000100

That's why 1<<2 changes the value 1 to 4.

这就是1 << 2将值1改为4的原因。

Your misunderstanding is that the bit ordering starts on the right and goes to the left.

您的误解是位顺序从右侧开始并向左侧移动。

Updated comments for your code:

更新了代码的注释:

typedef NS_OPTIONS(NSUInteger, MyState) {
    MyStateNone                 = 0,            // (0000 = 0)
    MyStateOn                   = (1 << 0),     // (0001 = 1)
    MyStateOff                  = (1 << 1),     // (0010 = 2)
    MyStateEnabled              = (1 << 2),     // (0100 = 4)
    MyStateDisabled             = (1 << 3),     // (1000 = 8)
    MyStateOnAndEnabled         = MyStateOn | MyStateEnabled,   // 5
    MyStateOffAndDisabled       = MyStateOff | MyStateDisabled  // 10
};

BTW - this is basic C. None this is specific to Objective-C.

顺便说一句 - 这是基本的C.没有这是Objective-C特有的。

#1


The expression 1<<2 means to shift the bits for the value 1 to the left 2 places. Think of the << as arrows pointing left.

表达式1 << 2表示将值1的位移位到左侧2位。想想< <左箭头指向左边。< p>

The value 1 in binary (8-bits):

二进制值(8位)中的值1:

00000001

Now shift the bits to the left 2 places:

现在将位移到左侧2位:

00000100

That's why 1<<2 changes the value 1 to 4.

这就是1 << 2将值1改为4的原因。

Your misunderstanding is that the bit ordering starts on the right and goes to the left.

您的误解是位顺序从右侧开始并向左侧移动。

Updated comments for your code:

更新了代码的注释:

typedef NS_OPTIONS(NSUInteger, MyState) {
    MyStateNone                 = 0,            // (0000 = 0)
    MyStateOn                   = (1 << 0),     // (0001 = 1)
    MyStateOff                  = (1 << 1),     // (0010 = 2)
    MyStateEnabled              = (1 << 2),     // (0100 = 4)
    MyStateDisabled             = (1 << 3),     // (1000 = 8)
    MyStateOnAndEnabled         = MyStateOn | MyStateEnabled,   // 5
    MyStateOffAndDisabled       = MyStateOff | MyStateDisabled  // 10
};

BTW - this is basic C. None this is specific to Objective-C.

顺便说一句 - 这是基本的C.没有这是Objective-C特有的。