<< Objective C 枚举中的运算符?

发布于 2024-12-23 08:00:00 字数 454 浏览 1 评论 0 原文

我正在寻找一些东西并进入这个枚举是apple UITableViewCell.h。

如果这是微不足道的,我很抱歉,但我想知道/好奇这有什么意义。

我知道<<来自 ruby​​ 但我真的不明白这个枚举?

enum {
    UITableViewCellStateDefaultMask                     = 0,
    UITableViewCellStateShowingEditControlMask          = 1 << 0,
    UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1
};

谢谢

顺便说一句 发现它是学习编码的好方法,我每天尝试一次进入对象列表的头文件。

沙尼

I was looking for something and got in to this enum is apple UITableViewCell.h.

I am sorry if this is trivial but I wonder/curious what is the point of this.

I know the << from ruby but I don't really understand this enum ?

enum {
    UITableViewCellStateDefaultMask                     = 0,
    UITableViewCellStateShowingEditControlMask          = 1 << 0,
    UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1
};

Thanks

BTW
Found it as a great way to learn coding, I am trying once in a day to get into the header files of at list on object.

Shani

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

朦胧时间 2024-12-30 08:00:00

这些是位字段标志。使用它们是因为您可以使用按位或运算符将它们组合起来。例如,您可以将它们组合起来,就像

(UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask)

它们通过在整数中设置一位来工作一样。在此示例中,以二进制形式,

UITableViewCellStateShowingEditControlMask        = 0000 0001
UITableViewCellStateShowingDeleteConfirmationMask = 0000 0010

将它们进行“或”运算时,会生成 0000 0011。然后框架知道这两个标志都已设置。

<< 运算符是左移。它改变了二进制表示形式。所以 1 << 1 表示

0000 0001 shifted left by one bit = 0000 0010

1 << 2 等于 0000 0100

These are bit-field flags. They are used because you can combine them using the bitwise-OR operator. So for example you can combine them like

(UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask)

They work by having one bit set in an integer. In this example, in binary,

UITableViewCellStateShowingEditControlMask        = 0000 0001
UITableViewCellStateShowingDeleteConfirmationMask = 0000 0010

When they are OR'ed together, they produce 0000 0011. The framework then knows that both of these flags are set.

The << operator is a left-shift. It shifts the binary representation. So 1 << 1 means

0000 0001 shifted left by one bit = 0000 0010

1 << 2 would equal 0000 0100.

挖个坑埋了你 2024-12-30 08:00:00

它实际上是按位移位运算符

<<  Indicates the bits are to be shifted to the left.
>>  Indicates the bits are to be shifted to the right.

所以在你的语句中 1 << 的值0 是 1 并且 1 << 1 等于 2

Its actually BItwise shift operator

<<  Indicates the bits are to be shifted to the left.
>>  Indicates the bits are to be shifted to the right.

So in your statement the value of 1 << 0 is 1 and 1 << 1 is 2

迷雾森÷林ヴ 2024-12-30 08:00:00

C 中的一个常见技巧是在枚举值中使用按位移位运算符,以允许您将枚举值与按位或运算符组合。

这段代码相当于

enum {
    UITableViewCellStateDefaultMask                     = 0,
    UITableViewCellStateShowingEditControlMask          = 1, // 01 in binary
    UITableViewCellStateShowingDeleteConfirmationMask   = 2  // 10 in binary
};

这允许您按位两个或多个枚举常量在一起,

 (UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask) // == 3 (or 11 in binary)

给出一个新常量,该常量同时表示这两种情况。在这种情况下,单元格显示编辑控件和删除确认控件,或类似的东西。

It's a common trick in C to use the bitwise shift operator in enum values to allow you to combine enumeration values with the bitwise or operator.

That piece of code is equivalent to

enum {
    UITableViewCellStateDefaultMask                     = 0,
    UITableViewCellStateShowingEditControlMask          = 1, // 01 in binary
    UITableViewCellStateShowingDeleteConfirmationMask   = 2  // 10 in binary
};

This allows you to bitwise or two or more enumeration constants together

 (UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask) // == 3 (or 11 in binary)

to give a new constant that means both of those things at once. In this case, the cell is showing both an editing control and a delete confirmation control, or something like that.

十秒萌定你 2024-12-30 08:00:00

这些操作数称为位移位。位移位操作数可以优先用于
2个原因。
- 为了快速操作
- 一次使用多个布尔值。

例如:1<<2为左移;这意味着
1:0001,
2:0010

1<< 2 这行的意思是 2 应该留一位。结果 0010 转移到 0100

并且转移的值必须排序为 1,2,4,8,16...

typedef NS_OPTIONS(int, EntityEngineer) {
EntityEngineeriOS = 1 << 0,
EntityCategoryAndroid = 1 << 1,
EntityCategoryDB = 1 << 2,
EntityCategoryTeamLead = 1 << 16,};

现在,我们要检查下面一行中的多个布尔值,

char engineer = (EntityEngineeriOS | EntityCategoryAndroid);

char 0011 = (0001 | 0010);

if (engineer & EntityEngineeriOS) {
    NSLog(@"we are looking for a developer who can write objective c or java");
}

if (engineer & EntityCategoryDB) {
    NSLog(@"we are not looking for a DB manager");
}

结果:我们正在寻找一位可以编写 Objective C 或 Java 的开发人员

These operand called bitshift. Bitshift operand can be preferred for
2 reasons.
- For fast operation
- Use multiple bool value in one time.

For example : 1<<2 is a left shift; that means
1 : 0001,
2 : 0010

1 << 2 this line means is 2 should be left one bit. As a result 0010 shifted to 0100

Also shifted value must ordered as a 1,2,4,8,16...

typedef NS_OPTIONS(int, EntityEngineer) {
EntityEngineeriOS = 1 << 0,
EntityCategoryAndroid = 1 << 1,
EntityCategoryDB = 1 << 2,
EntityCategoryTeamLead = 1 << 16,};

Now, we want to check mutiple boolean in below line,

char engineer = (EntityEngineeriOS | EntityCategoryAndroid);

char 0011 = (0001 | 0010);

if (engineer & EntityEngineeriOS) {
    NSLog(@"we are looking for a developer who can write objective c or java");
}

if (engineer & EntityCategoryDB) {
    NSLog(@"we are not looking for a DB manager");
}

Result : we are looking for a developer who can write objective c or java

最笨的告白 2024-12-30 08:00:00

这就是位移运算符。这通常用于可能具有多种行为的对象(每个枚举都是一种行为)。

这是一个类似的帖子,可以更好地阐明它。

That is the bitshift operator. That is used commonly for objects that may have multiple behaviors (each enum being a behavior).

Here is a similar post that may clarify it better.

甜尕妞 2024-12-30 08:00:00

这些类型的运算符称为按位运算符,它对数字的位值进行运算。与其他算术运算相比,这些运算非常快。

These types of operator are called bitwise operator which operates on bit value of a number. These operation are very fast as compared to other arithematic operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文