Shift bits or binary point of signal
Logic and Bit Operations
The Shift Arithmetic block can shift the bits or the binary point of an input signal, or both.
For example, shifting the binary point on an input of data type sfix(8)
,
by two places to the right and left, gives these decimal values.
Shift Operation  Binary Value  Decimal Value 

No shift (original number)  11001.011  –6.625 
Binary point shift right by two places  1100101.1  –26.5 
Binary point shift left by two places  110.01011  –1.65625 
This block performs arithmetic bit shifts on signed numbers.
Therefore, the block recycles the most significant bit for each bit
shift. Shifting the bits on an input of data type sfix(8)
,
by two places to the right and left, gives these decimal values.
Shift Operation  Binary Value  Decimal Value 

No shift (original number)  11001.011  –6.625 
Bit shift right by two places  11110.010  –1.75 
Bit shift left by two places  00101.100  5.5 
The block supports input signals of the following data types:
Input Signal  Supported Data Types 

u 

s 

The following rules determine the output data type:
Data Type of Input u  Output Data Type 

Floating point  Same as input u 
Builtin integer or fixed point 

The block parameters support the following data types:
Parameter  Supported Data Types 

Bits to shift: Number 

Binary points to shift 

For more information, see Data Types Supported by Simulink in the Simulink^{®} documentation.
The Shift Arithmetic block dialog box appears as follows:
Specify whether to enter the bits to shift on the dialog box or to inherit the values from an input port.
Specify the direction in which to shift bits: left, right, or bidirectional.
Specify a scalar, vector, or array of bit shift values. This
parameter is available when Bits to shift: Source is Dialog
.
If the direction is...  Then... 

Left or Right  Use positive integers to specify bit shifts. 
Bidirectional  Use positive integers for right shifts and negative integers for left shifts. 
Specify an integer number of places to shift the binary point of the input signal. A positive value indicates a right shift, while a negative value indicates a left shift.
Specify whether to produce a warning or error during simulation when the block contains an outofrange shift value. Options include:
None
— No warning or error
appears.
Warning
— Display a warning
in the MATLAB^{®} Command Window and continue the simulation.
Error
— Halt the simulation
and display an error in the Diagnostic Viewer.
For more information, see Simulation and Accelerator Mode Results for OutofRange Bit Shift Values.
Select this check box to include conditional statements in the
generated code that protect against outofrange bit shift values.
This check box is available when Bits to shift: Source is Input
port
.
For more information, see Code Generation for OutofRange Bit Shift Values.
Suppose that WL
is the
input word length. The shaded regions in the following diagram show
outofrange bit shift values for left and right shifts.
Similarly, the shaded regions in the following diagram show outofrange bit shift values for bidirectional shifts.
The diagnostic for outofrange bit shifts responds as follows, depending on the mode of operation:
Mode  Diagnostic for outofrange shift value  

None  Warning  Error  
Simulation  Do not report any warning or error.  Report a warning but continue simulation.  Report an error and stop simulation. 
Accelerator modes and code generation  Has no effect. 
Suppose that U
is the
input, WL
is the input word length, and Y
is
the output. The output for an outofrange bit shift value for left
shifts is as follows:
Similarly, the output for an outofrange bit shift value for right shifts is as follows:
For bidirectional shifts, the output for an outofrange bit shift value is as follows:
For the generated code, the method for handling outofrange bit shifts depends on the setting of Check for outofrange 'Bits to shift' in generated code.
Check Box Setting  Generated Code  Simulation Results Compared to Generated Code 

Selected  Includes conditional statements to protect against outofrange bit shift values.  Simulation and Accelerator mode results match those of code generation. 
Cleared  Does not protect against outofrange bit shift values. 

For right shifts on signed negative inputs, most C compilers use an arithmetic shift instead of a logical shift. Generated code for the Shift Arithmetic block depends on this compiler behavior.
The following modelmodel compares the behavior of right bit shifts using the dialog box versus the block input port.
The key block parameter settings of the Constant blocks are:
Block  Parameter  Setting 

Constant and Constant1  Constant value  124 
Output data type  int8  
Dynamic bit shift  Constant value  2 
Output data type  Inherit: Inherit from 'Constant value' 
The key block parameter settings of the Shift Arithmetic blocks are:
Block  Parameter  Setting 

Bit shift from dialog  Bits to shift: Source  Dialog 
Bits to shift: Direction  Right  
Bits to shift: Number  2  
Bit shift from input  Bits to shift: Source  Input port 
Bits to shift: Direction  Right 
The top Shift Arithmetic block takes an input of 124, which corresponds to 01111100 in binary format. Shifting the number of bits two places to the right produces 00011111 in binary format. Therefore, the block outputs 31.
The bottom Shift Arithmetic block performs the same operation as the top block. However, the bottom block receives the bit shift value through an input port instead of the dialog box. By supplying this value as an input signal, you can change the number of bits to shift during simulation.
The following modelmodel shows the effect of binary point shifts.
The key block parameter settings of the Constant blocks are:
Block  Parameter  Setting 

Constant and Constant1  Constant value  124 
Output data type  int8 
The key block parameter settings of the Shift Arithmetic blocks are:
Block  Parameter  Setting 

Shift binary point 3 places to the right  Bits to shift: Source  Dialog 
Bits to shift: Direction  Bidirectional  
Bits to shift: Number  0  
Binary points to shift: Number  3  
Shift binary point 3 places to the left  Bits to shift: Source  Dialog 
Bits to shift: Direction  Bidirectional  
Bits to shift: Number  0  
Binary points to shift: Number  –3 
The top Shift Arithmetic block takes an input of 124, which corresponds to 01111100 in binary format. Shifting the binary point three places to the right produces 01111100000 in binary format. Therefore, the top block outputs 995.
The bottom Shift Arithmetic block also takes an input of 124. Shifting the binary point three places to the left produces 01111.100 in binary format. Therefore, the bottom block outputs 15.5.
Direct Feedthrough  Yes 
Sample Time  Inherited from the driving block 
Scalar Expansion  Yes 
Dimensionalized  Yes 
Multidimensionalized  Yes, for the input port that specifies bit shift values 
ZeroCrossing Detection  No 