| MATLAB® | ![]() |
C = bitshift(A, k)
C = bitshift(A, k, n)
C = bitshift(A, k) returns the value of A shifted by k bits. Input argument A must be an unsigned integer or an array of unsigned integers. Shifting by k is the same as multiplication by 2^k. Negative values of k are allowed and this corresponds to shifting to the right, or dividing by 2^abs(k) and truncating to an integer. If the shift causes C to overflow the number of bits in the unsigned integer class of A, then the overflowing bits are dropped.
C = bitshift(A, k, n) causes any bits that overflow n bits to be dropped. The value of n must be less than or equal to the length in bits of the unsigned integer class of A (e.g., n <= 32 for uint32).
Instead of using bitshift(A, k, 8) or another power of 2 for n, consider using bitshift(uint8(A), k) or the appropriate unsigned integer class for A.
Shifting 1100 (12, decimal) to the left two bits yields 110000 (48, decimal).
C = bitshift(12, 2)
C =
48Repeatedly shift the bits of an unsigned 16 bit value to the left until all the nonzero bits overflow. Track the progress in binary:
a = intmax('uint16');
disp(sprintf( ...
'Initial uint16 value %5d is %16s in binary', ...
a, dec2bin(a)))
for k = 1:16
a = bitshift(a, 1);
disp(sprintf( ...
'Shifted uint16 value %5d is %16s in binary',...
a, dec2bin(a)))
endbitand, bitcmp, bitget, bitmax, bitor, bitset, bitxor, fix
![]() | bitset | bitxor | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |