<?php

echo "=== Format Information Placement Analysis ===\n\n";

// Python reference format info
$pythonFormatH = "101010000100100";
$pythonFormatV = "101010000010010";

// The format bits from table (Level M, Mask 0)
$formatBits = 0b101010000010010;
$formatBinary = sprintf('%015b', $formatBits);

echo "Format Bits (M, Mask 0): {$formatBinary}\n\n";

echo "Python Reference reads as:\n";
echo "Horizontal (Row 8): {$pythonFormatH}\n";
echo "Vertical (Col 8):   {$pythonFormatV}\n\n";

// Analyze how Python places them
echo "=== Horizontal Placement (Row 8) ===\n";
echo "Columns: 0, 1, 2, 3, 4, 5, 7, 8, 20, 19, 18, 17, 16, 15, 14\n";
echo "Bits:    ";
for ($i = 0; $i < 15; $i++) {
    echo $pythonFormatH[$i] . "  ";
}
echo "\n\n";

echo "Expected mapping from formatBits:\n";
for ($i = 0; $i < 15; $i++) {
    $bit = ($formatBits >> (14 - $i)) & 1;
    echo "Bit {$i}: {$bit}\n";
}

echo "\n=== Vertical Placement (Col 8) ===\n";
echo "Rows: 20, 19, 18, 17, 16, 15, 14, 8, 7, 5, 4, 3, 2, 1, 0\n";
echo "Bits: ";
for ($i = 0; $i < 15; $i++) {
    echo $pythonFormatV[$i] . "  ";
}
echo "\n\n";

// Check if they're the same sequence
echo "Are they the same sequence?\n";
echo "Horizontal: {$pythonFormatH}\n";
echo "Vertical:   {$pythonFormatV}\n";

$differences = 0;
for ($i = 0; $i < 15; $i++) {
    if ($pythonFormatH[$i] !== $pythonFormatV[$i]) {
        echo "Bit {$i}: H={$pythonFormatH[$i]} V={$pythonFormatV[$i]} ❌\n";
        $differences++;
    }
}

if ($differences === 0) {
    echo "✅ They are identical!\n";
} else {
    echo "❌ They differ in {$differences} positions!\n";
}

echo "\n=== Key Insight ===\n";
echo "The format bits should be placed in DIFFERENT READING ORDERS\n";
echo "but represent the SAME 15-bit sequence!\n";
