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

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

    template
    struct bin {
        enum { value = (N%10)+2*bin::value };
    } ;
    
    template<>
    struct bin<0> {
        enum { value = 0 };
    } ;
    
    // ...
        std::cout << bin<1000>::value << '\n';
    

    The leftmost digit of the literal still has to be 1, but nonetheless.

提交回复
热议问题