Home > OS >  Reimplementing a specific binary offset alignment pattern
Reimplementing a specific binary offset alignment pattern

Time:01-26

I'm recreating an application that has been done in Visual Basic 6.0 in C#, the problem is that I don't have the source code, and there's a method that returns an aligned binary offset for the input one.

I've been trying with this code but does not return the expected result.

static void Main(string[] args)
{
    for (int offset = 1; offset < 41; offset  )
    {
        int inputOffset = offset;
        if ((offset & 1) == 1) //Odd number
        {
            inputOffset = offset   1;
        }
        int AlignedOffset = (int)(inputOffset * 2.888888888888889);
        Console.WriteLine(offset   " -> "   AlignedOffset.ToString());
    }
    Console.ReadKey();
}

Here are some tests that I've done with the original Visual Basic application, left column is the input and the right column the output:

enter image description here

Seems that follows a pattern, but I'm not able to understand it.

Thank you very much!

CodePudding user response:

I think you're close. This seems to be multiples of 2.9 (not 2.8888) rounded to the nearest multiple of 4, with midpoints always rounding up.

This code seems to produce the correct sequence:

double factor = 2.9;
for (int i = 0; i < 100; i  )
{
    int result = (int)Math.Round((i * factor) / 4.0, MidpointRounding.AwayFromZero) * 4;
    Console.WriteLine($"{i} -> {result}");  
}

SharpLab

Nothing special went into this, other than observing that it's i * <something> rounded to the nearest multiple of 4, and playing around with the <something>, so this might diverge further on in the sequence and need a bit of tweaking.

  •  Tags:  
  • Related