Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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#)_C#_Arrays_Counter_Bits - Fatal编程技术网

字节数组中逐位递增(C#)

字节数组中逐位递增(C#),c#,arrays,counter,bits,C#,Arrays,Counter,Bits,我有一个两个字节的字节数组,我用它作为计数器。我需要一点一点地增加它,如: 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0010 0000 0000 0000 0011 . . . 0000 0000 1111 1111 0000 0001 1111 1111 0000 0010 1111 1111 0000 0011 1111 1111 最干净的方法是什么 编辑 对不起,这个超级愚蠢的问题,我看错了。如果将来有人遇到同样愚蠢的

我有一个两个字节的字节数组,我用它作为计数器。我需要一点一点地增加它,如:

0000 0000 0000 0000
0000 0000 0000 0001
0000 0000 0000 0010
0000 0000 0000 0011
.
.
.
0000 0000 1111 1111
0000 0001 1111 1111
0000 0010 1111 1111
0000 0011 1111 1111
最干净的方法是什么

编辑


对不起,这个超级愚蠢的问题,我看错了。如果将来有人遇到同样愚蠢的问题:如评论中所述,更简单的方法是增加一个Int16。

您只需将两个字节转换为Int16,附加所需的位,然后返回到字节数组:

byte[] byteArray = new byte[2] { 10, 20 }; // your byte array
Int16 yourNumber = BitConverter(byteArray, 0); // converts your byte array to int16
yourNumber ++; // appends 1 bit
byte[] getBytes = BitConverter.GetBytes(yourNumber); // converts the int16 to byte array (I think you should be using Int16, unless you really need to use a byte array)

我不确定您到底在请求什么,如果您只想在2字节数组中添加一位,我认为这是最快的方法。

请展示您尝试过的方法。稍后关注最干净的方式使用Int16-这已经是一个可以递增的2字节值…如果这对您不起作用,请编辑您的问题以解释为什么…这是一个经典。对于一项简单的工作,您使用了错误的工具,并询问我们最好的方法是什么。最好的方法是使用正确的工具(RB编写的Int16)。