<?php

echo "=== Finding Horizontal Bit Pattern ===\n\n";

$formatBits = 0b101010000010010;
$formatBinary = sprintf('%015b', $formatBits);

$pythonH = "101010000100100";

echo "Format Bits: {$formatBinary}\n";
echo "  Positions: 0123456789ABCDE\n\n";

echo "Python H:    {$pythonH}\n";
echo "  Positions: 0123456789ABCDE\n\n";

// Try to find pattern by brute force
echo "Finding which format bit goes to which horizontal position:\n\n";

$mapping = [];
for ($hPos = 0; $hPos < 15; $hPos++) {
    $needBit = $pythonH[$hPos];
    
    // Find all format bit positions that have this value
    $candidates = [];
    for ($fPos = 0; $fPos < 15; $fPos++) {
        if ($formatBinary[$fPos] === $needBit) {
            $candidates[] = $fPos;
        }
    }
    
    echo "H-Pos {$hPos} needs '{$needBit}': candidates from format = [" . implode(', ', $candidates) . "]\n";
}

echo "\n=== Manual Pattern Recognition ===\n";
echo "Positions 0-7 match perfectly (bits 0-7)\n";
echo "Position 8: needs 0, format bit 8 = 0 ✅\n";
echo "Position 9: needs 1, format bit 9 = 0, bit 10 = 1 → use bit 10\n";
echo "Position 10: needs 0, format bit 10 = 1, bit 9 = 0 → use bit 9\n";
echo "Position 11: needs 0, format bit 11 = 0 ✅\n";
echo "Position 12: needs 1, format bit 12 = 0, bit 13 = 1 → use bit 13\n";
echo "Position 13: needs 0, format bit 13 = 1, bit 12 = 0 → use bit 12\n";
echo "Position 14: needs 0, format bit 14 = 0 ✅\n";

echo "\nSo the pattern is:\n";
echo "[0,1,2,3,4,5,6,7,8,10,9,11,13,12,14]\n";
