I have a C++ code that I'm trying to reuse on my C# project and I need some help.

Here is the subject

`` for (int i = 0; i < numOfSamples; i++){*(((double*)m_Buffer) + i)= max(*(((double*)m_Buffer) + i*4), *(((double*)m_Buffer) + i*4 + 1));}``

where m_Buffer is array of float. This part of code read each 2 "floats" of array as a one "double" and then do some manipulations (shift it, choose max etc.)

The question is - how can I do the same operation in C#.

For example, I have an array [12,45,26,32,07,89,14,11] and I have to transform items in position 0 and 1 (12 and 45) so that I will get a new number (type of double) where highest (I'm not sure - maybe lowest) part of bits will be formed from 12 and lowest - from 45

It should be something like:

``````for (int i = 0; i < numOfSamples; i++)
{
m_Buffer[i] = Math.Max(m_Buffer[i * 4], m_Buffer[i * 4 + 1]);
}
``````

Where `m_Buffer` must be an array of at least `numOfSamples * 4 + 1` elements.

So, I got the solution. Key point here is a structure

``````[StructLayout(LayoutKind.Explicit)]
struct MyStruct
{
[FieldOffset(0)]
public double Double;

[FieldOffset(0)]
public float Float1;

[FieldOffset(4)]
public float Float2;
}
``````

I simply create a new array and put array[2*i] to Float1 and array[2*i+1] to Float2. Then apply Math.Max to each new_array[i].Double

Top