C++ binary constant/literal

后端 未结 7 2121
-上瘾入骨i
-上瘾入骨i 2020-12-14 21:09

I\'m using a well known template to allow binary constants

template< unsigned long long N >
struct binary
{
  enum { value = (N % 10) + 2 * binary<          


        
相关标签:
7条回答
  • 2020-12-14 21:55

    A simple #define works very well:

    #define HEX__(n) 0x##n##LU
    
    #define B8__(x) ((x&0x0000000FLU)?1:0)\
                   +((x&0x000000F0LU)?2:0)\
                  +((x&0x00000F00LU)?4:0)\
                   +((x&0x0000F000LU)?8:0)\
                   +((x&0x000F0000LU)?16:0)\
                   +((x&0x00F00000LU)?32:0)\
                   +((x&0x0F000000LU)?64:0)\
                   +((x&0xF0000000LU)?128:0)
    
    #define B8(d) ((unsigned char)B8__(HEX__(d)))
    #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
    #define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) + ((unsigned long)B8(db2)<<16) + ((unsigned long)B8(db3)<<8) + B8(dlsb))
    
    B8(011100111)
    B16(10011011,10011011)
    B32(10011011,10011011,10011011,10011011)
    

    Not my invention, I saw it on a forum a long time ago.

    0 讨论(0)
提交回复
热议问题