The Logic and Bit Operations Library
6.2 The Bit Operations Group Sub − Library
The Bit Operations Group Sub−Library contains the blocks described in Subsections 6.2.1 through 6.2.5 below.
TABLE 6.4 Operator parameters for the Compare to Constant block
Operator Action
== Determine whether the input is equal to the specified constant
~= Determine whether the input is not equal to the specified constant
< Determine whether the input is less than the specified constant
<= Determine whether the input is less than or equal to the specified constant
> Determine whether the input is greater than the specified constant
>= Determine whether the input is greater than or equal to the specified constant
A∗B
6.2.1 The Bit Set Block
The Bit Set block sets the specified bit of the stored integer to one. Scaling is ignored. We specify the bit to be set to one with the Index of bit parameter. Bit zero is the least significant bit.
Example 6.7
We will express the row vector in 8−bit binary form, and using the Bit Set block we will create a model that will convert this vector to . The converted vector will be displayed in binary form.
The model is shown in Figure 6.10.
Figure 6.10. Model for Example 6.7 For this model we have configured the blocks as follows:
Constant block − Constant value: − Signal data types: uint(8) Set bit block − Function block Parameters, Index bit: [0 2 1 3]
Sink block parameters − Display 1 and Display 2 blocks, Format: Binary (Stored Integer) 12 8 5 7
[ ]
13 12 7 15
[ ]
12 8 5 7
[ ]
6.2.2 The Bit Clear Block
The Bit Clear block sets the specified bit, given by its index, of the stored integer to zero. Scaling is ignored. We can specify the bit to be set to zero with the Index of bit parameter. Bit zero is the least significant bit.
Example 6.8
We will express the row vector in 8−bit binary form and using the Bit Clear block we will create a model that will convert this vector to . We will display the converted vec-tor in binary form.
The model is shown in Figure 6.11.
Figure 6.11. Model for Example 6.8 For this model we have configured the blocks as follows:
Constant block - Constant value: - Signal data types: uint(8) Clear bit block - Function block Parameters, Index bit: 3
Sink block parameters - Display 1 and Display 2 blocks, Format: Binary (Stored Integer) 14 8 5 12
[ ]
6 0 5 4
[ ]
14 8 5 12
[ ]
6.2.3 The Bitwise Operator Block
The Bitwise Operator block performs the specified bitwise operation on its operands. Unlike the logic operations performed by the Logical Operator block described in Section 2.12, Chapter 2, Page 2−18, bitwise operations treat the operands as a vector of bits rather than a single number.
The operations are listed 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 and vice-versa
The Bitwise Operator block cannot be used for shift operations. Shift operations are described in Subsection 6.2.4.
The size of the output of the Bitwise Operator block depends on the number of inputs, their vec-tor size, and the selected operavec-tor. For a single vecvec-tor 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. 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 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. If we do not select the Use bit mask check box, the block will accept multiple inputs. We select the number of input ports from the Number of input ports parameter. The input data types must be identical. For more information on the Bitwise Operator block please refer to the Simulink Help menu for this block.
Example 6.9
We will create a model containing a 3−input Bitwise AND block, a 3−input Bitwise NOR block, and a 2−input Bitwise XOR block. The inputs to the Bitwise AND and Bitwise NOR blocks are:
The inputs to the Bitwise XOR block are:
The model is shown in Figure 6.12.
Figure 6.12. Model for Example 6.9 For the model of Figure 6.12 we have entered:
Constant blocks 1, 2, and 3 − Constant value: , , and respectively − Signal data types: uint8
Bitwise AND block − Operator: AND, Number of input ports: 3 Bitwise NOR block − Operator: NOR, Number of input ports: 3 Bitwise XOR block − Operator: XOR, Number of input ports: 2
Data type Conversion 1, 2, and 3 blocks - Output data type mode: Specify via dialog− Output data type: ‘double’ - Input and output to have equal: Stored Integer (SI)
(152)10 = (10011000)2 (141)10 = (10001101)2 ( )75 10 = (01001011)2
(152)10 = (10011000)2 (141)10 = (10001101)2
[152] [141] [ ]75
Display 2, 4, and 6 blocks − Format: binary (Stored Integer)
A binary value used to selectively screen out or let through certain bits in a data value. Masking is performed by using a logical operator (AND, OR, XOR, NOT) to combine the mask and the data value. For example, the mask , when used with the AND operator, removes (masks off) the two uppermost bits in a data value but does not affect the rest of the value.
If we select the Use bit mask check box, then a single input is associated with the bit mask that we have specified with the Bit Mask parameter. We can specify the bit mask using any MATLAB expression in the Command Window. For example, we can specify the bit mask as . For long strings, we can use hexadecimal bit masks, e.g., [‘DA87’], and [‘E2F9B’]. If the bit mask is larger than the input signal data type, it is ignored.
We can use the bit mask to perform a bit set or a bit clear on the input. To perform a bit set, we set the Operator parameter list to OR and create a bit mask with a 1 for each corresponding input bit that we want to set to 1. To perform a bit clear, we set the Operator parameter list to AND and create a bit mask with a 0 for each corresponding input bit that we want to set to 0.
6.2.4 The Shift Arithmetic Block
The Shift Arithmetic block is be used to shift the bits or the binary point of a binary word, or both. This block performs arithmetic bit shifts on signed numbers. Therefore, the most significant bit is recycled for each bit shift. If the bits and the binary point are to be shifted to the left, we specify negative values.
Example 6.10
We will create a model with a Shift Arithmetic block with inputs decimal and decimal to display the outputs when both of these numbers are shifted left by 3 bits and the binary point is shifted left by 2 bits.
The model is shown in Figure 6.13 where the Constant blocks have been set for Output data type sfix(12) and the output scaling value 2^−3. Since it is specified that the bits and the binary point are to be shifted to the left, in the Shift Arithmetic block we enter the values and respec-tively. All three display blocks have been set for binary (Stored Integer) format. We can check the Shift Arithmetic block outputs as follows:
00111111
Figure 6.13. Model for Example 6.10
and after shifting 3 bits to the left and the binary point 2 places to the left we obtain
Likewise,
and after shifting 3 bits to the left and the binary point 2 places to the left we obtain
6.2.5 The Extract Bits Block
The Extract Bits block allows us to output a contiguous selection of bits from the stored integer value of the input signal. The Bits to extract parameter defines the method by which we select the output bits. We select Upper half to output the half of the input bits that contain the most signif-icant bit. If there is an odd number of bits in the input signal, the number of output bits is given by
* (6.1)
We select Lower half to output the half of the input bits that contain the least significant bit. If there is an odd number of bits in the input signal, the number of output bits is given by relation (6.1).
* The notation Ceil( ) rounds “up”, e.g., ceil(4.6) rounds to 5, and ceil(-4.6) rounds to -4. The notation Floor( ) +32.75
( )10 = (0001 0000 0.110)2
1000 .0011 0000
48.875
(– )10 = (–0001 1000 0.111)2 = (1110 0111 1.001)2s complement
0011 .1100 1000
Number of output bits = ceil Number of input bits 2( ⁄ )
We select Range starting with most significant bit to output a certain number of the most signifi-cant bits of the input signal. We specify the number of most signifisignifi-cant bits to output in the Num-ber of bits parameter.
We select Range ending with least significant bit to output a certain number of the least signifi-cant bits of the input signal. We specify the number of least signifisignifi-cant bits to output in the Num-ber of bits parameter.
We select Range of bits to indicate a series of contiguous bits of the input to output in the Bit indices parameter. We indicate the range in [start end] format, and the indices of the input bits are labeled contiguously starting at 0 for the least significant bit.
Example 6.11
We will create a model with an Extract Bits block to accept the decimal number as the input and outputs the binary number .
The model is shown in Figure 6.14 where the Constant block is set for Signal data types − Output data type mode: uint16, the Display 1 block shows the given decimal number in binary form, and the Display 2 block shows the Upper Half of that binary number. Both display blocks have been set for binary (Stored Integer) format. Had we specified the display blocks for decimal (Stored Integer), Display 1 block would show the decimal number 65403 and Display 2 block would show the decimal value 255 which is equivalent to binary 1111 1111.
Figure 6.14. Model for Example 6.11