Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reimplementing a specific binary offset alignment pattern

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

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

Thank you very much!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading