Jump to content

Photo

Bit Operations


16 replies to this topic

#1 yuppicide OFFLINE  

yuppicide

    I am the Black Knight. Give me your money!

  • 6,933 posts
  • Location:New Jersey

Posted Wed Dec 30, 2009 1:00 PM

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?

#2 RevEng OFFLINE  

RevEng

    River Patroller

  • 4,992 posts
  • Bitnik
  • Location:bottom of the stack

Posted Wed Dec 30, 2009 1:40 PM

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


#3 yuppicide OFFLINE  

yuppicide

    I am the Black Knight. Give me your money!

  • Topic Starter
  • 6,933 posts
  • Location:New Jersey

Posted Wed Dec 30, 2009 3:46 PM

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, Wed Dec 30, 2009 3:52 PM.


#4 Random Terrain OFFLINE  

Random Terrain

    Visual batari Basic User

  • 28,789 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Wed Dec 30, 2009 3:53 PM

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



#5 SeaGtGruff OFFLINE  

SeaGtGruff

    Quadrunner

  • 5,566 posts
  • Location:South Carolina, USA

Posted Wed Dec 30, 2009 5:07 PM

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

#6 yuppicide OFFLINE  

yuppicide

    I am the Black Knight. Give me your money!

  • Topic Starter
  • 6,933 posts
  • Location:New Jersey

Posted Wed Dec 30, 2009 7:56 PM

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

#7 SeaGtGruff OFFLINE  

SeaGtGruff

    Quadrunner

  • 5,566 posts
  • Location:South Carolina, USA

Posted Wed Dec 30, 2009 11:40 PM

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

#8 yuppicide OFFLINE  

yuppicide

    I am the Black Knight. Give me your money!

  • Topic Starter
  • 6,933 posts
  • Location:New Jersey

Posted Thu Dec 31, 2009 8:08 AM

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.

#9 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • 211 posts
  • Location:Brazil

Posted Mon Jan 3, 2011 6:53 AM

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!

#10 RevEng OFFLINE  

RevEng

    River Patroller

  • 4,992 posts
  • Bitnik
  • Location:bottom of the stack

Posted Mon Jan 3, 2011 7:27 AM

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, Mon Jan 3, 2011 11:16 AM.


#11 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • 211 posts
  • Location:Brazil

Posted Mon Jan 3, 2011 7:33 AM

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

#12 RevEng OFFLINE  

RevEng

    River Patroller

  • 4,992 posts
  • Bitnik
  • Location:bottom of the stack

Posted Mon Jan 3, 2011 7:44 AM

No problem! :)

#13 Random Terrain OFFLINE  

Random Terrain

    Visual batari Basic User

  • 28,789 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Jan 3, 2011 10:39 AM

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


#14 RevEng OFFLINE  

RevEng

    River Patroller

  • 4,992 posts
  • Bitnik
  • Location:bottom of the stack

Posted Mon Jan 3, 2011 11:05 AM

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.

#15 Random Terrain OFFLINE  

Random Terrain

    Visual batari Basic User

  • 28,789 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Jan 3, 2011 11:15 AM

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.

#16 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • 211 posts
  • Location:Brazil

Posted Mon Jan 3, 2011 4:07 PM

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

#17 Random Terrain OFFLINE  

Random Terrain

    Visual batari Basic User

  • 28,789 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Tue Jan 4, 2011 5:57 AM

Here's my first attempt:

http://www.randomter...html#rand_bitop

You may need to refresh the page. If anyone has a better way to word it, please let me know.




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users