Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#获取32位uint的最后10位_C#_C# 4.0 - Fatal编程技术网

C#获取32位uint的最后10位

C#获取32位uint的最后10位,c#,c#-4.0,C#,C# 4.0,我想得到32位整数的最后10位 基本上我所做的就是把一个数字塞进前22位,我可以用 int test = Convert.ToInt32(uIActivityId >> nMethodBits); //method bits == 10 so this just pushes the last ten bits off the range 其中测试结果在我把数字放在首位 但现在我被难倒了。所以我要问你们的问题是。如何获取32位整数的最后10位?首先创建一个掩码,其中1位表示您想要的

我想得到32位整数的最后10位

基本上我所做的就是把一个数字塞进前22位,我可以用

int test = Convert.ToInt32(uIActivityId >> nMethodBits); //method bits == 10 so this just pushes the last ten bits off the range
其中测试结果在我把数字放在首位


但现在我被难倒了。所以我要问你们的问题是。如何获取32位整数的最后10位?

首先创建一个掩码,其中1位表示您想要的位,0位表示您不感兴趣的位。然后使用二进制
仅保留相关位

const uint mask = (1 << 10) - 1; // 0x3FF
uint last10 = input & mask;
const uint mask=(1
public bool[]GetLast10Bits(int输入){
BitArray数组=新的BitArray(新[]{input});
列表位=新列表();
如果(array.Length>10){
返回可枚举的.Range(0,10).Select(i=>array[i]).ToArray();
}
返回新bool[0];
}

这是另一次尝试

List<bool> BitsOfInt(int input , int bitCount)
    {
        List<bool> outArray = new BitArray (BitConverter.GetBytes(input)).OfType<bool>().ToList();
        return outArray.GetRange(outArray.Count - bitCount, bitCount);
    }
List BitsOfInt(int输入,int位计数)
{
List outArray=类型()的新位数组(BitConverter.GetBytes(输入)).ToList();
返回outArray.GetRange(outArray.Count-bitCount,bitCount);
}

不确定,但我认为您可以向上推22位,而denn可以向下推22位。您所说的“第一位”和“最后一位”是什么意思?最重要和最不重要的内容更容易理解。理想情况下,也可以展示一个简短但完整的程序来演示问题……如果您所说的“最后一位”是最不重要的内容,var last=data&0x3ff;通常最好对您作为答案提供的代码以及它是如何回答问题的进行一些解释。即使这是非常不言自明的。此代码的问题在于输出取决于主机端性。也就是说,它输出内存中最后的位,而不是最不重要的位位。@CODES因为这个问题提到了32位整数的最后10位,所以相对答案可能是内存中的最后10位。不管怎样,我只关注了
last
,您的评论是可以接受的。
int i = Convert.ToInt32("11100111101", 2);
int mask = Convert.ToInt32("1111111111", 2);
int test = Convert.ToInt32(( i&mask));
int j = Convert.ToInt32("1100111101", 2);
if (test == j)
   System.Console.Out.WriteLine("it works");
List<bool> BitsOfInt(int input , int bitCount)
    {
        List<bool> outArray = new BitArray (BitConverter.GetBytes(input)).OfType<bool>().ToList();
        return outArray.GetRange(outArray.Count - bitCount, bitCount);
    }