Main Content

Control Signed Left Shifts in Generated Code

If you have an Embedded Coder® license, you can control whether MATLAB® Coder™ replaces multiplications by powers of two with signed left bitwise shifts. Some coding standards, such as MISRA™, do not allow bitwise operations on signed integers.

By default, MATLAB Coder replaces multiplication by powers of two with signed left shifts. Here is an example of generated C code that uses a signed left shift for multiplication by eight.

i <<= 3;

To increase the likelihood of generating MISRA C:2012 compliant code, disable the replacement of multiplication by powers of two with signed left shifts. Here is an example of generated C code that does not use a signed left shift for multiplication by eight:

i = i * 8;

Control Signed Left Shifts Using the MATLAB Coder App

  1. On the Generate Code page, to open the Generate dialog box, click the Generate arrow .

  2. Set Build type to one of the following:

    • Source Code

    • Static Library (.lib)

    • Dynamic Library (.dll)

    • Executable (.exe)

  3. Click More Settings.

  4. On the Code Appearance tab, select or clear the Use signed shift left for fixed-point operations and multiplication by powers of 2 check box.

Control Signed Left Shifts Using the Command-Line Interface

  1. Create a code configuration object for 'lib', 'dll', or 'exe'. For example:

    cfg = coder.config('lib','ecoder',true); % or dll or exe
    

  2. Set the EnableSignedLeftShifts property to true or false. For example:

    cfg.EnableSignedLeftShifts = false;
    

See Also

Related Topics