bitAnd Oddity?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Hutchboy
Posts: 159
Joined: Wed Aug 01, 2018 2:57 pm
Contact:

bitAnd Oddity?

Post by Hutchboy » Sat Dec 13, 2025 1:26 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10284
Joined: Fri Feb 19, 2010 10:17 am

Re: bitAnd Oddity?

Post by richmond62 » Sat Dec 13, 2025 2:05 pm

Probably a stupid question: but, what do you mean by
normalize negative numbers
?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10284
Joined: Fri Feb 19, 2010 10:17 am

Re: bitAnd Oddity?

Post by richmond62 » Sat Dec 13, 2025 2:17 pm

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].
Attachments
SS 2025-12-13 at 15.16.35.png

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10284
Joined: Fri Feb 19, 2010 10:17 am

Re: bitAnd Oddity?

Post by richmond62 » Sat Dec 13, 2025 2:32 pm

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/

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10284
Joined: Fri Feb 19, 2010 10:17 am

Re: bitAnd Oddity?

Post by richmond62 » Sat Dec 13, 2025 2:35 pm

SS 2025-12-13 at 15.33.44.png
-
Ouch.

Hutchboy
Posts: 159
Joined: Wed Aug 01, 2018 2:57 pm
Contact:

Re: bitAnd Oddity?

Post by Hutchboy » Sat Dec 13, 2025 4:56 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10284
Joined: Fri Feb 19, 2010 10:17 am

Re: bitAnd Oddity?

Post by richmond62 » Sat Dec 13, 2025 5:48 pm

It would be interesting to look at HyperCard and MetaCard in this respect.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10284
Joined: Fri Feb 19, 2010 10:17 am

Re: bitAnd Oddity?

Post by richmond62 » Sat Dec 13, 2025 6:13 pm

No mention in here:
-
TheCompleteHypercardHandbook_0000.jpg

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10436
Joined: Wed May 06, 2009 2:28 pm

Re: bitAnd Oddity?

Post by dunbarx » Sat Dec 13, 2025 6:41 pm

Richmond, "bitAnd" was not in the HC lexicon. It was introduced in v.1.0 of LC.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10284
Joined: Fri Feb 19, 2010 10:17 am

Re: bitAnd Oddity?

Post by richmond62 » Sat Dec 13, 2025 7:14 pm

I learn something every day: so 'twasn't in MetaCard?

Klaus
Posts: 14272
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: bitAnd Oddity?

Post by Klaus » Sun Dec 14, 2025 2:53 am

Just checked, it was/is!

Post Reply