Using binary integer literals in C source

Technical Note 46979

Arkitekturer:

All

Komponent:

compiler

Uppdaterad:

2022-06-07 12:19

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.

Det här innehållet finns tyvärr inte på svenska.

Vår webbplats finns främst på vårt koncernspråk engelska, förutom det innehåll för investerare som vi är lagstadgade att kommunicera på svenska. Vi rekommenderar att du besöker vår globala webbplats på engelska för att få en bättre upplevelse.

Vi stöder inte längre Internet Explorer. För att få bästa möjliga upplevelse av iar.com rekommenderar vi att du uppgraderar till en modern webbläsare som Chrome eller Edge.