bitshift
Shifts the bits of an integer left or right.
Category: Language Fundamentals > Operators and Elementary Operations
Syntax
C = bitshift(A, k)
Shifts A by k bit positions: left for positive k, right for negative k.
Description
Shifting left by one doubles the value and shifting right by one halves it, discarding the fraction. So bitshift(1, 3) is 8 and bitshift(16, -2) is 4. Bits pushed out beyond the width of the class are lost: bitshift(uint8(255), 1) is 254.
Shifts make quick work of powers of two and of packing several small fields into one integer.
Input arguments
A- Array of nonnegative integers.
k- Number of positions. Positive shifts left, negative shifts right.
Output arguments
C- Shifted values, of the same class as
A.
Example
bitshift(1, 3)
bitshift(16, -2)
bitshift(uint8(1), [1 2 3])
bitshift(uint8(255), 1)
Output
ans = 8
ans = 4
ans = 1×3 uint8 row vector
2 4 8
ans = uint8
254