Using binary integer literals in C source

Technical Note 46979

Architectures:

All

Component:

compiler

Updated:

6/7/2022 12:19 PM

Introduction

Binary integer literals (0b…) in C source are supported by newer versions of the IAR C/C++ Compiler®.

If your compiler version does not support binary integer literals in C, this technical note describes an alternative.

Discussion

Background

Binary integer literals (0b…) are supported in the C++ language but not in the C language.

IAR Systems has added a compiler extension to support binary integer literals in C source code. (Legacy versions of the compiler do not support binary integer literals.)

There is an example project for IAR Embedded Workbench® for Arm 9.20.1 here.

Solution

The solution is to use a #define directive for the wanted binary integer literal. For example, the binary integer value of 5 can be implemented as:

#define b0000000000000101 0x0005

Add such a #define directive for each binary integer value in use in the application.

Template files

To simplify using #define directives to set binary integer values, download this archive. The .zip file contains two files:

  • Binary_values_8_bit.txt holds defines for all positive 8-bit binary integer values.
  • Binary_values_16_bit.txt holds defines for all positive 16-bit binary integer values.

Using the template files

Copy each needed #define directive from the text file and add it to a header file in your project.

Please note that the contents of the text files are not intended to be used in their entirety in the header files. Using all the contents would increase the build time significantly.

Conclusion

Binary integer literals (0b…) are not supported in legacy versions of the C compiler, but there are workarounds. 

All product names are trademarks or registered trademarks of their respective owners.

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.