' IEEE 754 float/double bit layout — display sign, exponent, mantissa
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL fVal AS SINGLE
    LOCAL dVal AS DOUBLE
    LOCAL fBits  AS LONG
    LOCAL fSign  AS LONG
    LOCAL fExp  AS LONG
    LOCAL fMant  AS LONG
    LOCAL dBitsHigh  AS LONG
    LOCAL dBitsLow  AS LONG
    LOCAL dSign  AS LONG
    LOCAL dExp  AS LONG
    LOCAL dMantHigh  AS LONG
    LOCAL dMantLow  AS LONG
    LOCAL fStr  AS STRING
    LOCAL dStr  AS STRING
    LOCAL result  AS STRING
    PRINT "=== IEEE 754 Bit Layout Dumper ==="
    PRINT

    fVal = 3.14159
    fStr = MKD$(fVal)
    fBits = ASC(LEFT$(fStr, 1))
    fBits = fBits + ASC(MID$(fStr, 2, 1)) * 256
    fBits = fBits + ASC(MID$(fStr, 3, 1)) * 65536
    fBits = fBits + ASC(MID$(fStr, 4, 1)) * 16777216
    fSign = fBits / 2147483648
    fExp = (fBits / 8388608) AND 255
    fMant = fBits AND 8388607

    PRINT "FLOAT: 3.14159"
    PRINT "  Bits: " & FORMAT$(fBits)
    PRINT "  Sign: " & STR$(fSign)
    PRINT "  Exponent: " & STR$(fExp) & " (biased, actual: " & STR$(fExp - 127) & ")"
    PRINT "  Mantissa: " & FORMAT$(fMant) & " (23 bits)"
    result = "  1."
    FOR i = 22 TO 0 STEP -1
        IF (fMant AND (1 SHL i)) > 0 THEN
            result = result + "1"
        ELSE
            result = result + "0"
        END IF
    NEXT i
    PRINT result & " x 2^" & STR$(fExp - 127)
    PRINT

    fVal = -2.5
    fStr = MKD$(fVal)
    fBits = ASC(LEFT$(fStr, 1))
    fBits = fBits + ASC(MID$(fStr, 2, 1)) * 256
    fBits = fBits + ASC(MID$(fStr, 3, 1)) * 65536
    fBits = fBits + ASC(MID$(fStr, 4, 1)) * 16777216
    fSign = fBits / 2147483648
    fExp = (fBits / 8388608) AND 255
    fMant = fBits AND 8388607

    PRINT "FLOAT: -2.5"
    PRINT "  Bits: " & FORMAT$(fBits)
    PRINT "  Sign: " & STR$(fSign)
    PRINT "  Exponent: " & STR$(fExp) & " (biased, actual: " & STR$(fExp - 127) & ")"
    PRINT "  Mantissa: " & FORMAT$(fMant) & " (23 bits)"
    PRINT

    dVal = 3.14159265358979
    dStr = MKD$(dVal)
    dBitsLow = ASC(LEFT$(dStr, 1))
    dBitsLow = dBitsLow + ASC(MID$(dStr, 2, 1)) * 256
    dBitsLow = dBitsLow + ASC(MID$(dStr, 3, 1)) * 65536
    dBitsLow = dBitsLow + ASC(MID$(dStr, 4, 1)) * 16777216
    dBitsHigh = ASC(MID$(dStr, 5, 1))
    dBitsHigh = dBitsHigh + ASC(MID$(dStr, 6, 1)) * 256
    dBitsHigh = dBitsHigh + ASC(MID$(dStr, 7, 1)) * 65536
    dBitsHigh = dBitsHigh + ASC(MID$(dStr, 8, 1)) * 16777216
    dSign = dBitsHigh / 2147483648
    dExp = (dBitsHigh / 1048576) AND 2047
    dMantHigh = dBitsHigh AND 1048575
    dMantLow = dBitsLow

    PRINT "DOUBLE: 3.14159265358979"
    PRINT "  High bits: " & FORMAT$(dBitsHigh) & ", Low bits: " & FORMAT$(dBitsLow)
    PRINT "  Sign: " & STR$(dSign)
    PRINT "  Exponent: " & STR$(dExp) & " (biased, actual: " & STR$(dExp - 1023) & ")"
    PRINT "  Mantissa high: " & FORMAT$(dMantHigh) & " (20 bits)"
    PRINT "  Mantissa low: " & FORMAT$(dMantLow) & " (32 bits)"
    PRINT

    fVal = 0.0
    fStr = MKD$(fVal)
    fBits = ASC(LEFT$(fStr, 1))
    fBits = fBits + ASC(MID$(fStr, 2, 1)) * 256
    fBits = fBits + ASC(MID$(fStr, 3, 1)) * 65536
    fBits = fBits + ASC(MID$(fStr, 4, 1)) * 16777216
    fExp = (fBits / 8388608) AND 255
    fMant = fBits AND 8388607

    PRINT "FLOAT: 0.0"
    PRINT "  Bits: " & FORMAT$(fBits)
    IF fExp = 0 AND fMant = 0 THEN PRINT "  -> Zero"
    PRINT

    fVal = 1.0
    fStr = MKD$(fVal)
    fBits = ASC(LEFT$(fStr, 1))
    fBits = fBits + ASC(MID$(fStr, 2, 1)) * 256
    fBits = fBits + ASC(MID$(fStr, 3, 1)) * 65536
    fBits = fBits + ASC(MID$(fStr, 4, 1)) * 16777216
    fExp = (fBits / 8388608) AND 255
    fMant = fBits AND 8388607

    PRINT "FLOAT: 1.0"
    PRINT "  Exponent: " & STR$(fExp) & " (actual: " & STR$(fExp - 127) & ")"
    PRINT "  Mantissa: " & FORMAT$(fMant) & " (should be 0 for 1.0)"
    PRINT

    PRINT "Special values:"
    fBits = 2139095040
    fExp = (fBits / 8388608) AND 255
    fMant = fBits AND 8388607
    IF fExp = 255 AND fMant = 0 THEN PRINT "  Bits " & FORMAT$(fBits) & " -> +Infinity"
    fBits = 4286578688
    fExp = (fBits / 8388608) AND 255
    fMant = fBits AND 8388607
    IF fExp = 255 AND fMant = 0 THEN PRINT "  Bits " & FORMAT$(fBits) & " -> -Infinity"
    fBits = 2143289344
    fExp = (fBits / 8388608) AND 255
    fMant = fBits AND 8388607
    IF fExp = 255 AND fMant > 0 THEN PRINT "  Bits " & FORMAT$(fBits) & " -> NaN (quiet)"

    PRINT
    PRINT "IEEE 754 dump complete."

    FUNCTION = 0
END FUNCTION
