Main Content

bitshift

Shift bits specified number of places

Description

intout = bitshift(A,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, then bitshift returns the arithmetic shift results, preserving the signed bit when k is negative, and not preserving the signed bit when k is positive.

  • If k is positive, MATLAB® shifts the bits to the left and inserts k 0-bits on the right.

  • If k is negative and A is nonnegative, then MATLAB shifts the bits to the right and inserts |k| 0-bits on the left.

  • If k is negative and A is negative, then MATLAB shifts the bits to the right and inserts |k| 1-bits on the left.

example

intout = bitshift(A,k,assumedtype) assumes A is of type assumedtype.

example

Examples

collapse all

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')
uintout = 1×3

   192   128     0

intout = bitshift(6,5:7,'int8')
intout = 1×3

   -64  -128     0

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.

packedNum = byte1;

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,8*2));
packedNum = bitor(packedNum,bitshift(byte4,8*3));

View the packed 32-bit integer.

format hex
packedNum
packedNum = uint32
   87654321

Input Arguments

collapse all

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, and assumedtype is not specified, then MATLAB treats A as an unsigned 64-bit integer.

  • If assumedtype is specified, then all elements in A must have integer values within the range of assumedtype.

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 as A.

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, then assumedtype must specify that same integer type.

  • If A is a double array, then assumedtype can specify any valid integer type.

Data Types: char | string

Output Arguments

collapse all

Shifted values, returned as an array. intout is the same data type as A.

  • If A and k are scalars, then intout is also a scalar.

  • If either A or k is an array, then intout is the same size as that array.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a