简体   繁体   English

运算符“&”不能应用于类型为“ byte []”和“ byte []”的操作数

[英]Operator '&' cannot be applied to operands of type 'byte[]' and 'byte[]'

I'm trying to process a byte array retrieving from a sensor. 我正在尝试处理从传感器检索的字节数组。 In the retrieved byte array, there will be a byte-mask, xx-xx-xx-xx, that tells me which data will be in the array. 在检索到的字节数组中,将有一个字节掩码xx-xx-xx-xx,告诉我哪些数据将在数组中。

The sensor mask: 传感器罩:

DATA_1 0x00000001
DATA_2 0x00000002
DATA_3 0x00000004
DATA_4 0x00000008
DATA_5 0x00000010
DATA_6 0x00000020
DATA_7 0x00000040
DATA_8 0x00000080
DATA_9 0x00000100
DATA_10 0x00000200
DATA_11 0x00000400

This byte-mask 43-05-00-00, for example, tells me that data_1, data_2, data_7, data_9, data_11 will be in response array. 例如,此字节掩码43-05-00-00告诉我data_1,data_2,data_7,data_9,data_11将在响应数组中。 I know this by using bitwise-and in Calculator app of Windows: in hex mode, type 543, click "And" button, then type the sensor mask (1, 2, 4, 8, 10,...). 我知道这是通过使用按位运算符-和在Windows的计算器应用程序中实现的:在十六进制模式下,键入543,单击“和”按钮,然后键入传感器蒙版(1、2、4、8、10,...)。 If the result is the same as sensor mask (1, 2, 4, 8, 10,...), this data is included. 如果结果与传感器蒙版(1、2、4、8、10 ...)相同,则包含此数据。 But I don't know how to achieve this in C#. 但是我不知道如何在C#中实现这一目标。

This is my ideal but it gave me error: Operator '&' cannot be applied to operands of type 'byte[]' and 'byte[]' 这是我的理想选择,但是它给了我错误:运算符'&'不能应用于类型为'byte []'和'byte []'的操作数

int[] sensorMaskList = new int[length] {1, 2, 4, 8,... };
internal List<int> GetSelectedData(byte[] byteMask)
{
    List<int> lstDataIndex = new List<int>();
    for (int i = 0; i < sensorMaskList.Length; i++)
    {
        byte[] mask = BitConverter.GetBytes(sensorMaskList[i]);
        if (mask & byteMask == mask)
           lstDataIndex.Add(i);
     }
     return lstDataIndex;
}

Can someone give me some ideal. 有人可以给我一些理想。 Thanks in advance and sorry for bad english 在此先感谢,对不起英语不好

You can use BitArray class to easily perform and operation 您可以使用BitArray类轻松执行和操作

int[] sensorMaskList = new int[length] {1, 2, 4, 8,... };
internal List<int> GetSelectedData(byte[] byteMask)
{
    BitArray byteMaskBits = new BitArray(byteMask);
    List<int> lstDataIndex = new List<int>();
    for (int i = 0; i < sensorMaskList.Length; i++)
    {
        byte[] mask = BitConverter.GetBytes(sensorMaskList[i]);
        BitArray maskBits = new BitArray(mask);
        if(maskBits.And(byteMaskBits) == maskBits)
        {
            lstDataIndex.Add(i);
        }
     }
     return lstDataIndex;
}

All the error message "Operator '&' cannot be applied to operands of type 'byte[]' and 'byte[]'" means is that you can not use the bitwise AND operator on variables of type array. 所有错误消息“运算符'&'不能应用于类型为'byte []'和'byte []'的操作数”表示您不能在类型为array的变量上使用按位AND运算符。 You can only use them on a single integer or unsigned integer variable or two integer or unsigned integers can be bitwise ANDED together. 您只能在单个整数或无符号整数变量上使用它们,或者两个整数或无符号整数可以按位ANDED。

What you may want to do if you want to do it for the whole array is make up another array of equal size and AND each variable in the first array with each variable in the second array in the corresponding positions such that the first variable in the array 1 will be bitwise ANDED with the first variable in array 2 and so forth. 如果要对整个数组执行此操作,您可能想做的是组成另一个大小相等的数组,然后将第一个数组中的每个变量与第二个数组中的每个变量放在相应的位置,使得第一个数组中的第一个变量数组1将与数组2中的第一个变量按位与,依此类推。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符&#39;+&#39;不能应用于&#39;byte []&#39;和&#39;int&#39;类型的操作数 - Operator '+' cannot be applied to operands of type 'byte[]' and 'int' 运算符 &amp; 不能应用于 C# 中 bool 和 byte 类型的操作数 - operator & cannot be applied to operands of type bool and byte in C# 运算符“-”不能应用于类型为 - Operator '-' cannot be applied to operands of type 运算符&#39;==&#39;不能应用于类型为 - Operator '==' cannot be applied to operands of type 运算符&#39;!=&#39;不能应用于类型为 - Operator '!=' cannot be applied to operands of type 运算符&#39;==&#39;不能应用于&#39;Type&#39;类型的操作数 和&#39;类型?&#39; - Operator '==' cannot be applied to operands of type 'Type?' and 'Type?' 运算符“ &amp;&amp;”不能应用于类型为“ int”和“ int”的操作数 - Operator “&&” cannot be applied to operands of type 'int' and 'int' 运算符“-”不能应用于“ float”和“ string”类型的操作数 - Operator '-' cannot be applied to operands of type 'float' and 'string' 运算符'<'不能应用于'decimal'和'double'类型的操作数 - Operator '<' cannot be applied to operands of type 'decimal' and 'double' 运算符“==”不能应用于“KeyValuePair”和“string”类型的操作数? - Operator '==' cannot be applied to operands of type 'KeyValuePair' and 'string'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM