Main Content

bitshift

Shift bits specified number of places

Description

example

intout= bitshift(A,k)returnsAshifted to the left bykbits, equivalent to multiplying by 2k. Negative values ofkcorrespond to shifting bits right or dividing by 2|k|and rounding to the nearest integer towards negative infinity. Any overflow bits are truncated.

  • IfAis an array of signed integers, thenbitshiftreturns the arithmetic shift results, preserving the signed bit whenkis negative, and not preserving the signed bit whenkis positive.

  • Ifkis positive, MATLAB®shifts the bits to the left and insertsk0-bits on the right.

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

  • Ifkis negative andAis negative, then MATLAB shifts the bits to the right and inserts|k|1-bits on the left.

example

intout= bitshift(A,k,assumedtype)assumesAis of typeassumedtype.

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
fori = 1:8 a = bitshift(a,1); fprintf(s2,a,dec2bin(a))end
254是11111110的二进制Shif uint8转移价值ted 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×3192 128 0
intout = bitshift(6,5:7,'int8')
intout =1×3-64 -128 0

Usebitorandbitshiftto 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-u32suffix to specify that the data should be stored asuint32. 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 intopackedNum, usingbitshiftto shift the bytes to the proper locations, andbitorto 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.

formathexpackedNum
packedNum =uint3287654321

Input Arguments

collapse all

Input values, specified as an array.Acan be a scalar or an array of the same size ask.

  • IfAis a double array, andassumedtypeis not specified, then MATLAB treatsAas an unsigned 64-bit integer.

  • Ifassumedtypeis specified, then all elements inAmust have integer values within the range ofassumedtype.

Data Types:double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Number of switched bits, specified as an integer or integer array.kcan 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 ofA, specified as'uint64','uint32','uint16','uint8','int64','int32','int16', or'int8'.

  • IfAis an integer type array, thenassumedtypemust specify that same integer type.

  • IfAis a double array, thenassumedtypecan specify any valid integer type.

Data Types:char|string

Output Arguments

collapse all

Shifted values, returned as an array.intoutis the same data type asA.

  • IfAandkare scalars, thenintoutis also a scalar.

  • If eitherAorkis an array, thenintoutis 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