Codesnippets > Math

BOOLEAN LOGIC Functions

(1/3) > >>

Hemlos:
bNOT correct usage is posted at bottom of thread.

Moru:
I was bumping into those problem while doing LSB/MSB conversion for the Mappy loader. The reason for this is possibly because of signed numbers are involved.

--- Code: (glbasic) ---Start values: Dec:  2147450879  Hex: 7FFF7FFF
After Not:    Dec: -2147450880  Hex: 91118000
Expected:     Dec:  2147516416   Hex: 80008000

Start value bin: 01111111111111110111111111111111
After Not bin:   01111111111111111000000000000000
Expected:        10000000000000001000000000000000
--- End code ---

I'm not totally sure my binary and hex routines are correct with so large numbers though, haven't tested much above 16 bit.

Kitty Hello:
Aye, fixed in next version.

Kitty Hello:
uhm... can you give a simple sample, where bNOT does not return what you think?

Moru:

--- Code: (glbasic) ---// --------------------------------- //
// Project: Binary test
// Start: Thursday, October 16, 2008
// IDE Version: 5.360

LOCAL a, b, c

a = 0x7fff7fff
b = bNOT(a)

DEBUG "Org dec: " + a + "  Hex: " + DecToHex\$(a) + "\n"
DEBUG "Not dec: " + b + "  Hex: " + DecToHex\$(b) + "\n"
DEBUG "Org bin:   " + DecToBin\$(a) + "\n"
DEBUG "Not bin:   " + DecToBin\$(b) + "\n"
DEBUG "Expected: " + "10000000000000001000000000000000"

END

FUNCTION DecToHex\$: d
LOCAL h\$, t

WHILE d <> 0
t = bAND(d, 0xF)
d = INTEGER(d / 16)
IF t >= 0 AND t <= 9
h\$ = CHR\$(ASC("0") + t) + h\$
ELSE
h\$ = CHR\$(ASC("A") + (t-10)) + h\$
ENDIF
WEND
IF LEN(h\$)=0 THEN h\$ = "0"
RETURN h\$
ENDFUNCTION

FUNCTION DecToBin\$: d
LOCAL h\$, t

WHILE d <> 0
t = bAND(d, 0x1)
d = INTEGER(d / 2)
IF t >= 0 AND t <= 1
h\$ = CHR\$(ASC("0") + t) + h\$
ENDIF
WEND
IF LEN(h\$)=0 THEN h\$ = "0"
RETURN h\$
ENDFUNCTION
--- End code ---