从uint32获取特定位

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

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

 Decimal: 3238844000 Binary: 11000001000011001101011001100000 ^^ 

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

更新2:

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

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

我之前听说过按位移位操作往往更快。 但今天,出于好奇*,我真的比较了bitwise-shift + mask( (int)((input & mask2) >> 24) )与单独按位移位( (int)((input << 2) >> 26) )。 按位移位操作大约快10%-15%

这是我得到的结果:

 [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找到二进制表示一样,你也应该在它的二进制表示中找到正确的按位掩码

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

然后使用bitwise-and运算符

要获得前两位,您可以简单地使用这样的掩码:

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

并获得接下来的6位:

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

如果你在int需要它们,那么只需转换它们:

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

如果你想把结果放在LSB(最低有效byte ,在这种情况下是最右边的8-bit ),使用按位右移运算符:

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

更新:

至于上面的移位版本,实际上,您也可以简单地按位右移第一个版本,并且几乎类似于第二个版本,除了它需要按位掩码 0x3F0011 1111 )来清除不需要的前两位。

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

在位表示中他们发生的事情如下(我给出了评论以便于遵循逻辑流程):

 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)

除了上面的例子之外,我认为你也可以在许多其他不同情况下了解如何做到这一点。