iTextSharp.text.pdf.qrcode.FormatInformation.NumBitsDiffering C# (CSharp) Метод

NumBitsDiffering() публичный статический Метод

public static NumBitsDiffering ( int a, int b ) : int
a int
b int
Результат int
        public static int NumBitsDiffering(int a, int b) {
            a ^= b; // a now has a 1 bit exactly where its bit differs with b's
            // Count bits set quickly with a series of lookups:
            return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
                BITS_SET_IN_HALF_BYTE[(a >> 4 & 0x0F)] +
                BITS_SET_IN_HALF_BYTE[(a >> 8 & 0x0F)] +
                BITS_SET_IN_HALF_BYTE[(a >> 12 & 0x0F)] +
                BITS_SET_IN_HALF_BYTE[(a >> 16 & 0x0F)] +
                BITS_SET_IN_HALF_BYTE[(a >> 20 & 0x0F)] +
                BITS_SET_IN_HALF_BYTE[(a >> 24 & 0x0F)] +
                BITS_SET_IN_HALF_BYTE[(a >> 28 & 0x0F)];
        }

Usage Example

Пример #1
0
        static Version DecodeVersionInformation(int versionBits)
        {
            int bestDifference = int.MaxValue;
            int bestVersion    = 0;

            for (int i = 0; i < VERSION_DECODE_INFO.Length; i++)
            {
                int targetVersion = VERSION_DECODE_INFO[i];
                // Do the version info bits match exactly? done.
                if (targetVersion == versionBits)
                {
                    return(GetVersionForNumber(i + 7));
                }
                // Otherwise see if this is the closest to a real version info bit string
                // we have seen so far
                int bitsDifference = FormatInformation.NumBitsDiffering(versionBits, targetVersion);
                if (bitsDifference < bestDifference)
                {
                    bestVersion    = i + 7;
                    bestDifference = bitsDifference;
                }
            }
            // We can tolerate up to 3 bits of error since no two version info codewords will
            // differ in less than 4 bits.
            if (bestDifference <= 3)
            {
                return(GetVersionForNumber(bestVersion));
            }
            // If we didn't find a close enough match, fail
            return(null);
        }