Specified bitwise operation on inputs

Logic and Bit Operations

The Bitwise Operator block performs the bitwise operation that you specify on one or more operands. Unlike logic operations of the Logical Operator block, bitwise operations treat the operands as a vector of bits rather than a single value.

You can select one of the following bitwise operations:

Bitwise 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
modulo-2 addition according to the IEEE^{®} Standard for Logic Elements.

The output data type, which the block inherits from the driving block, must represent zero exactly. Data types that satisfy this condition include signed and unsigned integer data types.

The size of the block output depends on the number of inputs, the vector size, and the operator you select:

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 you do not specify a bit mask, the output is a scalar.

If you do specify a bit mask, 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 block performs the operation between corresponding elements of the vectors to produce a vector output.

Block behavior changes depending on whether you use a bit mask.

If the Use bit mask check box is... | The block accepts... | And you specify... | By using... |
---|---|---|---|

Selected | One input | Bit Mask | Any valid MATLAB^{®} expression, such as `2^5+2^2+2^0` for
the bit mask 00100101 |

Not selected | Multiple inputs, all having the same base data type | Number of input ports | Any positive integer greater than 1 |

You can also use character vectors to specify a hexadecimal
bit mask such as `{'FE73','12AC'}`

.

You can use the bit mask to set or clear a bit on the input.

To perform a... | Set the Operator parameter to... | And create a bit mask with... |
---|---|---|

Bit set | OR | A 1 for each corresponding input bit that you want to set to 1 |

Bit clear | AND | A 0 for each corresponding input bit that you want to set to 0 |

Suppose you want to set the fourth bit of an 8-bit input vector.
The bit mask would be 00010000, which you can specify as `2^4`

for
the **Bit Mask** parameter. To clear the bit, 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`

for
the **Bit Mask** parameter.

The Bitwise Operator block supports the following data types:

Built-in integer

Fixed point

Boolean

The block does not support floating-point data types or enumerated
data types. For more information, see Data Types Supported by Simulink in
the Simulink^{®} documentation.

**Operator**Specify the bitwise logical operator for the block operands.

**Use bit mask**Select to use the bit mask. Clearing this check box enables

**Number of input ports**and disables**Bit Mask**and**Treat mask as**.**Number of input ports**Specify the number of inputs. The default value is 1.

**Bit Mask**Specify the bit mask to associate with a single input. This parameter is available only when you select

**Use bit mask**.### Tip

Do not use a mask greater than 53 bits. Otherwise, an error message appears during simulation.

**Treat mask as**Specify whether to treat the mask as a real-world value or a stored integer. This parameter is available only when you select

**Use bit mask**.The encoding scheme is

=`V`

+`SQ`

, as described in Scaling (Fixed-Point Designer) in the Fixed-Point Designer™ documentation.`B`

`Real World Value`

treats the mask as.`V`

`Stored Integer`

treats the mask as.`Q`

The following model shows how the Bitwise Operator block works for unsigned inputs.

Each Constant block outputs an 8-bit unsigned integer (`uint8`

).
To determine the binary value of each Constant block output, use the `dec2bin`

function. The results for all
logic operations appear in the next table.

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 model shows how the Bitwise Operator block works for signed inputs.

Each Constant block outputs an 8-bit signed integer (`int8`

).
To determine the binary value of each Constant block output, use the `dec2bin`

function. The results for all
logic operations appear in the next table.

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 |

Data Types | Boolean | Base Integer | Fixed-Point |

Sample Time | Inherited from driving block |

Direct Feedthrough | Yes |

Multidimensional Signals | Yes |

Variable-Size Signals | No |

Zero-Crossing Detection | No |

Code Generation | Yes |

Was this topic helpful?