Jump to content
Random Terrain

How do we check if two bits are different?

Recommended Posts

I know we can't use this:

[size=5][b]if a{0} <> a{1} then blah blah blah[/b][/size]

If I knew how to compare two bits, I forgot and I don't see it on the bB page.

The only thing I can think of is this:

[size=5][b]if a{0} then if !a{1} then goto __Pause_Game[/b][/size]

[size=5][b]if !a{0} then if a{1} then goto __Paus[/b][/size][size=5][b]e_Gam[/b][/size][size=5][b]e[/b][/size]

Is that the best way to do it?



Thanks. Edited by Random Terrain

Share this post


Link to post
Share on other sites
You can xor them together. If they differ, the result is 1. If they are the same, the result is 0.

As far as efficiency, if the bits are neighbors you come out ahead 9 bytes of rom space, for your example...

[code]
temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
[/code]

Share this post


Link to post
Share on other sites
[quote name='RevEng' timestamp='1317221690' post='2379767']
You can xor them together. If they differ, the result is 1. If they are the same, the result is 0.

As far as efficiency, if the bits are neighbors you come out ahead 9 bytes of rom space, for your example...

[code]
temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
[/code]
[/quote]
Thanks. Since it uses division, does that mean it's a little slower?

Share this post


Link to post
Share on other sites
Yours is a bit faster because the comparisons avoid some work that mine does in constant time. (a division by 2 in this case only adds 2 cycles) I think your worst case scenario is 24 cycles, and mine is 33 cycles.

Share this post


Link to post
Share on other sites
[quote name='Random Terrain' timestamp='1317223399' post='2379777']
[quote name='RevEng' timestamp='1317221690' post='2379767']
You can xor them together. If they differ, the result is 1. If they are the same, the result is 0.

As far as efficiency, if the bits are neighbors you come out ahead 9 bytes of rom space, for your example...

[code]
temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
[/code]
[/quote]
Thanks. Since it uses division, does that mean it's a little slower?
[/quote]
RT,
Bitwise operations are fast but I think it would be easier to just evaluate the bitwise expression if it is handled:
[size=5][b]if (a{0} ^ !a{1}) then goto __Pause_Game[/b][/size]

Share this post


Link to post
Share on other sites
[quote name='RevEng' timestamp='1317225042' post='2379787']
Yours is a bit faster because the comparisons avoid some work that mine does in constant time. (a division by 2 in this case only adds 2 cycles) I think your worst case scenario is 24 cycles, and mine is 33 cycles.
[/quote]
OK, thanks. I better put this on the bB page too.



[quote name='Mr SQL' timestamp='1317229144' post='2379830']RT,
Bitwise operations are fast but I think it would be easier to just evaluate the bitwise expression if it is handled:
[size=5][b]if (a{0} ^ !a{1}) then goto __Pause_Game[/b][/size]
[/quote]
Thanks, but I don't think we can do that with batari basic:

http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#bit

Share this post


Link to post
Share on other sites
[quote name='RevEng' timestamp='1317221690' post='2379767']
You can xor them together. If they differ, the result is 1. If they are the same, the result is 0.

As far as efficiency, if the bits are neighbors you come out ahead 9 bytes of rom space, for your example...

[code]
temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
[/code]
[/quote]
Can you just bit-shift right once instead of dividing by 2? Or would that use the same number of cycles? (does bBasic support bit shifting?)

Share this post


Link to post
Share on other sites
[quote name='Emehr' timestamp='1317241302' post='2379948']Can you just bit-shift right once instead of dividing by 2? Or would that use the same number of cycles? (does bBasic support bit shifting?)[/quote]
We can use assembly language:

http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#assembly

But I usually try to keep things in bB mode for the bB page to avoid confusing new users. Unless it's something cool like this:

http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#points_roll_up

Share this post


Link to post
Share on other sites
[quote name='Random Terrain' timestamp='1317240087' post='2379938']
[quote name='RevEng' timestamp='1317225042' post='2379787']
Yours is a bit faster because the comparisons avoid some work that mine does in constant time. (a division by 2 in this case only adds 2 cycles) I think your worst case scenario is 24 cycles, and mine is 33 cycles.
[/quote]
OK, thanks. I better put this on the bB page too.



[quote name='Mr SQL' timestamp='1317229144' post='2379830']RT,
Bitwise operations are fast but I think it would be easier to just evaluate the bitwise expression if it is handled:
[size=5][b]if (a{0} ^ !a{1}) then goto __Pause_Game[/b][/size]
[/quote]
Thanks, but I don't think we can do that with batari basic:

[url="http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#bit"]http://www.randomter...mmands.html#bit[/url]
[/quote]
Sorry RT,
I meant:
[b]if(a{0}^a{1}) then [/b]...
must have left the other operator in there; should work without the parenthesis if the compiler doesn't like them since only a single term is in the expression.

Share this post


Link to post
Share on other sites
[quote name='Mr SQL' timestamp='1317242258' post='2379957']Sorry RT,
I meant:
[b]if(a{0}^a{1}) then [/b]...
must have left the other operator in there; should work without the parenthesis if the compiler doesn't like them since only a single term is in the expression.[/quote]
Check out the "Did You Know?" box here:

http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#bit

Unless we were never told about it or something has changed, I think we are very limited when it comes to bit operations and if-thens. Edited by Random Terrain

Share this post


Link to post
Share on other sites
[quote name='Random Terrain' timestamp='1317242640' post='2379963']
[quote name='Mr SQL' timestamp='1317242258' post='2379957']Sorry RT,
I meant:
[b]if(a{0}^a{1}) then [/b]...
must have left the other operator in there; should work without the parenthesis if the compiler doesn't like them since only a single term is in the expression.[/quote]
Check out the "Did You Know?" box here:

[url="http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#bit"]http://www.randomter...mmands.html#bit[/url]

Unless we were never told about it or something has changed, I think we are very limited when it comes to bit operations and if-thens.
[/quote]
You're right, you can't use ^ that way, it flips two arguments I thought it was a comparison operator :(
I think your first solution has the least overhead.

Share this post


Link to post
Share on other sites
[quote name='Emehr' timestamp='1317241302' post='2379948']Can you just bit-shift right once instead of dividing by 2? Or would that use the same number of cycles? (does bBasic support bit shifting?)
[/quote]
bB automatically turns a divide with a power-of-2 into a number of LSR bit-shifts.

Share this post


Link to post
Share on other sites
Not sure I remember the conventions rightly, but assuming bit 0 is the
least significant bit ...

If you add a 1 to a bit you can, in effect, propagate it to a more significant
position via carry

If you add two bits together the sum is the XORing of the two

You can do both at once in this case by adding 1 to a

Then use & to isolate bit 1 to test it (ie a bitwise AND with 2)

[code]
if (a + 1)& 2 then goto __pause_game

__pause_game

[/code]

produces

[code]
.L00 ; if ( a + 1 ) & 2 then goto __pause_game

; complex statement detected
LDA a
CLC
ADC #1
AND #2
BEQ .skipL00
.condpart0
jmp .__pause_game

.skipL00
.
;

.__pause_game
; __pause_game

[/code]

That ought to be something like what you want Edited by bogax

Share this post


Link to post
Share on other sites
[code]
if a/2 ^ a & 1 then goto __pause_game

__pause_game

[/code]

[code]
.L00 ; if a / 2 ^ a & 1 then goto __pause_game

; complex condition detected
; complex statement detected
LDA a
lsr
EOR a
AND #1
BEQ .skipL00
.condpart0
jmp .__pause_game

.skipL00
.
;

.__pause_game
; __pause_game

[/code]

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...