bitshift - Shift bits specified number of places - MATLAB (original) (raw)
Shift bits specified number of places
Syntax
Description
[intout](#bth5rcc-intout) = bitshift([A](#bth5rcc-A),[k](#bth5rcc-k))
returns A
shifted to the left by k
bits, equivalent to multiplying by 2k
. Negative values of k
correspond to shifting bits right or dividing by 2|k|
and rounding to the nearest integer towards negative infinity. Any overflow bits are truncated.
- If
A
is an array of signed integers, thenbitshift
returns the arithmetic shift results, preserving the signed bit whenk
is negative, and not preserving the signed bit whenk
is positive. - If
k
is positive, MATLAB® shifts the bits to the left and insertsk
0-bits on the right. - If
k
is negative andA
is nonnegative, then MATLAB shifts the bits to the right and inserts|
k
|
0-bits on the left. - If
k
is negative andA
is negative, then MATLAB shifts the bits to the right and inserts|
k
|
1-bits on the left.
[intout](#bth5rcc-intout) = bitshift([A](#bth5rcc-A),[k](#bth5rcc-k),[assumedtype](#bth5rcc-assumedtype))
assumes A
is of type assumedtype
.
Examples
Repeatedly shift the bits of an unsigned 8-bit value to the left until all the nonzero bits overflow.
a = intmax('uint8'); s1 = 'Initial uint8 value %5d is %08s in binary\n'; s2 = 'Shifted uint8 value %5d is %08s in binary\n'; fprintf(s1,a,dec2bin(a))
Initial uint8 value 255 is 11111111 in binary
for i = 1:8 a = bitshift(a,1); fprintf(s2,a,dec2bin(a)) end
Shifted uint8 value 254 is 11111110 in binary Shifted uint8 value 252 is 11111100 in binary Shifted uint8 value 248 is 11111000 in binary Shifted uint8 value 240 is 11110000 in binary Shifted uint8 value 224 is 11100000 in binary Shifted uint8 value 192 is 11000000 in binary Shifted uint8 value 128 is 10000000 in binary Shifted uint8 value 0 is 00000000 in binary
Find the shift for a number using different assumed integer types.
uintout = bitshift(6,5:7,'uint8')
intout = bitshift(6,5:7,'int8')
Use bitor
and bitshift
to pack four 8-bit bytes into the 32-bit integer they make up.
Create four bytes of data. Specify the data with hexadecimal literals, using the -u32
suffix to specify that the data should be stored as uint32
. Each byte contains 8 bits worth of data.
byte4 = 0x87u32; byte3 = 0x65u32; byte2 = 0x43u32; byte1 = 0x21u32;
Start by adding the first byte as the first 8 bits of a 32-bit unsigned integer.
Next, pack the other three bytes into packedNum
, using bitshift
to shift the bytes to the proper locations, and bitor
to copy the bits over.
packedNum = bitor(packedNum,bitshift(byte2,8)); packedNum = bitor(packedNum,bitshift(byte3,82)); packedNum = bitor(packedNum,bitshift(byte4,83));
View the packed 32-bit integer.
packedNum = uint32 87654321
Input Arguments
Input values, specified as an array. A
can be a scalar or an array of the same size as k
.
- If
A
is a double array, andassumedtype
is not specified, then MATLAB treatsA
as an unsigned 64-bit integer. - If
assumedtype
is specified, then all elements inA
must have integer values within the range ofassumedtype
.
Data Types: double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Number of bits to shift, specified as an integer or integer array.k
can be a scalar or an array of the same size asA
.
Data Types: double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Assumed data type of A
, specified as 'uint64'
, 'uint32'
, 'uint16'
, 'uint8'
, 'int64'
, 'int32'
, 'int16'
, or 'int8'
.
- If
A
is an integer type array, thenassumedtype
must specify that same integer type. - If
A
is a double array, thenassumedtype
can specify any valid integer type.
Data Types: char
| string
Output Arguments
Shifted values, returned as an array. intout
is the same data type as A
.
- If
A
andk
are scalars, thenintout
is also a scalar. - If either
A
ork
is an array, thenintout
is the same size as that array.
Extended Capabilities
For efficient HDL code generation, use the Fixed-Point Designerâ„¢ functions bitsll
, bitsrl
, orbitsra
instead of bitshift
.
The bitshift
function supports GPU array input with these usage notes and limitations:
- At least one of the inputs,
A
ork
, must be an integer array. - Input
A
cannot be a signed integer array. - 64-bit integers are not supported.
- The
assumedtype
argument is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a