Can I use a binary literal in C or C++?

前端 未结 19 2659
梦如初夏
梦如初夏 2020-11-22 07:40

I need to work with a binary number.

I tried writing:

const x = 00010000;

But it didn\'t work.

I know that I can use an hex

19条回答
  •  梦如初夏
    2020-11-22 08:20

    A few compilers (usually the ones for microcontrollers) has a special feature implemented within recognizing literal binary numbers by prefix "0b..." preceding the number, although most compilers (C/C++ standards) don't have such feature and if it is the case, here it is my alternative solution:

    #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;
    

    Disadvantages (it's not such a big ones):

    • The binary numbers have to be grouped 4 by 4;
    • The binary literals have to be only unsigned integer numbers;

    Advantages:

    • 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);

提交回复
热议问题