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

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

    The "type" of a binary number is the same as any decimal, hex or octal number: int (or even char, short, long long).

    When you assign a constant, you can't assign it with 11011011 (curiously and unfortunately), but you can use hex. Hex is a little easier to mentally translate. Chunk in nibbles (4 bits) and translate to a character in [0-9a-f].

    0 讨论(0)
  • 2020-11-22 08:07

    You can also use inline assembly like this:

    int i;
    
    __asm {
        mov eax, 00000000000000000000000000000000b
        mov i,   eax
    }
    
    std::cout << i;
    

    Okay, it might be somewhat overkill, but it works.

    0 讨论(0)
  • 2020-11-22 08:08

    This thread may help.

    /* Helper macros */
    #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)
    
    /* User macros */
    #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))
    
    
    #include <stdio.h>
    
    int main(void)
    {
        // 261, evaluated at compile-time
        unsigned const number = B16(00000001,00000101);
    
        printf("%d \n", number);
        return 0;
    }
    

    It works! (All the credits go to Tom Torfs.)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 08:10

    Just use the standard library in C++:

    #include <bitset>
    

    You need a variable of type std::bitset:

    std::bitset<8ul> x;
    x = std::bitset<8>(10);
    for (int i = x.size() - 1; i >= 0; i--) {
          std::cout << x[i];
    }
    

    In this example, I stored the binary form of 10 in x.

    8ul defines the size of your bits, so 7ul means seven bits and so on.

    0 讨论(0)
  • 2020-11-22 08:11
    template<unsigned long N>
    struct bin {
        enum { value = (N%10)+2*bin<N/10>::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.

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