Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to express binary numbers with some sort of separation between digits in C?

Tags:

c

In reality, we often express long binary numbers in the form of 0000 1111 0011 1100 to make reading easier. And very often, especially in dealing with chip-chip communications in embedded systems (like I2C), we have to perform bitwise logical operations (usually bitwise OR) to abstract informations for decisions.

Yet as far as I know, in the C standard, no spacing or any form of separation between digits is allowed, which makes it more difficult to read the long binary numbers. Using hexadecimal number is somehow worse since it masks the binary number.

Is there a conforming way to express long binary numbers with separators (such as 0b0000 1111 0011 1100) in C?

like image 733
OW1TY2 Avatar asked Dec 21 '25 06:12

OW1TY2


1 Answers

Yes, with the newly released C standard (C23), you can use ' as a digit separator:

uint16_t x = 0b0000'1111'0011'1100;

Digit separators are ignored when determining the value of the constant.

like image 157
Ted Lyngmo Avatar answered Dec 23 '25 23:12

Ted Lyngmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!