我需要处理一个二进制数。

我试着写:

const char x = 00010000;

但这并没有起作用。

我知道我可以使用与00010000值相同的十六进制数,但我想知道在c++中是否有用于二进制数的类型,如果没有,是否有其他解决方案?


当前回答

你可以使用二进制字面值。它们是在c++ 14中标准化的。例如,

int x = 0b11000;

GCC中的支持

GCC中的支持开始于GCC 4.3(见https://gcc.gnu.org/gcc-4.3/changes.html),作为C语言家族的扩展(见https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html#C-Extensions),但从GCC 4.9开始,它现在被认为是c++ 14的特性或扩展(见GCC二进制文字和c++ 14的差异?)

Visual Studio中的支持

Visual Studio中的支持开始于Visual Studio 2015预览版(参见https://www.visualstudio.com/news/vs2015-preview-vs#C++)。

其他回答

你可以尝试使用bool类型的数组:

bool i[8] = {0,0,1,1,0,1,0,1}

c++提供了一个名为std::bitset的标准模板。如果你喜欢,可以试试。

你也可以像这样使用内联汇编:

int i;

__asm {
    mov eax, 00000000000000000000000000000000b
    mov i,   eax
}

std::cout << i;

好吧,这可能有点过分,但确实有效。

一些编译器(通常是微控制器的编译器)有一个特殊的功能,通过数字前面的前缀“0b…”来识别二进制数字,尽管大多数编译器(C/ c++标准)没有这样的功能,如果是这样的话,这里是我的替代解决方案:

#define B_0000    0
#define B_0001    1
#define B_0010    2
#define B_0011    3
#define B_0100    4
#define B_0101    5
#define B_0110    6
#define B_0111    7
#define B_1000    8
#define B_1001    9
#define B_1010    a
#define B_1011    b
#define B_1100    c
#define B_1101    d
#define B_1110    e
#define B_1111    f

#define _B2H(bits)    B_##bits
#define B2H(bits)    _B2H(bits)
#define _HEX(n)        0x##n
#define HEX(n)        _HEX(n)
#define _CCAT(a,b)    a##b
#define CCAT(a,b)   _CCAT(a,b)

#define BYTE(a,b)        HEX( CCAT(B2H(a),B2H(b)) )
#define WORD(a,b,c,d)    HEX( CCAT(CCAT(B2H(a),B2H(b)),CCAT(B2H(c),B2H(d))) )
#define DWORD(a,b,c,d,e,f,g,h)    HEX( CCAT(CCAT(CCAT(B2H(a),B2H(b)),CCAT(B2H(c),B2H(d))),CCAT(CCAT(B2H(e),B2H(f)),CCAT(B2H(g),B2H(h)))) )

// Using example
char b = BYTE(0100,0001); // Equivalent to b = 65; or b = 'A'; or b = 0x41;
unsigned int w = WORD(1101,1111,0100,0011); // Equivalent to w = 57155; or w = 0xdf43;
unsigned long int dw = DWORD(1101,1111,0100,0011,1111,1101,0010,1000); //Equivalent to dw = 3745774888; or dw = 0xdf43fd28;

缺点(不是什么大缺点):

二进制数必须按4 × 4分组; 二进制字面值只能是无符号整数;

优点:

Total preprocessor driven, not spending processor time in pointless operations (like "?.. :..", "<<", "+") to the executable program (it may be performed hundred of times in the final application); It works "mainly in C" compilers and C++ as well (template+enum solution works only in C++ compilers); It has only the limitation of "longness" for expressing "literal constant" values. There would have been earlyish longness limitation (usually 8 bits: 0-255) if one had expressed constant values by parsing resolve of "enum solution" (usually 255 = reach enum definition limit), differently, "literal constant" limitations, in the compiler allows greater numbers; Some other solutions demand exaggerated number of constant definitions (too many defines in my opinion) including long or several header files (in most cases not easily readable and understandable, and make the project become unnecessarily confused and extended, like that using "BOOST_BINARY()"); Simplicity of the solution: easily readable, understandable and adjustable for other cases (could be extended for grouping 8 by 8 too);

二进制常数将在C23中标准化。在撰写本文时,最新的C2x标准草案的6.4.4.1/4中提到了拟议的符号:

[…二进制常数由前缀0b或0b后跟一组数字0或1组成。