得到UINT32特定位 [英] Get specific bit from uint32

查看:149
本文介绍了得到UINT32特定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个3238844000 UInt32的变量。

现在我想的前两位后得到这个号码的前两个比特和6位。这两个位应该是int。

 十进制:3238844000
二进制:11000001000011001101011001100000
         ^^

 十进制:3238844000
二进制:11000001000011001101011001100000
           ^^^^^^


解决方案

更新2:

简单的(也是的最快的)的方式对于这种情况原来是通过的纯粹的使用按位移运营商

  INT VAL =(INT)(输入>> 30); //执行相同的
INT val2的=(int)的((输入&下; 2)>> 26); //最简单和最快的方法

我有按位移位操作往往会更快听说过。但是今天,出于好奇*的,我真的比较按位移+面膜((INT)((输入和放大器; MASK2)GT;> 24))之间的性能单独按位移((INT)((输入< 2)GT;> 26))。按位移单机运行约按快10%-15%

这是我得到的结果:

  [2016年1月20日04:01:26.638 UTC]换挡面罩:235毫秒移位仅199毫秒
[二○一六年一月二十〇日04:01:30.402 UTC]换挡面罩:233 MS-只转移:200毫秒
[二○一六年一月二十〇日04:01:31.265 UTC]换挡面罩:233 MS-只转移:198毫秒
[二○一六年一月二十〇日04:01:32.116 UTC]换挡面罩:227 MS-只转移:199毫秒
[二○一六年一月二十〇日04:01:32.850 UTC]换挡面罩:233 MS-只转移:198毫秒
[二○一六年一月二十〇日04:01:33.584 UTC]换挡面罩:230 MS-只转移:199毫秒
[二○一六年一月二十〇日04:01:34.280 UTC]换挡面罩:263 MS-只转移:214毫秒
[二○一六年一月二十〇日04:01:35.055 UTC]换挡面罩:229 MS-只转移:201毫秒
[二○一六年一月二十〇日04:01:36.996 UTC]换挡面罩:234 MS-只转移:201毫秒
[二○一六年一月二十〇日04:01:37.933 UTC]换挡面罩:224 MS-只转移:198毫秒
[二○一六年一月二十〇日04:01:38.353 UTC]换挡面罩:222 MS-只转移:196毫秒
[二○一六年一月二十〇日04:01:38.798 UTC]换挡面罩:233 MS-只转移:211毫秒
[二○一六年一月二十〇日04:01:39.246 UTC]换挡面罩:235 MS-只转移:213毫秒
[二○一六年一月二十〇日04:01:39.668 UTC]换挡面罩:223 MS-只转移:198毫秒
[二○一六年一月二十〇日04:01:41.102 UTC]换挡面罩:234 MS-只转移:200毫秒
[二○一六年一月二十〇日04:01:41.524 UTC]换挡面罩:224 MS-只转移:198毫秒
[二○一六年一月二十〇日04:01:41.948 UTC]换挡面罩:223 MS-只转移:200毫秒
[二○一六年一月二十〇日04:01:42.373 UTC]换挡面罩:224 MS-只转移:200毫秒
[二○一六年一月二十〇日04:01:43.521 UTC]换挡面罩:233 MS-只转移:197毫秒
[二○一六年一月二十〇日04:01:44.272 UTC]换挡面罩:237 MS-只转移:216毫秒
[二○一六年一月二十〇日04:01:44.909 UTC]换挡面罩:231 MS-只转移:196毫秒
[二○一六年一月二十〇日04:01:45.353 UTC]换挡面罩:230 MS-只转移:213毫秒
[二○一六年一月二十〇日04:01:45.850 UTC]换挡面罩:237 MS-只转移:207毫秒
[二○一六年一月二十〇日04:01:46.276 UTC]换挡面罩:226 MS-只转移:200毫秒
[二○一六年一月二十〇日04:01:47.074 UTC]换挡面罩:234 MS-只转移:203毫秒
[二○一六年一月二十〇日04:01:47.718 UTC]换挡面罩:230 MS-只转移:199毫秒
[二○一六年一月二十〇日04:01:48.144 UTC]换挡面罩:226 MS-只转移:200毫秒
[二○一六年一月二十〇日04:01:48.567 UTC]换挡面罩:225 MS-只转移:198毫秒
[二○一六年一月二十〇日04:01:48.994 UTC]换挡面罩:225 MS-只转移:199毫秒
[二○一六年一月二十〇日04:01:49.429 UTC]换挡面罩:223 MS-只转移:211毫秒
[二○一六年一月二十〇日04:01:49.860 UTC]换挡面罩:232 MS-只转移:198毫秒
[二○一六年一月二十〇日04:01:50.284 UTC]换挡面罩:225 MS-只转移:199毫秒

请注意:每个实验为(500万×100),操作完成

*记得我的老日子处理微控制器...;)


原文:

就像你是怎么找到你的 UInt32的,你应该找到合适的位掩码二重presentation二重presentation 太:

  UINT掩码1 = 0xC0000000的; // 1100 0000 0000 0000 0000 0000 0000 0000
UINT MASK2 = 0x3F000000; // 0011 1111 0000 0000 0000 0000 0000 0000

和然后使用他们的按位与运营商

要获得前两位,你可以简单地使用面膜是这样的:

  UINT VAL =输入&放大器;掩码1; //应该给你的前两位,其余为为零

和获得下一个6位:

  UINT将val2 =输入&放大器; MASK2; //类似,应该只给你要在其中的位置六位

如果你需要他们在 INT ,然后简单地丢掉。

  INT VAL =(INT)(输入和放大器;掩码1);
INT将val2 =(INT)(输入和放大器; MASK2);

如果你想要把结果在LSB(最低显著字节,最右侧的 8位在这种情况下),使用按位向右移动运算符:

  INT VAL =(INT)((输入和放大器;掩码1)GT;> 30); // 30位为0
INT将val2 =(INT)((输入和放大器; MASK2)GT;> 24); // 24位是0


更新:

对于上述移版本,其实,你也可以简单的按位向右移动第一个和第二个做几乎类似,除了它需要一个位掩码 0x3F的强> ( 0011 1111 ),清理不需要的前两位。

  INT VAL =(INT)(输入>> 30); //执行相同的
INT val2的=(int)的((输入>> 24)及0x3F的); //的更简单的方法

什么发生在他们身上的位重presentation情况如下(我发表意见,以缓解之后的逻辑流程):

 第一种方法:1100 0001 0000 1100 1101 0110 0110 0000
--------------------------------------->> 30 30 //按位右移
0000 0000 0000 0000 0000 0000 0000 0011 //你只有前两位,其余为全部换成0第二个:1100 0001 0000 1100 1101 0110 0110 0000
--------------------------------------->> 24 24 //按位右移
0000 0000 0000 0000 0000 0000 1100 0001
                              0011 1111 //这是0x3F的
---------------------------------------&安培; //这是按位与
0000 0000 0000 0000 0000 0000 0000 0001 //你只能得到你想要的6位

因此​​,您将获得 3(0000 0000 0000 0000 0000 0000 0000 0011)为您的第一个值,并获得 1(0000 0000 0000 0000 0000 0000 0000 0001)你的第二个值。

随着按照上面的例子中,我认为你可以得到关于如何做到这一点在许多其他不同的情况太的想法。

I have a UInt32 variable like 3238844000.

Now I want to get the first two bits of this number and the 6 bits after the first two bits. Both bits should be int.

Decimal: 3238844000
Binary:  11000001000011001101011001100000
         ^^

and

Decimal: 3238844000
Binary:  11000001000011001101011001100000
           ^^^^^^ 

解决方案

Update 2:

The simplest (and also the fastest) way for this case turns out to be by purely using the bitwise-shift operators

int val = (int)(input >> 30); // performs the same 
int val2 = (int)((input << 2) >> 26); //the simplest and the fastest way

I have heard before that bitwise-shift operations tend to be faster. But today, out of curiosity*, I really compared the performance between bitwise-shift + mask ((int)((input & mask2) >> 24)) with bitwise-shift alone ((int)((input << 2) >> 26)). bitwise-shift alone operation is approximately faster by 10%-15%.

This is the result I got:

[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

Note: each experiment is done for (5,000,000 x 100) operations.

*remembering my old days dealing with micro-controllers... ;)


Original:

Just like how you find binary representation for your UInt32, you should find the right bitwise mask in its binary representation too:

uint mask1 = 0xC0000000; //1100 0000 0000 0000 0000 0000 0000 0000
uint mask2 = 0x3F000000; //0011 1111 0000 0000 0000 0000 0000 0000 

And then use them with bitwise-and operator

To get the first two bits, you could simply use the mask like this:

uint val = input & mask1; //should give you the first two bits, the rests are zero

And to get the next 6 bits:

uint val2 = input & mask2; //similarly, should give you only the six bits in the position which you want

If you need them in int, then simply cast them:

int val = (int)(input & mask1);
int val2 = (int)(input & mask2);

And if you want to put the results in the LSB (the least significant byte, the rightmost 8-bit in this case), use bitwise right shift operator:

int val = (int)((input & mask1) >> 30); //30 bits are 0
int val2 = (int)((input & mask2) >> 24); //24 bits are 0


Update:

As for the above shifted version, actually, you could also simply bitwise right shift the first one and do almost similarly for the second, except that it would require a bitwise mask of 0x3F (0011 1111) to clear up the unwanted first two bits.

int val = (int)(input >> 30); // performs the same 
int val2 = (int)((input >> 24) & 0x3F); //the simpler way

What happen to them in the bit-representation is as follow (I give comments to ease following the logical flow):

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

Thus you will get 3 (0000 0000 0000 0000 0000 0000 0000 0011) for your first value, and get 1 (0000 0000 0000 0000 0000 0000 0000 0001) for your second value.

Along with following the example above, I think you can get the idea on how to do this on many other different cases too.

这篇关于得到UINT32特定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆