C++ binary constant/literal

后端 未结 7 2158
-上瘾入骨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:44

    You can add more non-type template parameters to "simulate" additional bits:

    // Utility metafunction used by top_bit.
    template 
    struct compare {
        enum { value = N1 > N2 ? N1 >> 1 : compare::value };
    };
    
    // This is hit when N1 grows beyond the size representable
    // in an unsigned long long.  It's value is never actually used.
    template
    struct compare<0, N2> {
        enum { value = 42 };
    };
    
    // Determine the highest 1-bit in an integer.  Returns 0 for N == 0.
    template 
    struct top_bit {
        enum { value = compare<1, N>::value };
    };
    
    template 
    struct binary {
        enum {
            value =
                (top_bit::value>::value << 1) * binary::value +
                binary::value
        };
    };
    
    template 
    struct binary {
        enum { value = (N1 % 10) + 2 * binary::value };
    };
    
    template <>
    struct binary<0> {
        enum { value = 0 } ;
    };
    

    You can use this as before, e.g.:

    binary<1001101>::value
    

    But you can also use the following equivalent forms:

    binary<100,1101>::value
    binary<1001,101>::value
    binary<100110,1>::value
    

    Basically, the extra parameter gives you another 20 bits to play with. You could add even more parameters if necessary.

    Because the place value of the second number is used to figure out how far to the left the first number needs to be shifted, the second number must begin with a 1. (This is required anyway, since starting it with a 0 would cause the number to be interpreted as an octal number.)

提交回复
热议问题