Here's the thing; I'll explain using an example:

I have 3 conditions that can be shut on or off, and instead of saving them in a separate variable, there is a way to save them in just one. For instance:

A variable starting with 0. If I want a certain condition to be true, I'll add a certain value to the variable, like:

Condition 1: +1

Condition 2: +2

Condition 3: +4

Condition 4: +8

So that, for instance, to have conditions 1 and 3 activated, the variable's number should be (1+4=)5

Does anyone know the complete formula to interact with this, or at least, this method's name? I've seen it in action in Sonic Robo Blast 2's level headers:

If I want the level to be available to 1 player (4096), 2 players (1) and race (2), the TypeOfLevel would have a value of (4096+1+2=)4099

# Tags... Of Sorts.

Started by
Espyo
, Sep 01 2008 04:13 PM

7 replies to this topic

### #1

Posted 01 September 2008 - 04:13 PM

### #2

Posted 01 September 2008 - 09:39 PM

Isn't that like a simple checksum?

### #3

Posted 03 September 2008 - 06:47 AM

Well, you are just encoding some flags (condition on/off) in a 4-digit binary number.

To be short :

condition 1 is 2

condition 2 is 2

condition 3 is 2

condition 4 is 2

etc.

If a condition is true, you add its value. Let's say if conditions 1 and 3 are true, then your binary number is 0101 (higher powers of 2 are always on the left)

which makes

0 Ã 2

= 0 Ã 8 + 1 Ã 4 + 0 ÃÂ 2 + 1 Ã 1

= 5

If 4 and 3 are true, you get

1100 in binary = 1 Ã 2

= 1 Ã 8 + 1 Ã 4 + 0 ÃÂ 2 + 0 ÃÂ 1

= 12

Very old trick indeed. I don't know if this method as a name.

To be short :

condition 1 is 2

^{0}= 1condition 2 is 2

^{1}= 2condition 3 is 2

^{2}= 4condition 4 is 2

^{3}= 8.etc.

If a condition is true, you add its value. Let's say if conditions 1 and 3 are true, then your binary number is 0101 (higher powers of 2 are always on the left)

which makes

0 Ã 2

^{3}+ 1 Ã 2^{2}+ 0 Ã 2^{1}+ 1 Ã 2^{0}= 0 Ã 8 + 1 Ã 4 + 0 ÃÂ 2 + 1 Ã 1

= 5

If 4 and 3 are true, you get

1100 in binary = 1 Ã 2

^{3}+ 1 Ã 2^{2}+ 0 Ã 2^{1}+ 0 Ã 2^{0}= 1 Ã 8 + 1 Ã 4 + 0 ÃÂ 2 + 0 ÃÂ 1

= 12

Very old trick indeed. I don't know if this method as a name.

### #4

Posted 03 September 2008 - 01:31 PM

Yes, that's just it! Thanks TyYan.

Yet, I wanted to know how does the reverse process work:

If (condition 1 is activated) Then

(Run "Condition 1 true" instructions)

Else If (condition 2 is activated) Then

...

Yet, I wanted to know how does the reverse process work:

If (condition 1 is activated) Then

(Run "Condition 1 true" instructions)

Else If (condition 2 is activated) Then

...

### #5

Posted 03 September 2008 - 03:37 PM

You're going to use the binary instruction AND.

Let's say you want to test if condition 3 is true. Condition 3 is 2

You have a binary value.

You test

If value ANDÂ 2

Examples in binary :

value : 1100 (=12)

condition 3 : 0100 (2

Value AND condition 3 :

1100 AND 0100 = 0100 : condition 3 is true, 0100 ≠ 0.

Value 1010

Value AND condition 3 :

1010 ANDÂ 0100 = 0000 : condition 3 is not true, 0000 = 0.

This is because the result table for AND is

0 AND 0 = 0

0 ANDÂ 1 = 0

1 ANDÂ 0 = 0

1 AND 1 = 1

But you might need to be in binary mode. I don't know what calculator/language you're using.

You divide by powers of 2.

Here's the details :

Let's say you want to test if condition 3 is true. Condition 3 is 2

You have a value.

You divide your value by 2

Which gives

If 2Int (ValueÃ·2

Here's what happens.

When you divide by 2

Since you're not working in binary, you need to work with integers...

Examples :

Condition 3 is 2

value :

Value divided by 2

000

Then multiplied by 2 :

00

Compare to Value divided by 2

0011

They are different so your condition is true.

Other example :

Condition 3 is 2

value :

Value divided by 2

000

Then multiplied by 2 :

00

Compare to Value divided by 2

0010

They are equal, your condition is not true.

Let's say you want to test if condition 3 is true. Condition 3 is 2

^{2}= 0100 in binary.You have a binary value.

You test

If value ANDÂ 2

^{2}≠ 0 Then ... (your condition is true)Examples in binary :

value : 1100 (=12)

condition 3 : 0100 (2

^{2})Value AND condition 3 :

1100 AND 0100 = 0100 : condition 3 is true, 0100 ≠ 0.

Value 1010

Value AND condition 3 :

1010 ANDÂ 0100 = 0000 : condition 3 is not true, 0000 = 0.

This is because the result table for AND is

0 AND 0 = 0

0 ANDÂ 1 = 0

1 ANDÂ 0 = 0

1 AND 1 = 1

But you might need to be in binary mode. I don't know what calculator/language you're using.

**If you can't work with binaries, you can use this heavier alternate method :**You divide by powers of 2.

Here's the details :

Let's say you want to test if condition 3 is true. Condition 3 is 2

^{2}= 4.You have a value.

You divide your value by 2

^{2+1}, take the integer of the result, multiply it by 2, and check if it is different from the integer of the original value divided by 2^{2}Which gives

If 2Int (ValueÃ·2

^{2+1})≠Int(ValueÃ·2^{2}) Then your condition is true.Here's what happens.

When you divide by 2

^{n}in binary, you move all the digits n steps to the right (exactly the same as you divide by 10^{n}in decimals)Since you're not working in binary, you need to work with integers...

Examples :

Condition 3 is 2

^{2}= 0100 in binary.value :

**1**100 (=12)Value divided by 2

^{2+1}:000

**1**Then multiplied by 2 :

00

**1**0Compare to Value divided by 2

^{2}:0011

They are different so your condition is true.

Other example :

Condition 3 is 2

^{2}= 0100 in binary.value :

**1**010 (=10)Value divided by 2

^{2+1}:000

**1**Then multiplied by 2 :

00

**1**0Compare to Value divided by 2

^{2}:0010

They are equal, your condition is not true.

### #6

Posted 03 September 2008 - 06:26 PM

Thank you very much! I really think I got it. If I have any further questions, I'll try to be brief, but with the explanation you gave, I don't think I'll have to.

(I actually learned binary, even though my mind is a bit foggy...)

I use Casio FX 1.0 Plus, which has to be in binary mode to use binary, so I'll use the second way.

Thanks again!

(I actually learned binary, even though my mind is a bit foggy...)

I use Casio FX 1.0 Plus, which has to be in binary mode to use binary, so I'll use the second way.

Thanks again!

### #7

Posted 12 June 2009 - 05:47 PM

I'm sorry for making this gigantic bump, but I figured out what is the name of this operation (after all this time...)

It's called a bitwise operation. It's quite simple really. I've used it in Visual Basic and it works great. Now, I'm going to check if it works on the Casio calculators as well.

*testing*

No, it doesn't seem so.

Well, just found out the name of the operation and posted case anyone wanted to know.

(Darn, if it would work, we could save a lot more variables. But that's ok.)

It's called a bitwise operation. It's quite simple really. I've used it in Visual Basic and it works great. Now, I'm going to check if it works on the Casio calculators as well.

*testing*

No, it doesn't seem so.

Well, just found out the name of the operation and posted case anyone wanted to know.

(Darn, if it would work, we could save a lot more variables. But that's ok.)

### #8

Posted 15 June 2009 - 09:00 AM

I tried doing bit-shifting myself in my calc. It didn't work so well, for some reason.

TyYann's theory should be correct at some point, but bear in mind that Casio calculators don't do any complex bitwise operations (they use 1-bit only, to represent true or false).You'll have to take the value and bitshift them by diving them by 2^n.

A neat trick would be bit subtraction. Let's say we have this value:

Bin - Dec

1101 - 13

In order to take the 3rd digit, first we'll bitshift 2 digits, using integer division by 2^2:

0011 - 3

Now the 3rd, 4th and so on digits are kept. Since we don't want 4th and further digits, we'll save this current value and do another single bitshift. We get 2 values:

0011 - 3

0001 - 1

That "0001" must be now shifted to the LEFT, which will produce 0010. This is the same as multiplying by 2.

0011 - 3

0010 - 2

Now we subtract them:

0011 - 0010 = 1

Or in its decimal form:

3 - 2 = 1

Presto. It's simple logic. I hope this helps. You can use integer values in conditional expressions, by the way.

TyYann's theory should be correct at some point, but bear in mind that Casio calculators don't do any complex bitwise operations (they use 1-bit only, to represent true or false).You'll have to take the value and bitshift them by diving them by 2^n.

A neat trick would be bit subtraction. Let's say we have this value:

Bin - Dec

1101 - 13

In order to take the 3rd digit, first we'll bitshift 2 digits, using integer division by 2^2:

0011 - 3

Now the 3rd, 4th and so on digits are kept. Since we don't want 4th and further digits, we'll save this current value and do another single bitshift. We get 2 values:

0011 - 3

0001 - 1

That "0001" must be now shifted to the LEFT, which will produce 0010. This is the same as multiplying by 2.

0011 - 3

0010 - 2

Now we subtract them:

0011 - 0010 = 1

Or in its decimal form:

3 - 2 = 1

Presto. It's simple logic. I hope this helps. You can use integer values in conditional expressions, by the way.

**Edited by E_net4, 15 June 2009 - 09:04 AM.**

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users