Main Content

bitshift

换位位指定位置数量

描述

例子

intout= bitshift(一个,,,,k返回一个shifted to the left byk位,等同于乘以2k。负值kcorrespond to shifting bits right or dividing by 2| K |并绕过最近的整数朝着负无穷大。任何溢出位都被截断。

  • 如果一个是一系列签名的整数,然后bitshift返回算术转移结果,保留签名位k是负面的,并且不能保留签名的位k是积极的。

  • 如果k是积极的,matlab®将钻头向左移动并插入k右边的0位。

  • 如果kis negative and一个is nonnegative, then MATLAB shifts the bits to the right and inserts|k|0-bits on the left.

  • 如果kis negative and一个是负的,然后Matlab将位移到右侧并插入|k|左侧1位。

例子

intout= bitshift(一个,,,,k,,,,假设假设一个是类型假设

例子

全部收缩

反复将未签名的8位值的位向左移动,直到所有非零位溢出。

a = intmax('uint8'); s1 ='Initial uint8 value %5d is %08s in binary\n'; s2 ='二进制\ n'中移动的UINT8值%5D为%08S; fprintf(s1,a,dec2bin(a))
Initial uint8 value 255 is 11111111 in binary
为了i = 1:8 a = bitshift(a,1); fprintf(s2,a,dec2bin(a))结尾
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

使用不同的假定整数类型找到数字的偏移。

uintout = bitshift(6,5:7,'uint8'
uintout =1×3192 128 0
intout = bitshift(6,5:7,'int8'
intout =1×3-64 -128 0

Use比尔andbitshift将四个8位字节打包成他们组成的32位整数。

Create four bytes of data. Specify the data with hexadecimal literals, using the-U32suffix to specify that the data should be stored asUINT32。每个字节包含8位价值的数据。

byte4 = 0x87u32; byte3 = 0x65u32; byte2 = 0x43u32; byte1 = 0x21u32;

首先将第一个字节添加为32位未签名整数的前8位。

包装= byte1;

接下来,将其他三个字节打包到包装, 使用bitshiftto shift the bytes to the proper locations, and比尔复制位。

packednum = bitor(packednum,bitshift(byte2,8));packednum = bitor(packednum,bitshift(byte3,8*2));packednum = bitor(packednum,bitshift(byte4,8*3));

查看包装的32位整数。

为了mat十六进制包装
包装=UINT3287654321

输入参数

全部收缩

Input values, specified as an array.一个可以是标量或大小相同的数组k

  • 如果一个是双阵列,假设未指定,然后是matlab治疗一个作为一个未签名的64位整数。

  • 如果假设is specified, then all elements in一个必须在范围内具有整数值假设

数据类型:双倍的|int8|int16|int32|INT64|UINT8|uint16|UINT32|Uint64

开关位数,指定为an integer or integer array.k可以是标量或大小相同的数组一个

数据类型:双倍的|int8|int16|int32|INT64|UINT8|uint16|UINT32|Uint64

假定的数据类型一个,指定为'uint64',,,,'uint32',,,,'uint16',,,,'uint8',,,,'int64',,,,'int32',,,,'int16', 或者'int8'

  • 如果一个是整数类型数组,然后假设必须指定相同的整数类型。

  • 如果一个是双阵列,然后假设可以指定任何有效的整数类型。

数据类型:char|细绳

输出参数

全部收缩

移动的值,返回为数组。intout与数据类型相同一个

  • 如果一个andk是标量intout也是标量。

  • 如果either一个ork是一个数组,然后intout与该数组的大小相同。

扩展功能

C/C ++代码生成
Generate C and C++ code using MATLAB® Coder™.

GPU代码生成
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

版本历史记录

Introduced before R2006a