Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Stripes and Checkerboard

Author
Message
Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 4th Jun 2010 07:56
I was playing around with "&" and these are a couple of things I found.

Checkerboard done in the least code I can manage


Stripes


29 games
14
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 4th Jun 2010 22:21
I had a look at the checkerboard code.

As I understand it, the line:

odd = (x+y)&1

flips the value of odd between 1 and 0.

What I don't get is why the "&" command should work like that.

I can use "&" as part of an "if then" statement but this has confused me.

I've just put this bit of code together



which gives an alternating value of 1 and 0 but why?
Latch
14
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 4th Jun 2010 23:19 Edited at: 4th Jun 2010 23:48
& is the binary operator AND not the logical boolean AND . In the case of evaluating a value & 1 , this is a test for the ON state of bit 0.

In an 8 bit value, for example:

0 0 0 0 0 0 0 1

&ing this with 1 results in the value 1. Now if the value is 2:

0 0 0 0 0 0 1 0

&ing this with 1 results in the value of 0.

& returns the value of all ON bits that are matching between the compared values.

So in your example, every other value is even, so therefore every other value does not have bit 0 set(whose value is 1 when it is set). That's why you get alternating values of 0 and 1.

so what would be the value of 123 & 17 ?

0 1 1 1 1 0 1 1
0 0 0 1 0 0 0 1
----------------
0 0 0 1 0 0 0 1 = 17

Enjoy your day.
29 games
14
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 4th Jun 2010 23:29
That makes sense.

Thanks for that.
t10dimensional
11
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 5th Jun 2010 00:55 Edited at: 5th Jun 2010 00:59
I was just messing around and noticed that if you keep the 1's that are by themselves and throw out the pairs of 1's you get the difference of the two numbers.

0 1 1 1 1 0 1 1
0 0 0 1 0 0 0 1 -
----------------
0 1 1 0 1 0 1 0 = 106

You guys prob new that but how would ya get the sum?

edit:

And why doesn't this work in DBP

odd = (x+y)&1

Latch
14
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 5th Jun 2010 02:24 Edited at: 5th Jun 2010 02:34
Quote: "I was just messing around and noticed that if you keep the 1's that are by themselves and throw out the pairs of 1's you get the difference of the two numbers."

What your description implies is what is called Exclusive Or, XOR . However, this is not the difference between the numbers. Take:

0 0 0 1 1 0 1 0
0 0 0 1 0 1 0 1

using your statement, the value would be

0 0 0 0 1 1 1 1 = 15 and the difference between 26 and 21 is 5

Quote: "And why doesn't this work in DBP

odd = (x+y)&1"

DBPro uses && and DBC uses &


Quote: "You guys prob new that but how would ya get the sum?"

123 + 17 = 140

But to do it binary, it's very similar to decimal, you just have to keep track of the carry. Any 1+1 will result in a 0 value with a carry



Enjoy your day.
t10dimensional
11
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 5th Jun 2010 03:49 Edited at: 5th Jun 2010 04:01
Well, lol I thought I figured something out. When I tested it out with 111 and 101 it worked.

Thanks for the explanation all knowing Latch. That's a lot simpler than I thought it would be.

Edit

so subtraction:

26-21=


COOL


My method only works when you don't have to borrow.

Thanks

Yodaman Jer
User Banned
Posted: 16th Jun 2010 18:23
Nice one, OBese! I especially like the checkerboard one.

I forgot how fun the DBC boards were. I definitely have to come back here more often than I have been.

Login to post a reply

Server time is: 2020-08-12 02:51:34
Your offset time is: 2020-08-12 02:51:34