Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

bitshift

Shift bits specified number of places

Syntax

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

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

Examples

collapse 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')
uintout =

   192   128     0

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

   -64  -128     0

Input Arguments

collapse all

A — Input valuesarray

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

k — Number of switched bitsinteger | integer array

Number of switched bits, 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

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

Output Arguments

collapse all

intout — Shifted valuesarray

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.

See Also

| | | | | |

Introduced before R2006a

Was this topic helpful?