Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# bitshift

Shift bits specified number of places

bitshift(A,k,N) will not accept the argument N in a future release. Use bitshift(A,k) or bitcmp(A,k,assumedtype) instead.

bitshift no longer interprets double values as 53-bit unsigned integers by default, but as uint64 values.

## Syntax

• intout = bitshift(A,k) example
• intout = bitshift(A,k,assumedtype) example
• intout = bitshift(A,k,N)

## Description

example

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

intout = bitshift(A,k,N) truncates any bits that overflow N bits.

## Examples

expand all

### Shifted 8-bit Integer

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))
for i = 1:8
a = bitshift(a,1);
fprintf(s2,a,dec2bin(a))
end
```
```Initial uint8 value   255 is 11111111 in binary
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```

### Different Results for Different Integer Types

Find the shift for a number using different assumed integer types.

```uintout = bitshift(6,5:7,'uint8')
intout = bitshift(6,5:7,'int8')```
```uintout =

192   128     0

intout =

-64  -128     0```

## Input Arguments

expand all

### A — Input valuesigned integer array | unsigned integer array | double array

Input value, specified as a signed integer array, unsigned integer array, or double array.

• 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

### k — Number of switched bitsinteger | integer array

Number of switched bits, specified as an integer or integer array.

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

### assumedtype — Assumed data type of A 'uint64' | 'uint32' | 'uint16' | 'uint8' | 'int64' | 'int32' | 'int16' | 'int8'

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

### N — Number of bits keptnonnegative integer | nonnegative integer array

Number of bits kept, specified as a nonnegative integer or integer array. N must be less than or equal to the number of bits in the unsigned integer class of A.

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

## Output Arguments

expand all

### intout — Shifted valuesigned integer array | unsigned integer array | double array

Shifted value, returned as signed integer array, unsigned integer array, or double array. intout is the same size and type as A.

## See Also

Was this topic helpful?