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

前端 未结 19 2752
梦如初夏
梦如初夏 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:08

    You can use the function found in this question to get up to 22 bits in C++. Here's the code from the link, suitably edited:

    template< unsigned long long N >
    struct binary
    {
      enum { value = (N % 8) + 2 * binary< N / 8 > :: value } ;
    };
    
    template<>
    struct binary< 0 >
    {
      enum { value = 0 } ;
    };
    

    So you can do something like binary<0101011011>::value.

提交回复
热议问题