<?php

echo "=== Verifying Bit Swap Logic ===\n\n";

// Expected format bits for M, Mask 2
$formatBits = 0b101111001111100;
$formatBinary = sprintf('%015b', $formatBits);

echo "Format Bits (M, Mask 2): {$formatBinary}\n";
echo "Positions: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14\n";
echo "Bits:      ";
for ($i = 0; $i < 15; $i++) {
    echo $formatBinary[$i] . " ";
}
echo "\n\n";

// Current bit order in horizontal placement
$bitOrder = [8, 10, 9, 11, 13, 12, 14];
$rightCols = [20, 19, 18, 17, 16, 15, 14];

echo "Current implementation places:\n";
echo "Column | Bit Index | Bit Value\n";
for ($i = 0; $i < 7; $i++) {
    $bitIndex = $bitOrder[$i];
    $bit = ($formatBits >> (14 - $bitIndex)) & 1;
    echo sprintf("%6d | %9d | %9d\n", $rightCols[$i], $bitIndex, $bit);
}

echo "\n";

// What Python reference expects
$pythonH = "101010000100100";  // For M, Mask 0
echo "Python reference (M, Mask 0):\n";
echo "Format: {$pythonH}\n";
echo "Position 14 (last): {$pythonH[14]}\n";
