Simulink Reference | ![]() ![]() |
Logically mask, invert, or shift the bits of an unsigned integer signal
Library
Description
The Bitwise Logical Operator performs any of a set of logical masking (AND, OR, XOR), inversion (NOT), and shifting (SHIFT_LEFT, SHIFT_RIGHT) operations on the bits of an unsigned integer signal. The block's parameter dialog lets you choose the operation to perform. You can use the Bitwise Logical Operator block to perform bitwise operations on arrays of unsigned integer signals.
Masking Operations
The Bitwise Logical Operator's masking operations (AND, OR, XOR) logically combine each bit of the input signal with the corresponding bit of a constant operand called the mask. You specify the mask's value and the logical operation via the block's parameter dialog. The mask and the logical operation determine the value of each bit of the output signal as follows.
Operation |
Mask Bit |
Input Bit |
Output Bit |
AND |
1 |
1 |
1 |
|
1 |
0 |
0 |
|
0 |
1 |
0 |
|
0 |
0 |
0 |
OR |
1 |
1 |
1 |
|
0 |
1 |
1 |
|
1 |
0 |
1 |
|
0 |
0 |
0 |
XOR |
1 |
1 |
0 |
|
1 |
0 |
1 |
|
0 |
1 |
1 |
|
0 |
0 |
0 |
A Bitwise Operator block accepts arrays for both signals and masks. In general, the mask must have the same dimensionality as the input signal, i.e., a 5-by-4 input signal requires a 5-by-4 mask. The block applies each element of the mask to the corresponding input element. The following exceptions exist to the general rule that the input and the mask must have the same dimensionality:
When selecting a masking operation, use the Second operand field of the block's parameter dialog to specify the mask or masks. You can enter any MATLAB expression that evaluates to a scalar, matrix, or cell array. Use strings in your mask expression to specify hexadecimal values (e.g., 'FFFF'
).
If necessary, the block truncates the high-order bits of the mask value to fit the word size of the input signal's data type. For example, suppose you specify the mask value as 'FF00'
and
the input signal is of type uint8
. The block truncates the specified value to '00'
.
You can use matrices to specify hexadecimal masks, but beware of the pitfalls of such an approach. For example, the MATLAB expression['00' 'FF']
represents a single string 'FF00'
rather than two strings. Similarly, the expression ['FFFF'; '0000']
represents two strings but the expression ['FFFF'; '00']
is invalid and hence causes MATLAB to signal an error. You can avoid these pitfalls by always using cell arrays to specify hexadecimal values, or to mix decimal and hexadecimal values, for masks. For example, the following model
uses a cell array ({'F0' '0F'}
) to specify hexadecimal values for the masks for a two-element input vector.
Inversion Operation
The Bitwise Logical Operator's NOT operation inverts the bits of the input signal. In particular, it performs a one's complement operation on the input signal to produce an output signal each of whose bits is 1 if the corresponding input bit is 0 and vice versa.
Shift Operations
The Bitwise Logical Operator's shift operations, SHIFT_LEFT
and SHIFT_RIGHT
, shift the bits of the input signal left or right to produce the output signal. You specify the amount of the shift in the Second operand field of the block's parameter dialog. If you specify a shift amount that is greater than the word size of the input signal, the block uses the input word size as the shift amount, resulting in a zero output signal. The dimensionality rules that apply to masks and inputs also apply to shift factors and inputs.
Data Type Support
The Bitwise Logical Operator accepts real-valued inputs of any of the unsigned integer data types: uint8
, uint16
, uint32
. All the elements of a vector input must be of the same data type. The output signal is of the same data type as the input.
Parameters and Dialog Box
NOT
, this parameter does not appear.) Characteristics
Sample Time |
Inherited from driving block |
Scalar Expansion |
Of inputs and Second operand parameter |
Dimensionalized |
Yes |
States |
None |
Zero Crossing |
No |
Direct Feedthrough |
Yes |
![]() | Band-Limited White Noise | Bus Creator | ![]() |