【发布时间】:2015-12-19 16:34:30
【问题描述】:
我有一个大小为 16384(SourceArray)的字节数组。我想把它分成 4 部分,大小为 4096 的 4 字节数组。现在 SourceArray 中的每个 32 位将分为 10 位、10 位、11 位和 1 位(10+10+11+1=32 位)。取前 10 位,然后是下 10 位,然后是 11 位,然后是 1 位。
例如,来自 sourceArray 的前 4 个字节,我得到一个值 9856325。现在 9856325(dec) = 00000000100101100110010101000101(Binary)------------------------ ------
0000000010(10 位) 0101100110(10 位) 01010100010(11 位) 1(1 位)。我不能在字节数组中存储 10、10、11 位,所以我将它存储到 Int16 数组中,对于 1 位,我可以将它存储到一个字节数组中。
`
我希望问题陈述清楚。我已经创建了一些逻辑。
Int16[] Array1 = new Int16[4096];
Int16[] Array2 = new Int16[4096];
Int16[] Array3 = new Int16[4096];
byte[] Array4 = new byte[4096];
byte[] SourceArray = File.ReadAllBytes(@"Reading an image file from directory");
//Size of source Array = 16384
for (int i = 0; i < 4096; i++)
{
uint temp = BitConverter.ToUInt32(SourceArray, i * 4);
uint t = temp;
Array1[i] = (Int16)(t >> 22);
t = temp << 10;
Array2[i] = (Int16)(t >> 22);
t = temp << 20;
Array3[i] = (Int16)(t >> 21);
t = temp << 31;
Array4[i] = (byte)(t>>31);
}
但我没有得到实际结果。我可能在我的逻辑中遗漏了一些东西。请验证它,有什么问题或者如果您有更好的逻辑,请建议我。
【问题讨论】:
-
有一些类型可以帮到你,第一个是
System.Collections.BitArray:msdn.microsoft.com/en-us/library/…另一个是System.Collections.Specialized.BitVector32msdn.microsoft.com/en-us/library/… -
拆分为 10,10,11,1 您将 4 字节值 (int32) 分解为 7 字节值,不是吗? IOW 它不应该是 4096 而是 16384 作为数组大小。
-
不,我将它分成大小为 4096 的 4 字节数组。即 4096*4 = 16384 字节
标签: c# bit-manipulation bitwise-operators bit-shift