定义BIT0,BIT1,BIT2等没有的#define [英] Define BIT0, BIT1, BIT2, etc Without #define

查看:2528
本文介绍了定义BIT0,BIT1,BIT2等没有的#define的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,它可以定义BIT0,BIT1,BIT2在C ++中另一种方式,而不使用#define?

Is it possible in C++ to define BIT0, BIT1, BIT2 in another way in C++ without using #define?

#define BIT0 0x00000001
#define BIT1 0x00000002
#define BIT2 0x00000004

然后我拿同样的事情,让国掉那些位:

I then take the same thing and make states out of those bits:

#define MOTOR_UP   BIT0
#define MOTOR_DOWN BIT1

请注意:我使用的是32位而已,没有64位。我也用 setBit(flagVariable,BIT)(因此一个 clrBit 宏做相反)宏设置该位,然后比较位是否设置使用位运算符,如

Note: I am using 32 bits only, not 64 bits. I am also using a setBit(flagVariable, BIT) (consequently a clrBit macro to do the opposite) macro to set the bits then compare whether the bit is set using the bitwise operator such as

if (flagVariable & MOTOR_UP) { 
   // do something
   clrBit(flagVariable, MOTOR_UP);
}

有没有C ++中的类型已经包含这些位掩码?

Is there a type in C++ that already contains these bit masks?

推荐答案

如何

enum Bits
{
    BIT0    = 0x00000001,
    BIT1    = 0x00000004,
    BIT2    = 0x00000008,

    MOTOR_UP    = BIT0,
    MOTOR_DOWN  = BIT1
};

这篇关于定义BIT0,BIT1,BIT2等没有的#define的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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