gpt4 book ai didi

c - 为什么对 C 枚举定义中的值使用 Bitwise-Shift 运算符?

转载 作者:太空狗 更新时间:2023-10-29 16:20:40 30 4
gpt4 key购买 nike

Apple 有时会在他们的enum 定义中使用 Bitwise-Shift 运算符。例如,在属于 Core Graphics 的 CGDirectDisplay.h 文件中:

enum {
kCGDisplayBeginConfigurationFlag = (1 << 0),
kCGDisplayMovedFlag = (1 << 1),
kCGDisplaySetMainFlag = (1 << 2),
kCGDisplaySetModeFlag = (1 << 3),
kCGDisplayAddFlag = (1 << 4),
kCGDisplayRemoveFlag = (1 << 5),
kCGDisplayEnabledFlag = (1 << 8),
kCGDisplayDisabledFlag = (1 << 9),
kCGDisplayMirrorFlag = (1 << 10),
kCGDisplayUnMirrorFlag = (1 << 11),
kCGDisplayDesktopShapeChangedFlag = (1 << 12)
};
typedef uint32_t CGDisplayChangeSummaryFlags;

为什么不像在“普通”枚举中那样简单地使用递增的int

最佳答案

也许以十六进制(或二进制)形式编写值会有所帮助:-)

enum {
kCGDisplayBeginConfigurationFlag = (1 << 0), /* 0b0000000000000001 */
kCGDisplayMovedFlag = (1 << 1), /* 0b0000000000000010 */
kCGDisplaySetMainFlag = (1 << 2), /* 0b0000000000000100 */
kCGDisplaySetModeFlag = (1 << 3), /* 0b0000000000001000 */
kCGDisplayAddFlag = (1 << 4), /* 0b0000000000010000 */
kCGDisplayRemoveFlag = (1 << 5), /* 0b0000000000100000 */
kCGDisplayEnabledFlag = (1 << 8), /* 0b0000000100000000 */
kCGDisplayDisabledFlag = (1 << 9), /* 0b0000001000000000 */
kCGDisplayMirrorFlag = (1 << 10),/* 0b0000010000000000 */
kCGDisplayUnMirrorFlag = (1 << 11),/* 0b0000100000000000 */
kCGDisplayDesktopShapeChangedFlag = (1 << 12) /* 0b0001000000000000 */
};

现在您可以添加它们(或“或”它们)并获得不同的值

kCGDisplayAddFlag | kCGDisplayDisabledFlag /* 0b0000001000010000 */

关于c - 为什么对 C 枚举定义中的值使用 Bitwise-Shift 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3999922/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com