为什么这个函数用位运算符转换成这种方式?

mic*_*ima 3 c# bitwise-operators

我正在为一个设备编写一个控件类,直到我需要将ARGB颜色转换为其格式.起初,我写了这个函数(有效):

private static int convertFormat(System.Drawing.Color c)
{
    String all;

    int a = (int)((float)c.A / 31.875);

    if (a == 0)
        a = 1;

    all = a.ToString() + c.B.ToString("X").PadLeft(2, '0') + c.G.ToString("X").PadLeft(2, '0') + c.R.ToString("X").PadLeft(2, '0');

    int num = int.Parse(all, System.Globalization.NumberStyles.AllowHexSpecifier);

    return num;
}
Run Code Online (Sandbox Code Playgroud)

但它太丑了我想写一个更优雅的解决方案.所以我做了一些以获得正确的值,尝试0到50之间的所有组合.它工作,我最终得到了这个:

private static int convertFormatShifting(System.Drawing.Color c)
{
    int alpha = (int)Math.Round((float)c.A / 31.875);

    int a = Math.Max(alpha,1);

    return (a << 24) | (c.B << 48) | (c.G << 40) | (c.R << 32);
}
Run Code Online (Sandbox Code Playgroud)

有效!

但是现在,我希望有人能够解释为什么这些是正确的变化值.

das*_*ght 9

最容易混淆的移位值应如下:

return (a << 24) | (c.B << 16) | (c.G << 8) | (c.R << 0);
// Of course there's no need to shift by zero  ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

您的值工作的原因是移位运算符mod在右侧,操作数的位长度在左侧.换句话说,以下所有变化都是相同的:

c.G << 8
c.G << 40
c.G << 72
c.G << 104
...
Run Code Online (Sandbox Code Playgroud)