| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
Logic and Bit Operations

The Bitwise Operator block performs the specified bitwise operation on its operands.
Unlike the logic operations performed by the Logical Operator block, bitwise operations treat the operands as a vector of bits rather than a single number. You select the bitwise Boolean operation from the Operator parameter list. The supported operations are given below.
| Operation | Description |
|---|---|
AND | TRUE if the corresponding bits are all TRUE |
OR | TRUE if at least one of the corresponding bits is TRUE |
NAND | TRUE if at least one of the corresponding bits is FALSE |
NOR | TRUE if no corresponding bits are TRUE |
XOR | TRUE if an odd number of corresponding bits are TRUE |
NOT | TRUE if the input is FALSE (available only for single input) |
The Bitwise Operator block does not support shift operations. For shift operations, use the Shift Arithmetic block.
When configured as a multi-input XOR gate, this block performs an addition modulo-two operation as mandated by the IEEE® Standard for Logic Elements.
The size of the output of the Bitwise Operator block depends on the number of inputs, their vector size, and the selected operator:
The NOT operator accepts only one input, which can be a scalar or a vector. If the input is a vector, the output is a vector of the same size containing the bitwise logical complements of the input vector elements.
For a single vector input, the block applies the operation (except the NOT operator) to all elements of the vector. If a bit mask is not specified, then the output is a scalar. If a bit mask is specified, then the output is a vector.
For two or more inputs, the block performs the operation between all of the inputs. If the inputs are vectors, the operation is performed between corresponding elements of the vectors to produce a vector output.
Tip If you do not select the Use bit mask check box, then the block can accept multiple inputs. You select the number of input ports from the Number of input ports parameter. All inputs must have the same base data type. |
If you select the Use bit mask check box, then a single input is associated with the bit mask you specify from the Bit Mask parameter. You specify the bit mask using any valid MATLAB expression. For example, you can specify the bit mask 00100101 as 2^5+2^2+2^0. Alternatively, you can use strings to specify a hexadecimal bit mask such as {'FE73','12AC'}. If the bit mask is larger than the input signal data type, then it is ignored.
Tip The output data type, which is inherited from the driving block, should represent zero exactly. Data types that satisfy this condition include signed and unsigned integers and any floating-point data type. |
You can use the bit mask to perform a bit set or a bit clear on the input. To perform a bit set, set the Operator parameter list to OR and create a bit mask with a 1 for each corresponding input bit that you want to set to 1. To perform a bit clear, set the Operator parameter list to AND and create a bit mask with a 0 for each corresponding input bit that you want to set to 0.
Suppose you want to perform a bit set on the fourth bit of an 8-bit input vector. The bit mask would be 00010000, which you can specify as 2^4 in the Bit mask parameter. To perform a bit clear, the bit mask would be 11101111, which you can specify as 2^7+2^6+2^5+2^3+2^2+2^1+2^0 in the Bit mask parameter.
The Bitwise Operator block supports Simulink integer, fixed-point, and Boolean data types. The block does not support true floating-point data types or enumerated data types.

The bitwise logical operator associated with the specified operands.
Specify if the bit mask is used (single input only).
The number of inputs.
The bit mask to associate with a single input. The Bit Mask parameter is converted from a double to the input data type offline using round-to-nearest and saturation.
Treat the mask as a real-world value or a stored integer.
Based on the encoding scheme described in Scaling in the Simulink Fixed Point documentation, Real World Value treats the mask as V = SQ + B where S is the slope and B is the bias. Stored Integer treats the mask as a stored integer, Q.
The following fixed-point model shows how the Bitwise Operator block works when inputs are unsigned.

Each Constant block outputs an 8-bit unsigned integer (uint8). The results for all logic operations are shown below.
| Operation | Binary Value | Decimal Value |
|---|---|---|
AND | 00101000 | 40 |
OR | 11111101 | 253 |
NAND | 11010111 | 215 |
NOR | 00000010 | 2 |
XOR | 11111000 | 248 |
NOT | N/A | N/A |
The following fixed-point model shows how the Bitwise Operator block works when inputs are signed.

Each Constant block outputs an 8-bit signed integer (int8). The results for all logic operations are shown below.
| Operation | Binary Value | Decimal Value |
|---|---|---|
AND | 01000000 | 64 |
OR | 11111011 | -5 |
NAND | 10111111 | -65 |
NOR | 00000100 | 4 |
XOR | 11000010 | -62 |
NOT | N/A | N/A |
Direct Feedthrough | Yes |
Scalar Expansion | Yes, of inputs |
Multidimensionalized | Yes |
![]() | Bit Set | Block Support Table | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |