# How do we check if two bits are different?

## Recommended Posts

I know we can't use this:

if a{0} <> a{1} then blah blah blah

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:

if a{0} then if !a{1} then goto __Pause_Game

if !a{0} then if a{1} then goto __Pause_Game

Is that the best way to do it?

Thanks.

Edited by Random Terrain

##### 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...

```temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
```

##### 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...

```temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
```

Thanks. Since it uses division, does that mean it's a little slower?

##### 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 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...

```temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
```

Thanks. Since it uses division, does that mean it's a little slower?

RT,

Bitwise operations are fast but I think it would be easier to just evaluate the bitwise expression if it is handled:

if (a{0} ^ !a{1}) then goto __Pause_Game

##### 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.

OK, thanks. I better put this on the bB page too.

RT,

Bitwise operations are fast but I think it would be easier to just evaluate the bitwise expression if it is handled:

if (a{0} ^ !a{1}) then goto __Pause_Game

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

```temp1=a^(a/2)
if temp1{0} then goto __Pause_Game
```

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 on other sites
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?)

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

OK, thanks. I better put this on the bB page too.

RT,

Bitwise operations are fast but I think it would be easier to just evaluate the bitwise expression if it is handled:

if (a{0} ^ !a{1}) then goto __Pause_Game

Thanks, but I don't think we can do that with batari basic:

http://www.randomter...mmands.html#bit

Sorry RT,

I meant:

if(a{0}^a{1}) then ...

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 on other sites
Sorry RT,

I meant:

if(a{0}^a{1}) then ...

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.

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 on other sites
Sorry RT,

I meant:

if(a{0}^a{1}) then ...

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.

Check out the "Did You Know?" box here:

http://www.randomter...mmands.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.

You're right, you can't use ^ that way, it flips two arguments I thought it was a comparison operator

##### Share on other sites
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?)

bB automatically turns a divide with a power-of-2 into a number of LSR bit-shifts.

##### Share on other sites

Very nice bogax!

My solution was way off

Edited by ScumSoft

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

```if (a + 1)& 2 then goto __pause_game

__pause_game

```

produces

```.L00 ; if ( a + 1 ) & 2 then goto __pause_game

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

.skipL00
.
;

.__pause_game
; __pause_game

```

That ought to be something like what you want

Edited by bogax

##### Share on other sites

```if a/2 ^ a & 1 then goto __pause_game

__pause_game

```

```.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

```

## Join the conversation

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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