Page 1 of 1

bitAnd Oddity?

Posted: Sat Dec 13, 2025 1:26 pm
by Hutchboy
Hi,

I ran into a debugging situation (implementing my AppleSoft Basic Interpreter) using the bitAnd LiveCode operator.

Here is the expression I want to evaluate: (-5 bitAnd 65535)

Here is my analysis of what the result should be:

-5 (32-bit) 11111111111111111111111111111011
AND 65535 00000000000000001111111111111111
--------------------------------------------------
Result 00000000000000001111111111111011
Dec = 65531

However, in the message box, "put -5 bitAnd 65535" results in a value of 0. A quick internet search indicates 65531 should be the result.

This is a subtle bug, if it is a bug at all, but it suggests LiveCode's bitAnd does not correctly normalize negative numbers. I am a bit out in the weeds here and there might be something I am missing but I would appreciate your inputs.

Thanks, Mike

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 2:05 pm
by richmond62
Probably a stupid question: but, what do you mean by
normalize negative numbers
?

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 2:17 pm
by richmond62

Code: Select all

on mouseUp
   put empty into fld "f1"
   put empty into fld "f2"
   put empty into fld "f3"
   put baseConvert((-5),10,2) into fld "f1"
   put baseConvert((-5),10,2) into N1
   put baseConvert(65535,10,2) into fld "f2"
   put baseConvert(65535,10,2) into N2
   put N1 + N2 into N3
   put baseConvert(N3,2,10) into fld "f3"
end mouseUp[code]
-
[attachment=0]SS 2025-12-13 at 15.16.35.png[/attachment]
-
So, obviously I do NOT understand [b][size=120]bitwise AND[/size][/b].

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 2:32 pm
by richmond62

Code: Select all

on mouseUp
   put empty into fld "f1"
   put empty into fld "f2"
   put empty into fld "f3"
   put empty into fld "f4"
   put empty into fld "f5"
   put empty into fld "f6"
   put "bitAnd" && (-5 bitAnd 65535) into fld "f4"
   put "bitOr" && (-5 bitOr 65535) into fld "f5"
   put "bitXor" && (-5 bitXor 65535) into fld "f6"
   put baseConvert((-5),10,2) into fld "f1"
   put baseConvert((-5),10,2) into N1
   put baseConvert(65535,10,2) into fld "f2"
   put baseConvert(65535,10,2) into N2
   put N1 + N2 into N3
   put baseConvert(N3,2,10) into fld "f3"
end mouseUp
-
SS 2025-12-13 at 15.31.31.png
-
https://miniwebtool.com/bitwise-calculator/

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 2:35 pm
by richmond62
SS 2025-12-13 at 15.33.44.png
-
Ouch.

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 4:56 pm
by Hutchboy
Thanks for the quick response,

By "not correctly normalize negative numbers" I mean that in LiveCode bitAnd is not a masking operator. For a 16 bit register a -1 should wrap to 65535, which is what happens with this operator in other languages, including AppleSoft BASIC. In LiveCode for negative arguments nothing automatically wraps. I'm actually not sure whether it is even a problem when programming LiveCode, it just does not seem to be "standard".

So, for my interpreter I had to create a helper function to use modulo arithmetic to handle bitAnd operations with negative arguments to get the right AppleSoft BASIC behavior:

function toAppleSoft16 pVal
put trunc(asNumber(pVal)) into t
return ((t mod 65536) + 65536) mod 65536
end toAppleSoft16

I just got interested in why LiveCode bitAnd operator works the way it does. I thought it could possibly be a bug but it has at least made me wary using bit operators until I understand them better.

Thanks,
Mike

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 5:48 pm
by richmond62
It would be interesting to look at HyperCard and MetaCard in this respect.

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 6:13 pm
by richmond62
No mention in here:
-
TheCompleteHypercardHandbook_0000.jpg

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 6:41 pm
by dunbarx
Richmond, "bitAnd" was not in the HC lexicon. It was introduced in v.1.0 of LC.

Craig

Re: bitAnd Oddity?

Posted: Sat Dec 13, 2025 7:14 pm
by richmond62
I learn something every day: so 'twasn't in MetaCard?

Re: bitAnd Oddity?

Posted: Sun Dec 14, 2025 2:53 am
by Klaus
Just checked, it was/is!