bitNot oddness
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 135
- Joined: Thu Sep 13, 2012 10:25 pm
bitNot oddness
Hi all -
Running into something I can't explain using LiveCode 5.5.3 on the Mac.
this expression:
bitNot 1
is yielding the result 4294967294
yet the documentation states (as I would expect) that it should yield 0
Is there something I need to change to make the program evaluate the expression correctly?
Thanks.
Joel
Running into something I can't explain using LiveCode 5.5.3 on the Mac.
this expression:
bitNot 1
is yielding the result 4294967294
yet the documentation states (as I would expect) that it should yield 0
Is there something I need to change to make the program evaluate the expression correctly?
Thanks.
Joel
-
- VIP Livecode Opensource Backer
- Posts: 9745
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: bitNot oddness
Wierd.
There is a method to this madness. I made a test:
The target field "hh" has:
4294967294
4294967293
4294967292
4294967291
4294967290
4294967289
4294967288
4294967287
4294967286
4294967285
None of this helps, of course.
Craig Newman
There is a method to this madness. I made a test:
Code: Select all
on mouseUp
repeat with y = 1 to 10
put bitnot y into line y of fld "hh"
end repeat
end mouseUp
4294967294
4294967293
4294967292
4294967291
4294967290
4294967289
4294967288
4294967287
4294967286
4294967285
None of this helps, of course.
Craig Newman
-
- Posts: 135
- Joined: Thu Sep 13, 2012 10:25 pm
Re: bitNot oddness
Yeah, this has to do with the fact that it's on a 32 bit system.
Somehow, I have to mask off the left-most bits I don't need before doing the bitNot.
But I'm a bit of a noob about this whole bitwise stuff, so I'm floundering some...
Thanks.
Joel
Somehow, I have to mask off the left-most bits I don't need before doing the bitNot.
But I'm a bit of a noob about this whole bitwise stuff, so I'm floundering some...
Thanks.
Joel
-
- VIP Livecode Opensource Backer
- Posts: 9745
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: bitNot oddness
Doing a bitnot on the number 3, (binary "11") ought to return, again in binary, "00", and convert to "0". But it returns "4294967292"
How will masking help? I do not see a pattern here at all, since the bitnot operator should return similar values for many arguments, like "1", "3" and "7". Since these are all supposed to be reconverted to decimal, they would all return "0".
Anyone?
Craig Newman
How will masking help? I do not see a pattern here at all, since the bitnot operator should return similar values for many arguments, like "1", "3" and "7". Since these are all supposed to be reconverted to decimal, they would all return "0".
Anyone?
Craig Newman
-
- VIP Livecode Opensource Backer
- Posts: 281
- Joined: Sat Jun 04, 2011 5:41 pm
- Location: Switzerland
Re: bitNot oddness
Mmm ... I think you have to revise a little bit the Boolean mathematics ...
The system work with 32 bits so 3 is NOT 11 but is 0000 0000 0000 0000 0000 0000 0000 0011. Now, if you negate this you have 1111 1111 1111 1111 1111 1111 1111 1100 which is exactly 4294967292
If you want to use ONLY 2 bits (of 32) you have to mask the 30 most significant bits which you can do with a bitAnd with the mask 0000 0000 0000 0000 0000 0000 0000 0011 (decimal 3).
Hope this help
Guglielmo
The system work with 32 bits so 3 is NOT 11 but is 0000 0000 0000 0000 0000 0000 0000 0011. Now, if you negate this you have 1111 1111 1111 1111 1111 1111 1111 1100 which is exactly 4294967292
If you want to use ONLY 2 bits (of 32) you have to mask the 30 most significant bits which you can do with a bitAnd with the mask 0000 0000 0000 0000 0000 0000 0000 0011 (decimal 3).
Hope this help
Guglielmo
Re: bitNot oddness
Proper functioning of bitNot does seem to be broke. And as mentioned by guglielmo there is some logic behind the results, but since it doesn't behave as indicated in the dictionary, perhaps a roll your own solution might be in order.
Something as simple as this should work:
Largely untested, but don't see why it won't work.
EDITED: to fix the return value so that it returns using the original base.
EDIT 2: You'll want to use try/catch on your first base convert to make sure that the start base actually makes sense with the passed value (IE, can't use ff with a start base of 10, throws an error)
Something as simple as this should work:
Code: Select all
function newBitNot pNum,pStartBase -- accept a number and its base (10 for decimal of course)
put baseconvert(pNum,pStartbase,2) into tTmp -- convert it to binary
-- swap 0 for 1, 1 for 0.
repeat for each char tChar in tTmp
switch tChar
case 1
put 0 after newTmp
break
default
put 1 after newTmp
end switch
end repeat
return baseconvert(newTmp,2,pStartBase) -- return the new value in the original base.
end newBitNot
EDITED: to fix the return value so that it returns using the original base.
EDIT 2: You'll want to use try/catch on your first base convert to make sure that the start base actually makes sense with the passed value (IE, can't use ff with a start base of 10, throws an error)
-
- VIP Livecode Opensource Backer
- Posts: 281
- Joined: Sat Jun 04, 2011 5:41 pm
- Location: Switzerland
Re: bitNot oddness
Come on sturgis ... all this just to do a binary NOT ?sturgis wrote: ... perhaps a roll your own solution might be in order.
...
The bitNot work wery well, just remember that you are working with 32 bits so, if you want less bits, follow the bitNot with a bitAnd to remove the unused bits
Two statements ... simple and quickly !
Guglielmo
Re: bitNot oddness
Heres the version with try/catch.
Code: Select all
function newBitNot pNum,pStartBase
try
put baseconvert(pNum,pStartbase,2) into tTmp
catch tErr
return line (item 1 of line 1 of tErr) of the cErrorsList of card 1 of stack "revErrorDisplay"
end try
repeat for each char tChar in tTmp
switch tChar
case 1
put 0 after newTmp
break
default
put 1 after newTmp
end switch
end repeat
put cr & newTmp after msg
return baseconvert(newTmp,2,pStartBase)
end newBitNot
Re: bitNot oddness
Hey it works. and "All this" isn't all that much. Yours works fine and is probably the easiest solution. Mine works fine too. /shrug. I'm more interested in the fact that the functionality doesn't match the dictionary description.
On that note, if you don't mind it would be helpful if you would add an explanation to the notes section of the dictionary for this, for people like me to better understand what is up.
Since it is NOT working as indicated in the docs, (all the other bit operators are working as expected) does this make it a bug?
On that note, if you don't mind it would be helpful if you would add an explanation to the notes section of the dictionary for this, for people like me to better understand what is up.
Since it is NOT working as indicated in the docs, (all the other bit operators are working as expected) does this make it a bug?
gpb01 wrote:Come on sturgis ... all this just to do a binary NOT ?sturgis wrote: ... perhaps a roll your own solution might be in order.
...
The bitNot work wery well, just remember that you are working with 32 bits so, if you want less bits, follow the bitNot with a bitAnd to remove the unused bits
Two statements ... simple and quickly !
Guglielmo
-
- VIP Livecode Opensource Backer
- Posts: 281
- Joined: Sat Jun 04, 2011 5:41 pm
- Location: Switzerland
Re: bitNot oddness
Hi sturgis,sturgis wrote: ... On that note, if you don't mind it would be helpful if you would add an explanation to the notes section of the dictionary for this, for people like me to better understand what is up. ...
note added ... now waiting for approval
Anyway, I repeat, is very easy to write ONE statement (... and not two as I wrote before ) on which you have bitNot and next bitAnd.
E.g. in the case of joel.epsteinBUS31vi, because he use just ONE bit (mask = 0000 0000 0000 0000 0000 0000 0000 0001), is sufficient to write :
Code: Select all
put (bitNot 1) bitAnd 1 into myVar
Guglielmo
-
- Posts: 135
- Joined: Thu Sep 13, 2012 10:25 pm
Re: bitNot oddness
Thanks, all, I'm learning quite a bit here.
So if I want to mask to 3 digits of binary precision, would I do this:
put (bitNot 1) bitAnd 4 into myVar
Thanks for your continued assistance.
Joel
So if I want to mask to 3 digits of binary precision, would I do this:
put (bitNot 1) bitAnd 4 into myVar
Thanks for your continued assistance.
Joel
-
- VIP Livecode Opensource Backer
- Posts: 9745
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: bitNot oddness
All this is good stuff. Now I get the early "32 bit" comment by joel.epsteinBUS31vi.
My question is: why does the ostensible final step in the normal process convert to decimal? Forget for a moment that it does not.
Shouldn't bitNot 5 (or bitnot 101) yield "010" and be done? Or, if you prefer, "0000 0000 0000 0000 0000 0000 0000 0010"?
Anyway, the dictionary is very misleading, even if the bitnot operator does have that method to its madness.
Bitand works as advertised, yielding a decimal with leading zeros masked, though I do not see the point of knowing that 6 bitand 5 = 6 bitand 4.
Craig Newman
My question is: why does the ostensible final step in the normal process convert to decimal? Forget for a moment that it does not.
Shouldn't bitNot 5 (or bitnot 101) yield "010" and be done? Or, if you prefer, "0000 0000 0000 0000 0000 0000 0000 0010"?
Anyway, the dictionary is very misleading, even if the bitnot operator does have that method to its madness.
Bitand works as advertised, yielding a decimal with leading zeros masked, though I do not see the point of knowing that 6 bitand 5 = 6 bitand 4.
Craig Newman
-
- VIP Livecode Opensource Backer
- Posts: 281
- Joined: Sat Jun 04, 2011 5:41 pm
- Location: Switzerland
Re: bitNot oddness
Hi Joel,joel.epsteinBUS31vi wrote:Thanks, all, I'm learning quite a bit here.
So if I want to mask to 3 digits of binary precision, would I do this:
Thanks for your continued assistance.Code: Select all
put (bitNot 1) bitAnd 4 into myVar
Joel
what you means for 3 digits ? 3 bits ? If you need 3 bits you must bitAnd with 0000 0000 0000 0000 0000 0000 0000 0111 which is decimal 7 and not 4 so your code will be :
Code: Select all
put (bitNot 1) bitAnd 7 into myVar
-
- Posts: 135
- Joined: Thu Sep 13, 2012 10:25 pm
Re: bitNot oddness
Yep. That's what I meant. Thanks for interpreting my intentions and pointing me in the right direction. I think I've got it now.
I appreciate your help.
Joel
I appreciate your help.
Joel
-
- VIP Livecode Opensource Backer
- Posts: 281
- Joined: Sat Jun 04, 2011 5:41 pm
- Location: Switzerland
Re: bitNot oddness
Hi Craig,dunbarx wrote: ...
Anyway, the dictionary is very misleading, even if the bitnot operator does have that method to its madness.
...
Craig Newman
I agree that the behavior of bitNot is not normal and the dictionary give incorrect info and ... also on my opinion the operaror should take charge only of used bits and not of all 32 !
But because at moment he take in charge ALL the 32 bits ... is necessary to use the bitAnd to obtain the correct result ...
Guglielmo