首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从uint32获取特定位

从uint32获取特定位
EN

Stack Overflow用户
提问于 2016-01-18 08:00:31
回答 1查看 8.4K关注 0票数 6

我有一个类似于3238844000的UInt32变量。

现在我想得到这个数字的前两位和前两位之后的6位。两个位都应该是int。

代码语言:javascript
复制
Decimal: 3238844000
Binary:  11000001000011001101011001100000
         ^^

代码语言:javascript
复制
Decimal: 3238844000
Binary:  11000001000011001101011001100000
           ^^^^^^ 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-18 08:03:23

更新2:

对于这种情况,最简单(也是最快)的方法是纯粹使用位移位运算符

代码语言:javascript
复制
int val = (int)(input >> 30); // performs the same 
int val2 = (int)((input << 2) >> 26); //the simplest and the fastest way

我以前听说过按位换班操作速度更快。但是今天,出于好奇*,我真的比较了位移位+掩码((int)((input & mask2) >> 24))和位移位单独((int)((input << 2) >> 26))的性能。单独按位移位操作大约比快10%-15%.

我得到的结果是:

代码语言:javascript
复制
[2016-01-20 04:01:26.638 UTC] shift-mask: 235 ms    shift-only: 199 ms
[2016-01-20 04:01:30.402 UTC] shift-mask: 233 ms    shift-only: 200 ms
[2016-01-20 04:01:31.265 UTC] shift-mask: 233 ms    shift-only: 198 ms
[2016-01-20 04:01:32.116 UTC] shift-mask: 227 ms    shift-only: 199 ms
[2016-01-20 04:01:32.850 UTC] shift-mask: 233 ms    shift-only: 198 ms
[2016-01-20 04:01:33.584 UTC] shift-mask: 230 ms    shift-only: 199 ms
[2016-01-20 04:01:34.280 UTC] shift-mask: 263 ms    shift-only: 214 ms
[2016-01-20 04:01:35.055 UTC] shift-mask: 229 ms    shift-only: 201 ms
[2016-01-20 04:01:36.996 UTC] shift-mask: 234 ms    shift-only: 201 ms
[2016-01-20 04:01:37.933 UTC] shift-mask: 224 ms    shift-only: 198 ms
[2016-01-20 04:01:38.353 UTC] shift-mask: 222 ms    shift-only: 196 ms
[2016-01-20 04:01:38.798 UTC] shift-mask: 233 ms    shift-only: 211 ms
[2016-01-20 04:01:39.246 UTC] shift-mask: 235 ms    shift-only: 213 ms
[2016-01-20 04:01:39.668 UTC] shift-mask: 223 ms    shift-only: 198 ms
[2016-01-20 04:01:41.102 UTC] shift-mask: 234 ms    shift-only: 200 ms
[2016-01-20 04:01:41.524 UTC] shift-mask: 224 ms    shift-only: 198 ms
[2016-01-20 04:01:41.948 UTC] shift-mask: 223 ms    shift-only: 200 ms
[2016-01-20 04:01:42.373 UTC] shift-mask: 224 ms    shift-only: 200 ms
[2016-01-20 04:01:43.521 UTC] shift-mask: 233 ms    shift-only: 197 ms
[2016-01-20 04:01:44.272 UTC] shift-mask: 237 ms    shift-only: 216 ms
[2016-01-20 04:01:44.909 UTC] shift-mask: 231 ms    shift-only: 196 ms
[2016-01-20 04:01:45.353 UTC] shift-mask: 230 ms    shift-only: 213 ms
[2016-01-20 04:01:45.850 UTC] shift-mask: 237 ms    shift-only: 207 ms
[2016-01-20 04:01:46.276 UTC] shift-mask: 226 ms    shift-only: 200 ms
[2016-01-20 04:01:47.074 UTC] shift-mask: 234 ms    shift-only: 203 ms
[2016-01-20 04:01:47.718 UTC] shift-mask: 230 ms    shift-only: 199 ms
[2016-01-20 04:01:48.144 UTC] shift-mask: 226 ms    shift-only: 200 ms
[2016-01-20 04:01:48.567 UTC] shift-mask: 225 ms    shift-only: 198 ms
[2016-01-20 04:01:48.994 UTC] shift-mask: 225 ms    shift-only: 199 ms
[2016-01-20 04:01:49.429 UTC] shift-mask: 223 ms    shift-only: 211 ms
[2016-01-20 04:01:49.860 UTC] shift-mask: 232 ms    shift-only: 198 ms
[2016-01-20 04:01:50.284 UTC] shift-mask: 225 ms    shift-only: 199 ms

注意:每个实验都是为(5,000,000 x 100)操作进行的。

*还记得我以前和微控制器打交道的日子.;)

原版:

就像您如何为您的UInt32找到二进制表示一样,您也应该在其二进制表示形式中找到正确的按位掩码

代码语言:javascript
复制
uint mask1 = 0xC0000000; //1100 0000 0000 0000 0000 0000 0000 0000
uint mask2 = 0x3F000000; //0011 1111 0000 0000 0000 0000 0000 0000 

然后将它们与位-和运算符一起使用。

要获得前两位,只需使用下面这样的掩码:

代码语言:javascript
复制
uint val = input & mask1; //should give you the first two bits, the rests are zero

为了得到接下来的6位:

代码语言:javascript
复制
uint val2 = input & mask2; //similarly, should give you only the six bits in the position which you want

如果您需要在int中使用它们,那么只需将它们转换为:

代码语言:javascript
复制
int val = (int)(input & mask1);
int val2 = (int)(input & mask2);

如果您想将结果放在LSB中(在本例中是最不重要的byte,最右边的8-bit ),请使用按位右移运算符:

代码语言:javascript
复制
int val = (int)((input & mask1) >> 30); //30 bits are 0
int val2 = (int)((input & mask2) >> 24); //24 bits are 0

更新:

实际上,对于上述移位版本,您也可以简单地按位右移第一个版本,对第二个版本执行几乎类似的操作,但这将需要一个按位掩码 of 0x3F (0011 1111)来清除不需要的前两位。

代码语言:javascript
复制
int val = (int)(input >> 30); // performs the same 
int val2 = (int)((input >> 24) & 0x3F); //the simpler way

在位表示中,它们发生了什么(我给出了一些注释,以便于遵循逻辑流程):

代码语言:javascript
复制
The first one:

1100 0001 0000 1100 1101 0110 0110 0000
--------------------------------------- >> 30 //bitwise right shift by 30
0000 0000 0000 0000 0000 0000 0000 0011 //you get only the first two bits, the rests are all replaced by 0

The second one:

1100 0001 0000 1100 1101 0110 0110 0000
--------------------------------------- >> 24 //bitwise right shift by 24
0000 0000 0000 0000 0000 0000 1100 0001
                              0011 1111 //this is 0x3f
--------------------------------------- & //this is bitwise-and
0000 0000 0000 0000 0000 0000 0000 0001 //you only get the 6 bits which you want

因此,第一个值将得到3 (0000 0000 0000 0000 0000 0000 0000 0011),第二个值将得到1 (0000 0000 0000 0000 0000 0000 0000 0001)

随着上面的例子,我认为您可以了解如何在许多其他不同的情况下这样做。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34849753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档