我在哪里可以使用 &和 == 比较常量时?有什么不同? [英] Where can I use & and == when comparing constants? What is the difference?

查看:27
本文介绍了我在哪里可以使用 &和 == 比较常量时?有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道两者之间的区别:

i want to know whats diffrence between :

file.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY

和:

file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY

当我使用第一个时,我会得到更多文件.

when i use the first one i get more files.

FILE_ATTRIBUTE_DIRECTORY 是文件属性常量,file.dwFileAttributes 是 Windows API 中的 WIN32_FIND_DATA 结构成员.

FILE_ATTRIBUTE_DIRECTORY is a File Attribute Constants and file.dwFileAttributes is a WIN32_FIND_DATA structure member in windows API.

推荐答案

规则很简单:

  • 当变量只能容纳一个值时,您可以使用相等运算符 == 进行测试.
  • 当变量可以包含零个、一个或多个标志时,您可以使用按位和运算符 & 测试该标志是否存在.
  • When the variable can hold exactly one value, you test with the equality operator, ==.
  • When the variable can hold zero, one or more flags, you test for the presence of the flag with the bitwise and operator, &.

dwFileAttributes 的情况下,文档说:

In the case of dwFileAttributes, the documentation says:

文件的文件属性.有关可能的值及其说明,请参阅 ...

The file attributes of a file. For possible values and their descriptions, see ...

使用复数表示我的第二个要点适用.

The use of the plural indicates that the second of my bullet points applies.

这篇关于我在哪里可以使用 &和 == 比较常量时?有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆