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 / binary numbering

Author
Message
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 5th May 2009 22:19
ok im tryin to make a media-less tetris game, my plan is to have the blocks be based off a 3*2 grid, with each square of the grid assigned to a binary value

___ ___ ___
|1| |2| |4|
--- --- ---
|8| 16 32
-----------

poor drawing but it works

then to keep the blocks random i have the next block be made of a value of rnd(63) (63= 1+2+4+8+16+32)

i recall someone talking about binary operators (&) but how do they work in this case?

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 5th May 2009 22:33
you need some log functions, let me re-dig up my guitar hero clone code. it has a binary system similar to that.





The important part to look at are the fretworks() calls. That is where you will see the decoding math.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 5th May 2009 22:47
it makes no sense to me, can u either explain the math and variables betr or give me a tutorial on the & operator?

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 5th May 2009 23:10
First off, the idea of binary is that it is 2^x where x can be any number.

However, in this case, the idea would be to figure out what x is. The way you do that is with logs (or ln, which is a natural log). The equation would look like this:

2^x=16

Obviously we can figure out that x must be 4, but lets pretend we don't.

Bust out your calculator with log functions (must be a scientific or graphing one).

2^x=16 is the same as saying Log2(16)=x where the number before the () is the base and the number in the () is the argument (normally a log is base 10, so just doing log 16 won't work on your calculator). The conversion is Log(argument)/Log(Base).

So, Log of your answer divided by log(2) will result in the power of two that it is(or highest power of two within it). You can flag that number as a one to know that it is occupied and then you subtract the 2^x (that you just found) from the randomly generated number (so that you can figure out what the next one that is filled is). Do the same math until you hit 0.

The only catch: I couldn't find any log functions for DBC, so I used LN, which apparently (at least in this case) works the same. I got the LN function off of the code base (hence the citation). Just use LN the same as Log and it will work out fine (I just did it to make sure so the math works)


Make sense?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 6th May 2009 00:53
ok tnx 4 ur help but i found my own solution using & and bin$() (or bin(), i 4get which, lol)

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 6th May 2009 01:05
@BN2
You've got a great brain and you obviously love to dig into that math! That's fantastic!

@That1Smart Guy
You may be also able to achieve the results you are looking for by approaching this the same way as you started in your top post: by using &. BN2's approach is great when you don't know what you are looking for. But if you already have the values of your 6 cell grid that draws the blocks, then you can turn on and off the parts of the grid by the bit value of the number using an & test. You already know your values:

32 16 8 4 2 1

And to ensure that at least 1 grid space is filled you could use:

number=rnd(62)+1

Now it's just a matter of testing which bits in the number are ON.

is 32 ON?
if number & 32 then draw the 32 block

is 16 ON?
if number & 16 then draw the 16 block

etc.

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 6th May 2009 03:29 Edited at: 6th May 2009 03:32
o, thats WAAAAAAY less complicated than my solution

so that will return a 0/1 for yes or no as to whether in binary that value can be made from the digits of the random number

cool

[edit]

ummmm no, it doesnt return 0/1, it returns the value of the number after the &

but i guess if block is my random block value then if block & 32 > 0 then that block is on

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 6th May 2009 03:46 Edited at: 6th May 2009 03:48
When using the conditional IF, the result doesn't need to be a 1 to be true. Any non zero value and the condition is evaluated as true. It's faster to do



than



And since each 2^n value is unique, if the bit mask (32,16,8 etc) is not contained in the number, the result is 0 or false.

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 6th May 2009 04:18
ok, ya wen i started coding this i realized that stuff went screwy wen i tryed to do > 0, it would either make a 96*64 rectangle (all the boxes) or nothing, based on whether block & 1 was true

i have no idea why it did this, but if i removed the > 0 and made it block & 1, block & 2, etc then it worked like it should

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 6th May 2009 08:29
@latch

Could you use a loop like so to do the entire thing in one block?


And have it set different areas of the array to 1 for decoding after?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 6th May 2009 09:52
@BN2
Yes.

Quote: "i have no idea why it did this, but if i removed the > 0 and made it block & 1, block & 2, etc then it worked like it should"


Using & returns the value of matching bits between two numbers. If you are using a power of 2 value, then each value is a single bit.

32, for example, is bit 5. 2^5 = 32 . So when you compare some number against a bit mask using &, you will either get 0 for no match, or the value of the bit mask. I use the term bit mask to indicate that I'm only interested in testing for certain bits being ON (equal to 1).

32 & 32 = 32
32 & 33 = 32
32 & 1 = 0
33 & 1 = 1
63 & 32 = 32
63 & 1 = 1

Make any sense?

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 6th May 2009 15:48
sort of, but actually i did some reading on it and now i get that the & takes the binary representation of the two values, then loops through each bit and if BOTH are 1 then in the answer, that bit is one

so:

01011001 (156, i think)
&
10010110 (105, i think
=
00010000 (8)

then it usually takes this result and puts it back into a regular number (8) and that is displayed, but if you use str$(something & somethingelse) then you can extract the binary string intact (or i guess u could use bin(something & somethingelse) but that would return a value, not a string)

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 6th May 2009 19:21
Quote: "01011001 (156, i think)"

89
Quote: "
01011001 (156, i think)
&
10010110 (105, i think
=
00010000 (8)
"


Correction:
89 & 150=16

Remember, binary reads right to left in powers of two starting with 0. They are then added together to form the number.
This is how it reads (imagine this goes the other way, I put it this way because it COULD stretch to infinity)

1,2,4,8,16,32,64,128,256,512,1024,2048,4096,.....

Looks like where you went wrong was reading the wrong direction.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 6th May 2009 21:34
Binary numbers are read from right to left. The bit values in an 8 bit number, for example, would be:

7 6 5 4 3 2 1 0

where each position represents the power that 2 is raised to.

128 64 32 16 8 4 2 1

so

0 0 0 1 0 0 0 0 = 16

Whenever talking about a bit position, you start from 0 and count up towards the left. So in a 32 bit number, the highest bit position is 31 and would look like

10000000 00000000 00000000 00000000

The operators in DBC one uses to perform direct bit comparisons with are & (pronouced AND) and | (pronounced OR) .

& compares all the matching bits between 2 numbers and returns only those bits that are ON (equal to 1) in each number.

5 & 3 = 1

0 0 0 0 0 1 0 1
&
0 0 0 0 0 0 1 1
=
0 0 0 0 0 0 0 1

| compares all the bits between 2 numbers and returns ANY bits that are ON. They do not have to be in matching bit positions.

5 | 3 = 7

0 0 0 0 0 1 0 1
|
0 0 0 0 0 0 1 1
=
0 0 0 0 0 1 1 1

&,| can only be used with integers.

A bit mask is a series of bits you use to test bits in a number. The result of the is another number that is a result of the binary logic that was used.

! in DBC can be read as NOT the same as. In some programming languages, it is the logical NOT or not equal to.

! does a bit comparison between two values and as soon as one of the bit positions do not match, it returns true (1) . Otherwise it returns false (0). Don't confuse this with <> .

<> performs a conditional test and therefore requires an IF THEN clause whereas ! does not.

That means ! is more flexible and faster than <>. You could use ! in establishing movement and screen limits for a sprite all in one:



Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 6th May 2009 23:07 Edited at: 6th May 2009 23:07
ok, i read the binary backwards,

pretty sad considering my sig


There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 6th May 2009 23:20
Quote: "pretty sad considering my sig"


rofl

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose

Login to post a reply

Server time is: 2025-06-08 05:24:25
Your offset time is: 2025-06-08 05:24:25