Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rewrite array manipulations from C++ to C#

Tags:

c++

c#

algorithm

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

like image 901
Andriy Zakharko Avatar asked May 17 '26 22:05

Andriy Zakharko


1 Answers

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.

like image 142
xanatos Avatar answered May 19 '26 10:05

xanatos