%% Control Operator Type in Generated Code
% This example shows how to optimize the generated code by controlling the operator type in the code.
% You can generate code with Bitwise or Logical operators or a combination of both for inputs with Boolean data type.
% Selecting bitwise operators in the code might improve ROM efficiency when the code contains bitwise operators.
%
%
% When you select a value for the *Operator to represent Bitwise and
% Logical Operator blocks* parameter, the generated code maintains the operators as modelled or
% converts all operators into either bitwise or logical operators independent of the
% modeling blocks.
%
%
% *NOTE:*
% This example requires Embedded Coder&reg;.

% Copyright 2019 The MathWorks, Inc.

%% Example Model
% In the model <matlab:logicalandbitwise>, a Logical AND block connects to a Bitwise AND block.
model = 'logicalandbitwise';
open_system(model);

%% Generate Code as modeled
% Use your system temporary folder for
% building and inspection process.
currentDir = pwd;

%%
% # Open the Configuration Parameters dialog box.
% # On the *Optimization* pane, set the *Operator to represent Bitwise and
% Logical Operator blocks* parameter to |Same as modeled|.
%
% Alternatively, use the command-line API to set the parameter value:
set_param(model, 'BitwiseOrLogicalOp', 'Same as modeled');
evalc('rtwbuild(model)');

%%
% View the generated code with operators in the code that matches the modeled blocks.
% Here is a portion of |logicalandbitwise.c|.
cfile = fullfile(currentDir,'logicalandbitwise_ert_rtw','logicalandbitwise.c');
rtwdemodbtype(cfile,'/* Model step function ','/* Model initialize function',1, 1);
%%
% The generated code contains both a logical |&| operator and a bitwise
% |&&| operator representing the Logical AND block and Bitwsie AND block in
% the code.

%% Generate Code with Logical Operators
% # Open the Configuration Parameters dialog box.
% # On the *Optimization* pane, set the *Operator to represent Bitwise and
% Logical Operator blocks* parameter to |Logical operator|.
%
% Alternatively, use the command-line API to set the parameter value:
set_param(model, 'BitwiseOrLogicalOp', 'Logical operator');
evalc('rtwbuild(model)');

%%
% View the generated code with only logical operators.
% Here is a portion of |logicalandbitwise.c|.
cfile = fullfile(currentDir,'logicalandbitwise_ert_rtw','logicalandbitwise.c');
rtwdemodbtype(cfile,'/* Model step function ','/* Model initialize function',1, 1);
%%
% The generated code contains only the logical |&&| operator. The Bitwise
% AND block in the model is converted into the logical |&&| operator in the code.

%% Generate Code with Bitwise Operators
% # Open the Configuration Parameters dialog box.
% # On the *Optimization* pane, set the *Operator to represent Bitwise and
% Logical Operator blocks* parameter to |Bitwise operator|.
%
% Alternatively, use the command-line API to set the parameter value:
set_param(model, 'BitwiseOrLogicalOp', 'Bitwise operator');
evalc('rtwbuild(model)');

%%
% View the generated code with only bitwise operators.
% Here is a portion of |logicalandbitwise.c|.
cfile = fullfile(currentDir,'logicalandbitwise_ert_rtw','logicalandbitwise.c');
rtwdemodbtype(cfile,'/* Model step function ','/* Model initialize function',1, 1);
%%
% The generated code contains only the bitwise |&| operator. The Logical
% AND block in the model is converted into the bitwise |&| operator in the code.

%%
% Close the model and code generation report.
bdclose(model)
rtwdemoclean;
cd(currentDir)