%% Bitwise Operations
% This example shows how to implement bitwise operations 
% by using Simulink blocks, Stateflow Charts, and MATLAB Function blocks.

% Copyright 2018 The MathWorks, Inc.

%% Simulink Bitwise-Operator Block
% To include a logical operation in your model, use the Bitwise Operator
% block from the *Logic and Bit Operations* library. 
%
% 1. Open example model <matlab:ex_bit_logic_SL |ex_bit_logic_SL|>.
open_system('ex_bit_logic_SL');
%%
% The Logical Operator blocks perform an AND operation in the model. To change
% the operation, double-click the block and set the *Operator* field to any
% of the operations in the menu. 
%
% 2. Double-click the block to open the Block Parameters dialog box.
%
% 3. To perform bitwise operations with a bit-mask, select 
%    *Use bit mask*.
set_param('ex_bit_logic_SL/Bitwise Operator','UseBitMask','on');
%%
% If another input uses bitwise operations, clear the *Use bit mask* 
% parameter and enter the number of input ports.
%
% 4. In the *Bit Mask* field, enter a decimal number. Use |bin2dec| or 
%    |hex2dec| to convert the input from binary or hexadecimal. In this example, 
%    enter |hex2dec('D9')|.
set_param('ex_bit_logic_SL/Bitwise Operator','BitMask','hex2dec(''D9'')');
%%
% 5. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_bit_logic_SL'')');
%%
% The code implementing the bitwise operator AND is in the |ex_bit_logic_SL_step| 
% function in |ex_bit_logic_SL.c|:
file = fullfile('ex_bit_logic_SL_ert_rtw','ex_bit_logic_SL.c');
rtwdemodbtype(file,'/* Exported block signals */', ...
    '/* Model initialize function */',1,0);
%% Stateflow Chart
% 1. Open example model <matlab:ex_bit_logic_SF |ex_bit_logic_SF|>.
open_system('ex_bit_logic_SF');
open_system('ex_bit_logic_SF/Bit_Logic'); 
%%
% 2. Right-click the Stateflow chart to open the chart *Properties*.
%
% 3. Verify that the *Enable C-bit operations* check box is selected. 
%    For more information, see
%    <docid:stateflow_ug#mw_15c70e5b-3e5b-4140-b6ab-4a2846ac4b78 Enable
%    C-bit operations>.
%%
% 4. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_bit_logic_SF'')');
%%
% The code implementing the bitwise operator AND is in the |ex_bit_logic_SF_step| 
% function in |ex_bit_logic_SF.c|:
file = fullfile('ex_bit_logic_SF_ert_rtw','ex_bit_logic_SF.c');
rtwdemodbtype(file,'/* Exported block signals */', ...
    '/* Model initialize function */',1,0);
%% MATLAB Function Block
% In this example, to show the MATLAB Function block method for 
% implementing bitwise logic into the generated code, use the bitwise |OR|, 
% '|'.
%
% 1. Open example model <matlab:ex_bit_logic_ML |ex_bit_logic_ML|>.
open_system('ex_bit_logic_ML');
%%
% 2. The MATLAB Function Block contains this function:
%
% <include>bit_logic.m</include>
%%
% 3. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_bit_logic_ML'')');
%%
% The code implementing the bitwise operator OR is in the |ex_bit_logic_ML_step| 
% function in |ex_bit_logic_ML.c|:
file = fullfile('ex_bit_logic_ML_ert_rtw','ex_bit_logic_ML.c');
rtwdemodbtype(file,'/* Exported block signals */', ...
    '/* Model initialize function */',1,0);








