BOOLEAN LOGIC Functions

Previous topic - Next topic

Hemlos

bNOT correct usage is posted at bottom of thread.
Bing ChatGpt is pretty smart :O

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) Select
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


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) Select
// --------------------------------- //
// 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

Kitty Hello

Code (glbasic) Select

FUNCTION DecToBin$: d%
LOCAL h$
FOR id% = 31 TO 0 STEP -1
IF bAND( d% , POW(2,id%))
h$ = h$ + "1"
ELSE
h$ = h$ + "0"
ENDIF
NEXT
RETURN h$
ENDFUNCTION

Kitty Hello

~1 = -2. That's correct.
Do you want it unsigned? Then use INLINE. The integers are interpreted as signed integers.

Kitty Hello

Code (glbasic) Select

const char* binary(int n)
{
static char buffer[1024];
char* pBuf = buffer;
for(int x = 31; x>=0; --x)
{
if(n & (1<<x))
*pBuf='1';
else
*pBuf='0';
++pBuf;
}
*pBuf='\0';
return buffer;
}


int main()
{

int i = 1;
int j = ~i; // boolean not operation
printf("i = %2d = %s\n", i, binary(i));
printf("j = %2d = %s\n", j, binary(j));

return 0;
}


Output:
Code (glbasic) Select

i =  1 = 00000000000000000000000000000001
j = -2 = 11111111111111111111111111111110


Agree?

Kitty Hello

I think you misunderstood me.
int i = -2;
unsigned int ui = 0xfffffffe;

The bits of i and ui are totally the same. It's just a matter of what you output.
The hex function - as mentioned above - you used was wrong, however.

Hemlos

#9
Quote from: Kitty Hello on 2008-Oct-28

The hex function - as mentioned above - you used was wrong, however.

Ahhh i think i understand now, sorry if im slow upstairs, the midi headers zapped my brain last week  :zzz:

Basically, what i made is ascii integer boolean logic. ill post the whole library in math thread one of these days. :bed:
Bing ChatGpt is pretty smart :O

Hemlos

#10

QuoteStart value bin: 01111111111111110111111111111111
After Not bin:   01111111111111111000000000000000
Expected:        10000000000000001000000000000000
Ahh got it to work:

a$=bNOT(2147450879)
b$=DecToBin$(a$)
PRINT a$,10,10
PRINT b$,10,100

result: 10000000000000001000000000000000

Code (Thanks Gernot) Select

FUNCTION DecToBin$: d%
LOCAL h$
FOR id% = 31 TO 0 STEP -1
IF bAND( d% , POW(2,id%))
h$ = h$ + "1"
ELSE
h$ = h$ + "0"
ENDIF
NEXT
RETURN h$
ENDFUNCTION

Bing ChatGpt is pretty smart :O