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

前端 未结 19 2750
梦如初夏
梦如初夏 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条回答
  •  猫巷女王i
    2020-11-22 08:16

    The smallest unit you can work with is a byte (which is of char type). You can work with bits though by using bitwise operators.

    As for integer literals, you can only work with decimal (base 10), octal (base 8) or hexadecimal (base 16) numbers. There are no binary (base 2) literals in C nor C++.

    Octal numbers are prefixed with 0 and hexadecimal numbers are prefixed with 0x. Decimal numbers have no prefix.

    In C++0x you'll be able to do what you want by the way via user defined literals.

提交回复
热议问题