c#-获取特定位并获取ushort值的前14位 [英] c# - Get Specific Bit and Get first 14 bits of a ushort value

查看:349
本文介绍了c#-获取特定位并获取ushort值的前14位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读完所有有关移位/屏蔽的问题和答案后,我根本无法解决问题.我只是不了解它在基本层面上是如何工作的.我已经能够通过使用BitArray和BitConverter来实现各种技术,但是我真的很想更好地理解位移位/屏蔽.

After reading all of the questions and answers on bit shifting/masking, I simply cannot wrap my head around it. I'm just not understanding how it works on a fundamental level. I've been able to achieve various techniques by using BitArray and BitConverter instead, but I really would like to understand bit shifting/masking better.

我的具体需求是执行以下操作:

The specific need I have is to do the following:

我有一个ushort: 0x810E (33038)

I have a ushort: 0x810E (33038)

使用位移/屏蔽,我想知道如何:

Using bit shifting/masking, I'd like to know how to:

  • 获取第16位结果:1​​
  • 获取第15位结果:0
  • 获取一系列位以创建新的ushort值,特别是 前14位结果:270
  • Get the 16th bit Result: 1
  • Get the 15th bit Result: 0
  • Get a range of bits to create a new ushort value, specifically the first 14 bits Result: 270

正如我所说,我能够使用BitArray执行这些任务,这是我获得正确结果的方式,但是我想了解如何使用移位/屏蔽来执行这些操作.

As I said, I'm able to perform these tasks using BitArray which is how I've gotten the correct results, but I'd like to understand how to perform these operations using bit shifting/masking.

任何帮助将不胜感激.

推荐答案

屏蔽单个位

您可能知道ushort是一个16位值,因此您给定的数字0x810E也可以写为

As you probably know, ushort is a 16-bit value, so your given number 0x810E can also be written as

‭10000001 00001110‬

由于ushort没有移位运算符,因此该值首先转换为int.

Because there is no shifting operator for ushort, the value is first converted to int.

因此,如果要获得第15位,可以取一位

So, if you want to get the 15th bit, you can take a single bit

000000000 0000000 00000000 00000001

并将其向左移动14次(右侧充满0)

and shift it 14 times to the left (right side is filled with 0)

00000000 00000000 01000000 00000000

,您已经创建了一个位掩码.

and you have created a bitmask.

现在,如果将掩码和值按位组合and,则将仅获得第15位的值:

Now if you combine mask and value with an bitwise and, you'll get only the value of the 15th bit:

  ‭00000000 00000000 10000001 00001110‬
& 00000000 00000000 01000000 00000000
= 00000000 00000000 00000000 00000000

又是0.要访问此位,您必须将整个结果向右移14次,然后 cast 将其转换为ushort.

which is 0 again. To access this bit, you'll have to shift the whole result back to the right 14 times and cast it to a ushort.

这可以用以下代码表示:

This can be expressed with the following code:

ushort value_15 = (ushort)(((1 << 14) & value) >> 14);

我们可以做得更好吗?

虽然这种方法似乎是正确的,但是有一种更简单的方法可以做到:将原始值右移14次 (结果为00000000 00000000 00000000 00000010,左侧填充为0),然后使用1执行简单的按位&:

Although this approach seems to be correct, but there is a simpler way to do this: shift the original value to the right 14 times (result is 00000000 00000000 00000000 00000010, left side is filled with 0) and perform a simple bitwise & with 1:

  00000000 00000000 00000000 00000000 00000000 00000010 
& 00000000 00000000 00000000 00000000 00000000 00000001
= 00000000 00000000 00000000 00000000 00000000 00000000

这将导致C#出现在

ushort value_15 = (ushort)((value >> 14) & 1);

因此,您可以避免再移动一次,即使使用 带符号的数字(因为用于符号的最高位通过移位保持不变).

So you avoid one additional shift and we get the same results even when using signed numbers (because there the highest bit, used for the sign, is left unchanged by shifting).

设置一个范围

要掩盖一点范围,您要做的就是更改掩膜.因此,要获取低14位的值,可以使用掩码

To mask a bit range, all you have to do is to change your mask. So, to get the value of the lower 14 bits you can use the mask

  00000000 00000000 ‭10000001 00001110‬
& 00000000 00000000 00111111 11111111
= 00000000 ‭00000000 00000001 00001110

C#中,可以表示为

ushort first14bits = (ushort)((0xFFFF >> 2) & value);

其中(0xFFFF00000000 00000000 11111111 11111111).

这篇关于c#-获取特定位并获取ushort值的前14位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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