bitNot oddness

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

bitNot oddness

Post by joel.epsteinBUS31vi » Mon Jan 07, 2013 4:11 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9745
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: bitNot oddness

Post by dunbarx » Mon Jan 07, 2013 4:56 pm

Wierd.

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
The target field "hh" has:

4294967294
4294967293
4294967292
4294967291
4294967290
4294967289
4294967288
4294967287
4294967286
4294967285

None of this helps, of course.

Craig Newman

joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

Re: bitNot oddness

Post by joel.epsteinBUS31vi » Mon Jan 07, 2013 5:07 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9745
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: bitNot oddness

Post by dunbarx » Mon Jan 07, 2013 5:26 pm

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

gpb01
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Sat Jun 04, 2011 5:41 pm
Location: Switzerland

Re: bitNot oddness

Post by gpb01 » Mon Jan 07, 2013 5:39 pm

Mmm ... I think you have to revise a little bit the Boolean mathematics ... :D

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: bitNot oddness

Post by sturgis » Mon Jan 07, 2013 6:16 pm

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:

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
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)

gpb01
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Sat Jun 04, 2011 5:41 pm
Location: Switzerland

Re: bitNot oddness

Post by gpb01 » Mon Jan 07, 2013 6:29 pm

sturgis wrote: ... perhaps a roll your own solution might be in order.
...
Come on sturgis ... all this just to do a binary NOT ? :D :D :D

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: bitNot oddness

Post by sturgis » Mon Jan 07, 2013 6:48 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: bitNot oddness

Post by sturgis » Mon Jan 07, 2013 6:54 pm

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?
gpb01 wrote:
sturgis wrote: ... perhaps a roll your own solution might be in order.
...
Come on sturgis ... all this just to do a binary NOT ? :D :D :D

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

gpb01
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Sat Jun 04, 2011 5:41 pm
Location: Switzerland

Re: bitNot oddness

Post by gpb01 » Mon Jan 07, 2013 7:29 pm

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. ...
Hi sturgis,
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
... quite easy :)

Guglielmo

joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

Re: bitNot oddness

Post by joel.epsteinBUS31vi » Mon Jan 07, 2013 7:34 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9745
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: bitNot oddness

Post by dunbarx » Mon Jan 07, 2013 7:55 pm

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

gpb01
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Sat Jun 04, 2011 5:41 pm
Location: Switzerland

Re: bitNot oddness

Post by gpb01 » Mon Jan 07, 2013 8:33 pm

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:

Code: Select all

put (bitNot 1) bitAnd 4 into myVar
Thanks for your continued assistance.
Joel
Hi 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
Guglielmo

joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

Re: bitNot oddness

Post by joel.epsteinBUS31vi » Mon Jan 07, 2013 8:37 pm

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

gpb01
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Sat Jun 04, 2011 5:41 pm
Location: Switzerland

Re: bitNot oddness

Post by gpb01 » Mon Jan 07, 2013 9:14 pm

dunbarx wrote: ...
Anyway, the dictionary is very misleading, even if the bitnot operator does have that method to its madness.
...
Craig Newman
Hi Craig,
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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”