Documentation Center

  • Trials
  • Product Updates

Shift Arithmetic

Shift bits or binary point of signal

Library

Logic and Bit Operations

Description

Supported Shift 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 OperationBinary ValueDecimal 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 OperationBinary ValueDecimal 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

Data Type Support

The block supports input signals of the following data types:

Input SignalSupported Data Types
u
  • Floating point

  • Built-in integer

  • Fixed point

s
  • Floating point

  • Built-in integer

  • Fixed-point integer

The following rules determine the output data type:

Data Type of Input uOutput Data Type
Floating pointSame as input u
Built-in integer or fixed point
  • Sign of u

  • Word length of u

  • Slope of u * 2^(max(binary points to shift))

  • Bias of u * 2^(max(binary points to shiftbits to shift)), for bit shifts where the direction is bidirectional or right

  • Bias of u * 2^(max(binary points to shift + bits to shift)), for bit shifts where the direction is left

The block parameters support the following data types:

ParameterSupported Data Types
Bits to shift: Number
  • Built-in integer

  • Fixed-point integer

Binary points to shift
  • Built-in integer

  • Fixed-point integer

For more information, see Data Types Supported by Simulink in the Simulink® documentation.

Parameters and Dialog Box

The Shift Arithmetic block dialog box appears as follows:

Bits to shift: Source

Specify whether to enter the bits to shift on the dialog box or to inherit the values from an input port.

Bits to shift: Direction

Specify the direction in which to shift bits: left, right, or bidirectional.

Bits to shift: Number

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 RightUse positive integers to specify bit shifts.
BidirectionalUse positive integers for right shifts and negative integers for left shifts.

Binary points to shift

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.

Diagnostic for out-of-range shift value

Specify whether to produce a warning or error during simulation when the block contains an out-of-range 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 Out-of-Range Bit Shift Values.

Check for out-of-range 'Bits to shift' in generated code

Select this check box to include conditional statements in the generated code that protect against out-of-range bit shift values. This check box is available when Bits to shift: Source is Input port.

For more information, see Code Generation for Out-of-Range Bit Shift Values.

Out-of-Range Bit Shifts

Definition of an Out-of-Range Bit Shift

Suppose that WL is the input word length. The shaded regions in the following diagram show out-of-range bit shift values for left and right shifts.

Similarly, the shaded regions in the following diagram show out-of-range bit shift values for bidirectional shifts.

The diagnostic for out-of-range bit shifts responds as follows, depending on the mode of operation:

ModeDiagnostic for out-of-range shift value
NoneWarningError
SimulationDo not report any warning or error.Report a warning but continue simulation.Report an error and stop simulation.
Accelerator modes and code generationHas no effect.

Simulation and Accelerator Mode Results for Out-of-Range Bit Shift Values

Suppose that U is the input, WL is the input word length, and Y is the output. The output for an out-of-range bit shift value for left shifts is as follows:

Similarly, the output for an out-of-range bit shift value for right shifts is as follows:

For bidirectional shifts, the output for an out-of-range bit shift value is as follows:

Code Generation for Out-of-Range Bit Shift Values

For the generated code, the method for handling out-of-range bit shifts depends on the setting of Check for out-of-range 'Bits to shift' in generated code.

Check Box SettingGenerated CodeSimulation Results Compared to Generated Code

Selected

Includes conditional statements to protect against out-of-range bit shift values.

Simulation and Accelerator mode results match those of code generation.

Cleared

Does not protect against out-of-range bit shift values.

  • For in-range values, simulation and Accelerator mode results match those of code generation.

  • For out-of-range values, the code generation results are compiler specific.

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.

Examples

Block Output for Right Bit Shifts

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:

BlockParameterSetting
Constant and Constant1Constant value124
Output data typeint8
Dynamic bit shiftConstant value2
Output data typeInherit: Inherit from 'Constant value'

The key block parameter settings of the Shift Arithmetic blocks are:

BlockParameterSetting
Bit shift from dialogBits to shift: SourceDialog
Bits to shift: DirectionRight
Bits to shift: Number2
Bit shift from inputBits to shift: SourceInput port
Bits to shift: DirectionRight

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.

Block Output for Binary Point Shifts

The following modelmodel shows the effect of binary point shifts.

The key block parameter settings of the Constant blocks are:

BlockParameterSetting
Constant and Constant1Constant value124
Output data typeint8

The key block parameter settings of the Shift Arithmetic blocks are:

BlockParameterSetting
Shift binary point 3 places to the rightBits to shift: SourceDialog
Bits to shift: DirectionBidirectional
Bits to shift: Number0
Binary points to shift: Number3
Shift binary point 3 places to the leftBits to shift: SourceDialog
Bits to shift: DirectionBidirectional
Bits to shift: Number0
Binary points to shift: Number3

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.

Characteristics

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

Zero-Crossing Detection

No

Was this topic helpful?