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

我试着写:

const char x = 00010000;

但这并没有起作用。

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


当前回答

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

int i;

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

std::cout << i;

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

其他回答

c++的过度工程思维已经在这里的其他答案中得到了很好的解释。以下是我尝试用C,保持简单的心态来做这件事:

unsigned char x = 0xF; // binary: 00001111

二进制数的“类型”与任何十进制、十六进制或八进制数相同:int(甚至char, short, long long)。

当你给一个常数赋值时,你不能用11011011赋值(奇怪而不幸的是),但你可以使用hex。海克斯更容易从心理上理解。块在啃(4位)和翻译成一个字符在[0-9a-f]。

只需使用c++中的标准库:

#include <bitset>

你需要一个std::bitset类型的变量:

std::bitset<8ul> x;
x = std::bitset<8>(10);
for (int i = x.size() - 1; i >= 0; i--) {
      std::cout << x[i];
}

在本例中,我将10的二进制形式存储在x中。

8ul定义了位的大小,所以7ul意味着7位等等。

C语言没有用于纯二进制数的原生表示法。这里最好是八进制(例如07777)或十六进制(例如0xfff)。

一些编译器(通常是微控制器的编译器)有一个特殊的功能,通过数字前面的前缀“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);