Blaze 3.9
Bitwise Shift


Vector/Vector Shift


Via the left-shift operator (i.e. operator<<()) and the right-shift operator (i.e. operator>>()) it is possible to perform an elementwise shift of a dense vector:

// ... Initializing the vectors
v3 = v1 << v2; // Elementwise left-shift of a dense column vector
v3 = v1 >> v2; // Elementwise right-shift of a dense column vector
Efficient implementation of an arbitrary sized vector.
Definition: DynamicVector.h:223

Note that it is necessary that both operands have exactly the same dimensions. Violating this precondition results in an exception. Also note that it is only possible to shift vectors with the same transpose flag:

v1 << v2; // Compilation error: Cannot shift a column vector by a row vector
v1 << trans( v2 ); // OK: Shifting a column vector by another column vector
decltype(auto) trans(const DenseMatrix< MT, SO > &dm)
Calculation of the transpose of the given dense matrix.
Definition: DMatTransExpr.h:766
constexpr bool columnVector
Transpose flag for column vectors.
Definition: TransposeFlag.h:58
constexpr bool rowVector
Transpose flag for row vectors.
Definition: TransposeFlag.h:73

Furthermore, it is possible to use different element types in the two vector operands, but shifting two vectors with the same element type is favorable due to possible vectorization of the operation:

blaze::DynamicVector<unsigned int> v1( 100UL ), v2( 100UL ), v3;
// ... Initialization of the vectors
v3 = v1 << v2; // Vectorized left-shift of an unsigned int vector


Matrix/Matrix Shift


The left-shift operator (i.e. operator<<()) and the right-shift operator (i.e. operator>>()) can also be used to perform an elementwise shift of a dense matrix:

// ... Initializing the matrices
M3 = M1 << M2; // Elementwise left-shift of a dense column-major matrix
M3 = M1 >> M2; // Elementwise right-shift of a dense column-major matrix
Efficient implementation of a dynamic matrix.
Definition: DynamicMatrix.h:242
constexpr bool rowMajor
Storage order flag for row-major matrices.
Definition: StorageOrder.h:71
constexpr bool columnMajor
Storage order flag for column-major matrices.
Definition: StorageOrder.h:99

Note that it is necessary that both operands have exactly the same dimensions. Violating this precondition results in an exception. It is possible to use any combination of row-major and column-major matrices. Note however that in favor of performance using two matrices with the same storage order is favorable. The same argument holds for the element type: While it is possible to use matrices with different element type, using two matrices with the same element type potentially leads to better performance due to vectorization of the operation.

blaze::DynamicMatrix<unsigned int> M1( 50UL, 70UL ), M2( 50UL, 70UL ), M3;
// ... Initialization of the matrices
M3 = M1 << M2; // Vectorized left-shift of an unsigned int matrix


Scalar Shift


It is also possible to uniformly shift all elements of a dense vector or dense matrix by means of a scalar, which has the same effect as shifting by means of a uniform vector or matrix (see UniformVector and UniformMatrix). In Blaze it is possible to use all built-in/fundamental data types except bool as scalar values. Examples:

blaze::DynamicVector<unsigned int> v1{ 3, 2, 5, 4, 1, 6 };
// Uniform left-shift by one bit of all elements of v1; Results in
//
// ( 6, 4, 10, 8, 2, 12 )
//
{ 4, 1, 6 } };
// Uniform left-shift by one bit of all elements of M1; Results in
//
// ( 6, 4, 10 )
// ( 8, 2, 12 )
//


Previous: Bitwise Operations     Next: Bitwise AND