- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在阅读有关标志枚举和按位运算符的内容时,遇到了这段代码:
enum file{
read = 1,
write = 2,
readandwrite = read | write
}
我在某处读到关于为什么有包含性或声明以及为什么不能有 &,但找不到文章。有人可以帮我重温一下并解释一下原因吗?
此外,我怎么说和/或?例如。如果 dropdown1="hello"和/或 dropdown2="hello"...
谢谢
最佳答案
第一个问题:
A |
做按位或;如果在第一个值或第二个值中设置了某个位,则会在结果中设置它。 (您在 enums
上使用它来创建由其他值组合而成的值)如果您要使用按位 and,它就没有多大意义。
它的用法如下:
[Flags]
enum FileAccess{
None = 0, // 00000000 Nothing is set
Read = 1, // 00000001 The read bit (bit 0) is set
Write = 2, // 00000010 The write bit (bit 1) is set
Execute = 4, // 00000100 The exec bit (bit 2) is set
// ...
ReadWrite = Read | Write // 00000011 Both read and write (bits 0 and 1) are set
// badValue = Read & Write // 00000000 Nothing is set, doesn't make sense
ReadExecute = Read | Execute // 00000101 Both read and exec (bits 0 and 2) are set
}
// Note that the non-combined values are powers of two, \
// meaning each sets only a single bit
// ...
// Test to see if access includes Read privileges:
if((access & FileAccess.Read) == FileAccess.Read)
本质上,您可以测试是否设置了enum
中的某些位;在这种情况下,我们正在测试是否设置了对应于 Read
的位。值 Read
和 ReadWrite
都将通过此测试(都设置了位零); Write
不会(它没有设置位零)。
// if access is FileAccess.Read
access & FileAccess.Read == FileAccess.Read
// 00000001 & 00000001 => 00000001
// if access is FileAccess.ReadWrite
access & FileAccess.Read == FileAccess.Read
// 00000011 & 00000001 => 00000001
// uf access is FileAccess.Write
access & FileAccess.Read != FileAccess.Read
// 00000010 & 00000001 => 00000000
第二个问题:
我认为当您说“和/或”时,您的意思是“一个、另一个或两者”。这正是 ||
(或运算符)所做的。要说“一个或另一个,但不是两个”,您将使用 ^
(异或运算符)。
真值表(true==1,false==0):
A B | A || B
------|-------
OR 0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1 (result is true if any are true)
A B | A ^ B
------|-------
XOR 0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0 (if both are true, result is false)
关于c# - 按位或 | 是什么意思运营商呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/612072/
这个问题已经存在: 关闭 13 年前。 Possible Duplicate: What is the !! (not not) operator in JavaScript? 我见过运算符 ! 像
我最近正在读《行动中的格鲁夫》。在第7章中,它介绍了*。运算符(operator) 。当我运行有关此运算符的代码时,我会遇到一些错误。 class Invoice {
我在沙箱 Groovy 上工作,我想阻止 .@ 运算符。我正在使用 SecureASTCustomizer 并编写了一个自定义的 SecureASTCustomizer.ExpressionCheck
我有一个函数接受一个包含大量数据的数组,例如,其中一个数据是“name”:$data = ['name' => 'John'] ; 现在,如果我想使用默认值从中获取名称,我有两个经常使用的选项。 $n
我只是想知道这是否是 C89 程序的“好”代码。 obj_ptr = (obj*) (ptr1 || ptr2); 本质上,如果 ptr1 != NULL 和 ptr2,它的作用(至少在我的计算机上的
我定义了一个类 User,并覆盖了它的 == 运算符,如下所示: class User attr_reader :age def initialize age @age = age
这个问题在这里已经有了答案: Why does "++x || ++y && ++z" calculate "++x" first, even though operator "&&" has hi
pipe(|) 和output(>) 重定向运算符有什么区别?我可以在哪里使用它们? 例如: 我通常只在 grep 中使用管道 (|) find . | grep abc 我遇到的输入输出重定向的唯一
我正在为此代码使用 g++ 10.2。有谁知道为什么我在最后一次 std::views::reverse 中收到编译器错误?在 results3 ? #include #include int m
假设我们正在使用 MASM 6.1/16 位/大数据模型编写汇编代码,并且我们有一个名为 MY_VAR 的变量(标签)、一个名为 MY_SEG 的段和一个名为 MY_GROUP 的段组。假设 MY_V
我试图在基类中使用宇宙飞船运算符,所以我将通过编译器定义所有运算符。 (来源:https://devblogs.microsoft.com/cppblog/simplify-your-code-wit
我在某处看到这段代码片段: var idx = SOME_VALUE; var color = { yellor: 1, red: 2, black: 0 }; var x = color
我观察到在某个时候有一个 ?海湾合作委员会的运营商。我如何在 GCC 4.5 下使用它们?它们是否已被移除,如果是,何时移除? Offset block_count = (cpfs->geo.bloc
我不是每天都阅读 C#,而是正在阅读最近的提交,其中包含一些让我感到困惑的内容: if (( length > maxLength) && ! IsTable(furniture.FurnitureT
拥有 public void Main() { int i = 0; Console.WriteLine($"I was {i++}, now I is {i}"); bool b = t
我观察到在某个时候有一个 ?海湾合作委员会的运营商。我如何在 GCC 4.5 下使用它们?它们是否已被移除,如果是,何时移除? Offset block_count = (cpfs->geo.bloc
今天,我听说了 Karatsuba 算法,一种快速乘法算法。我很好奇这个“快”是什么意思? 通常,我们在计算一段代码的时间复杂度时,会考虑使用 * 运算符作为 O(1) 的乘法运算,如果它始终为真,那
我观察到在某个时候有一个 ?海湾合作委员会的运营商。我如何在 GCC 4.5 下使用它们?它们是否已被移除,如果是,何时移除? Offset block_count = (cpfs->geo.bloc
我正在学习 Angular 和 RxJS 运算符。我正在使用 Hearthstone API 来训练它。我究竟做错了什么 ?我想过滤流以仅返回“萨满”卡。我知道,Api 让我有机会按类(class)获
正如标题所说,我正在尝试通过 += 运算符将文本附加到隐式解包的可选字符串,这给了我 'String!' is not identical to 'UInt8' 让你明白我的意思: var myStr
我是一名优秀的程序员,十分优秀!