Jump to content
Sign in to follow this  
yuppicide

Bit Operations

Recommended Posts

This is my first attempt at using Bit Operations to squeeze a few more variables as I've used up all 26 right now.

 

I have 3 or 4 of them I can combine into one.. they only use 0 or 1.

 

Do I need to do anything special?

 

In the beginning of my program I have:

 

w{0} = 0
w{1} = 0
w{2} = 0

 

Then when I wanted to use the I'd use them as normal variables:

 

if w{0} = 0 then gosub thisrouting

 

Etc..

 

When I compile in VBB I get a syntax error. This is the end of the message in VBB:

 

L0167 ; if w{2} = 0 then goto largemovegapleft

LDA w
AND #4
BEQ .skipL0167
.condpart56

 

Cannot I not do an if than statement like that?

  • Like 1

Share this post


Link to post
Share on other sites

You're setting them correctly, but you shouldn't do a compare to zero or one in the "if" statement...

 

rem change this...
if w{0} = 0 then gosub thisrouting

rem ...to this...
if !w{0} then gosub thisrouting

Share this post


Link to post
Share on other sites

Edit.. oops.. I should have read your little code.. I had only read the first sentence..

 

I don't get what the !w does.. isn't that supposed to mean not equal?

 

So I could do something like this?

 

if w{0} then gosub drawfishleft else drawfishright

 

So if w{0} = 0 it'll do drawfishleft if it's 1 then it'll drawfishright?

Edited by yuppicide

Share this post


Link to post
Share on other sites

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

 

  if a{0} then gosub moosegizzard
 if !a{0} then goto gameover

 

Notice that bit operations do not use an equal sign in if-then statements. And remember that curly brackets {} are used, not round brackets or parentheses ().

Share this post


Link to post
Share on other sites

Edit.. oops.. I should have read your little code.. I had only read the first sentence..

 

I don't get what the !w does.. isn't that supposed to mean not equal?

 

So I could do something like this?

 

if w{0} then gosub drawfishleft else drawfishright

 

So if w{0} = 0 it'll do drawfishleft if it's 1 then it'll drawfishright?

You've got it backwards-- in your example, if w{0} = 0 it would do drawfishright, else if w(0) = 1 it would do drawfishleft. Yes, ! is "not," but not necessarily "not equal"-- more exactly, it means "is not true." So for example, "if !w{0} then gosub thisrouting" means "if w{0} is not true, then gosub thisrouting." So what does that mean, "if w{0} is not true"? When is w{0} "true," and when is it "not true"? Simply put, generally speaking in computers 0=off=false=nottrue, so any nonzero value is "true." So if w{0} = 1, it's "true," and if w{0} = 0, it's "not true." So !w{0} is the same thing as w{0} = 0. If it helps, think of it as on or off, where on is true and off is false-- "if w{0}" means "if w{0} is on, or = 1," and "if !w{0}" means "if w{0} is not on, or is off, or = 0."

 

Michael

Share this post


Link to post
Share on other sites

That was a mouthful, Michael. Almost sounded like a limerick, but I understand thanks.

You're welcome. :) But I'd say I was wrong when I said

more exactly, it means "is not true."
It really means a "logical negation"-- the logical opposite of whatever follows it. So if something is true, !something is false-- but if something is false, !something is true.

 

In the 'if' statement, the 'then' portion will be performed only if the portion between 'if' and 'then' is true-- "if something then dothis" would mean "if 'something' is true then do 'dothis'." 'Something' could be an expression containing one or more variables, constants, and operators, as in "if a = b + c then dothis" (which would perform 'dothis' only if the expression "a = b + c" evaluates to a true condition-- i.e., "yes, a is equal to b plus c"). But 'something' could also be simpler, like a single variable. For example, "if a then dothis" means "if 'a' is true then do 'dothis." If you've only ever seen 'if' statements that contain expressions, then "if a then dothis" might look like an incomplete statement, but it's valid. In this case, 'a' will be false if it equals 0, and true if it equals anything other than 0, so "if a then dothis" is the same as "if a <> 0 then dothis," whereas "if !a then dothis" is the same as "if a = 0 then dothis."

 

I suppose the reason batari Basic uses something like "if w{0} then dothis" and "if !w{0} then dothis" for bit variables is because a bit can be only 0 or 1, so there's no need to check for any other values (like "w{0} = 36"), and "if w{0}" or "if !w{0}" is shorter than "if w{0} = 1" or "if w{0} = 0," respectively-- also, they're much simpler and quicker to parse.

 

Michael

Share this post


Link to post
Share on other sites

I suppose the reason batari Basic uses something like "if w{0} then dothis" and "if !w{0} then dothis" for bit variables is because a bit can be only 0 or 1, so there's no need to check for any other values (like "w{0} = 36"), and "if w{0}" or "if !w{0}" is shorter than "if w{0} = 1" or "if w{0} = 0," respectively-- also, they're much simpler and quicker to parse.

 

Yes, good thinking on that one.

Share this post


Link to post
Share on other sites

If we are talking about bit operations... Let me ask you guys, what is the easiest way to randomize a bit?

 

For example, we cannot do this:

 

w{0} = (rand&1)

 

...because rand will return a 8-bit number, right?

 

So I always end up doing something like this:

 

y = (rand&1)
if y = 0 then w{0} = 0 else w{0} = 1

 

Is there a better way?

 

Thanks!

Share this post


Link to post
Share on other sites

If we are talking about bit operations... Let me ask you guys, what is the easiest way to randomize a bit?

 

For example, we cannot do this:

 

w{0} = (rand&1)

 

...because rand will return a 8-bit number, right?

 

So I always end up doing something like this:

 

y = (rand&1)
if y = 0 then w{0} = 0 else w{0} = 1

 

Is there a better way?

 

Thanks!

The if-then can be avoided...

 

y = (rand&%00000001) : rem ** y is either %00000001 or %00000000

w = w ^ y : rem ** randomly flip the rightmost bit in w

 

[edit - fixed after proper caffeination level was reached.]

Edited by RevEng
  • Like 1

Share this post


Link to post
Share on other sites

If we are talking about bit operations... Let me ask you guys, what is the easiest way to randomize a bit?

 

For example, we cannot do this:

 

w{0} = (rand&1)

 

...because rand will return a 8-bit number, right?

 

So I always end up doing something like this:

 

y = (rand&1)
if y = 0 then w{0} = 0 else w{0} = 1

 

Is there a better way?

 

Thanks!

The if-then can be avoided...

 

y = (rand|%11111110) : rem ** y is either %11111111 or %11111110

w = w & y : rem ** use y to randomly mask off the last bit in w

 

Really nice! That took me to Bitwise Operators in Random Terrain, thank you =)

Share this post


Link to post
Share on other sites
y = (rand|%11111110) : rem ** y is either %11111111 or %11111110

w = w & y : rem ** use y to randomly mask off the last bit in w

And am I confused or would that change depending on the bit I wanted to randomize? For example, if I wanted a random number for w{7} instead of w{0}, I'd use the following, right?

 

  rem  ****************************************************************
  rem  *  Random number for w{7}
  rem  *
  temp5 = (rand|%01111111)
  w = w & temp5

Share this post


Link to post
Share on other sites

Yup, that's right. Except my example was wrong. :ponder:

 

Here's the working version of the code. :P

 

  rem  ****************************************************************
  rem  *  Random number for w{7}
  rem  *
  temp5 = (rand&%10000000)
  w = w ^ temp5

 

I'll fix the example in my previous post.

Share this post


Link to post
Share on other sites

Yup, that's right. Except my example was wrong. :ponder:

 

Here's the working version of the code. :P

 

  rem  ****************************************************************
  rem  *  Random number for w{7}
  rem  *
  temp5 = (rand&%10000000)
  w = w ^ temp5

 

I'll fix the example in my previous post.

Thanks. I'll add that somewhere to the bB page later today.

Share this post


Link to post
Share on other sites

Yup, that's right. Except my example was wrong. :ponder:

 

Here's the working version of the code. :P

 

  rem  ****************************************************************
  rem  *  Random number for w{7}
  rem  *
  temp5 = (rand&%10000000)
  w = w ^ temp5

 

I'll fix the example in my previous post.

Thanks. I'll add that somewhere to the bB page later today.

 

That would be really nice :)

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...