GLBasic forum

Codesnippets => Math => Topic started by: Hemlos on 2008-Oct-16

Title: BOOLEAN LOGIC Functions
Post by: Hemlos on 2008-Oct-16
bNOT correct usage is posted at bottom of thread.
Title: Re: BOOLEAN LOGIC Functions
Post by: Moru on 2008-Oct-16
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.
Title: Re: BOOLEAN LOGIC Functions
Post by: Kitty Hello on 2008-Oct-20
Aye, fixed in next version.
Title: Re: BOOLEAN LOGIC Functions
Post by: Kitty Hello on 2008-Oct-27
uhm... can you give a simple sample, where bNOT does not return what you think?
Title: Re: BOOLEAN LOGIC Functions
Post by: Moru on 2008-Oct-27
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
Title: Re: BOOLEAN LOGIC Functions
Post by: Kitty Hello on 2008-Oct-27
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
Title: Re: BOOLEAN LOGIC Functions
Post by: Kitty Hello on 2008-Oct-28
~1 = -2. That's correct.
Do you want it unsigned? Then use INLINE. The integers are interpreted as signed integers.
Title: Re: BOOLEAN LOGIC Functions
Post by: Kitty Hello on 2008-Oct-28
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?
Title: Re: BOOLEAN LOGIC Functions
Post by: Kitty Hello on 2008-Oct-28
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.
Title: Re: BOOLEAN LOGIC Functions
Post by: Hemlos on 2008-Oct-28
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:
Title: Re: BOOLEAN LOGIC Functions
Post by: Hemlos on 2008-Oct-28

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