bitAnd Oddity?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
bitAnd Oddity?
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
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

- Posts: 10284
- Joined: Fri Feb 19, 2010 10:17 am
Re: bitAnd Oddity?
Probably a stupid question: but, what do you mean by
?normalize negative numbers
-
richmond62
- Livecode Opensource Backer

- Posts: 10284
- Joined: Fri Feb 19, 2010 10:17 am
Re: bitAnd Oddity?
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].-
richmond62
- Livecode Opensource Backer

- Posts: 10284
- Joined: Fri Feb 19, 2010 10:17 am
Re: bitAnd Oddity?
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
https://miniwebtool.com/bitwise-calculator/
-
richmond62
- Livecode Opensource Backer

- Posts: 10284
- Joined: Fri Feb 19, 2010 10:17 am
Re: bitAnd Oddity?
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
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

- Posts: 10284
- Joined: Fri Feb 19, 2010 10:17 am
Re: bitAnd Oddity?
It would be interesting to look at HyperCard and MetaCard in this respect.
-
richmond62
- Livecode Opensource Backer

- Posts: 10284
- Joined: Fri Feb 19, 2010 10:17 am
Re: bitAnd Oddity?
Richmond, "bitAnd" was not in the HC lexicon. It was introduced in v.1.0 of LC.
Craig
Craig
-
richmond62
- Livecode Opensource Backer

- Posts: 10284
- Joined: Fri Feb 19, 2010 10:17 am
Re: bitAnd Oddity?
I learn something every day: so 'twasn't in MetaCard?
Re: bitAnd Oddity?
Just checked, it was/is!
