我有这样的枚举:结合2个枚举与数学运算符
enum bus {
MEDIA_BUS_UNKNOWN,
MEDIA_BUS_VIRTUAL = 1 << 1,
MEDIA_BUS_PCI = 1 << 2,
MEDIA_BUS_USB = 1 << 3,
};
和:
enum bus get_bus(char *sys)
{
FILE *fd;
char file[PATH_MAX];
char s[1024];
if(!strcmp(sys, "/sys/devices/virtual"))
return MEDIA_BUS_VIRTUAL;
snprintf(file, PATH_MAX, "%s/modalias", sys);
fd = fopen(file, "r");
if(!fd)
return MEDIA_BUS_UNKNOWN;
if(!fgets(s, sizeof(s), fd)) {
fclose(fd);
return MEDIA_BUS_UNKNOWN;
}
fclose(fd);
if(!strncmp(s, "pci", 3))
return MEDIA_BUS_PCI;
if(!strncmp(s, "usb", 3))
return MEDIA_BUS_USB;
return MEDIA_BUS_UNKNOWN;
}
我想创建一个功能与PCI返回装置(S)或USB总线:
const char *get_device(const enum bus desired_bus)
{
enum bus bus;
...................................................
for(i = 0; i < md->md_size; i++, md_ptr++) {
bus = get_bus(md_ptr->sys);
if((bus & desired_bus) == desired_bus)
return md_ptr->node;
}
并称之为函数返回设备(多个):
get_device(const enum bus desired_bus)
如果请求是用于与PCI或USB总线类型的设备:
get_device(MEDIA_BUS_PCI | MEDIA_BUS_USB);
它可以使用数学运算符,枚举?
设置时desired_bus到MEDIA_BUS_PCI | MEDIA_BUS_USB并检查:如果((bus&desired_bus)== desired_bus)获得输出,如果总线是pci或usb,则不起作用。 – user935420
@ user935420:发布实际使用过的代码(包括枚举定义)。但是这个注释中的代码中的desired_bus必须是枚举值之一。 –
确定使用实际代码 – user935420