# How do we check if two bits are different?

14 replies to this topic

### #1 Random TerrainOFFLINE

Random Terrain

Visual batari Basic User

• 26,320 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Wed Sep 28, 2011 2:32 AM

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, Wed Sep 28, 2011 2:46 AM.

### #2 RevEngOFFLINE

RevEng

River Patroller

• 3,711 posts
• Bitnik

Posted Wed Sep 28, 2011 8:54 AM

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

### #3 Random TerrainOFFLINE

Random Terrain

Visual batari Basic User

• Topic Starter
• 26,320 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Wed Sep 28, 2011 9:23 AM

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?

### #4 RevEngOFFLINE

RevEng

River Patroller

• 3,711 posts
• Bitnik

Posted Wed Sep 28, 2011 9:50 AM

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.

### #5 Mr SQLOFFLINE

Mr SQL

Dragonstomper

• 946 posts

Posted Wed Sep 28, 2011 10:59 AM

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

### #6 Random TerrainOFFLINE

Random Terrain

Visual batari Basic User

• Topic Starter
• 26,320 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Wed Sep 28, 2011 2:01 PM

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

### #7 EmehrOFFLINE

Emehr

River Patroller

• 3,961 posts
• Happiness is... a chomping Pac-Man!
• Location:An obscure body in the SK system

Posted Wed Sep 28, 2011 2:21 PM

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

### #8 Random TerrainOFFLINE

Random Terrain

Visual batari Basic User

• Topic Starter
• 26,320 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Wed Sep 28, 2011 2:37 PM

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.randomter...s.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.randomter...#points_roll_up

### #9 Mr SQLOFFLINE

Mr SQL

Dragonstomper

• 946 posts

Posted Wed Sep 28, 2011 2:37 PM

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.

### #10 Random TerrainOFFLINE

Random Terrain

Visual batari Basic User

• Topic Starter
• 26,320 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Wed Sep 28, 2011 2:44 PM

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.

Edited by Random Terrain, Wed Sep 28, 2011 2:45 PM.

### #11 Mr SQLOFFLINE

Mr SQL

Dragonstomper

• 946 posts

Posted Wed Sep 28, 2011 2:50 PM

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

### #12 RevEngOFFLINE

RevEng

River Patroller

• 3,711 posts
• Bitnik

Posted Wed Sep 28, 2011 4:38 PM

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.

### #13 ScumSoftOFFLINE

ScumSoft

Moonsweeper

• 365 posts
• Location:Polysorbate 60

Posted Wed Sep 28, 2011 5:56 PM

Very nice bogax!
My solution was way off

Edited by ScumSoft, Sat Oct 1, 2011 3:45 AM.

### #14 bogaxOFFLINE

bogax

Dragonstomper

• 601 posts

Posted Fri Sep 30, 2011 4:48 PM

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, Fri Sep 30, 2011 4:50 PM.

### #15 bogaxOFFLINE

bogax

Dragonstomper

• 601 posts

Posted Fri Sep 30, 2011 10:16 PM

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

```

#### 0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users