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.

example

[intout](#bth5rcc-intout) = bitshift([A](#bth5rcc-A),[k](#bth5rcc-k),[assumedtype](#bth5rcc-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')

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

collapse all

Input values, specified as an array. A can be a scalar or an array of the same size as k.

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'.

Data Types: char | string

Output Arguments

collapse all

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

Extended Capabilities

expand all

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:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a