Jump to content



Photo
- - - - -

Tags... Of Sorts.


  • Please log in to reply
7 replies to this topic

#1 Espyo

Espyo

    Newbie

  • Members
  • Pip
  • 19 posts
  • Gender:Male
  • Location:Portugal
  • Interests:Games... And such.<br />About Casio... Programming! Woo yeah!

  • Calculators:
    Casio FX 1.0 Plus

Posted 01 September 2008 - 04:13 PM

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

#2 kucalc

kucalc

    Casio Maniac

  • Admin
  • PipPipPipPipPipPipPipPip
  • 1422 posts
  • Location:USA
  • Interests:Programming: C/C++, Fortran, LISP, COBOL 85 Standard, PHP, x86 and SH3 Assembly<br /><br />Computer graphics

  • Calculators:
    fx-9860G / fx-7400G Plus / Algebra FX 2.0+ / fx-9770G / CFX-9850G / CFX-9850GB+ / TI-89 / TI-nSpire

Posted 01 September 2008 - 09:39 PM

Isn't that like a simple checksum?

#3 TyYann

TyYann

    Casio Freak

  • Members
  • PipPipPipPip
  • 107 posts
  • Gender:Male
  • Location:Seoul, South Korea

  • Calculators:
    Casio fx-4000P (since 1988)
    Casio CFX-9850GB PLUS
    Sharp EL-9900
    Casio fx-9860G SD
    Casio fx-9860GII SD
    and a 25-column abacus...

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 20 = 1
condition 2 is 21 = 2
condition 3 is 22 = 4
condition 4 is 23 = 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 × 23 + 1 × 22 + 0 × 21 + 1 × 20
= 0 × 8 + 1 × 4 + 0 × 2 + 1 × 1
= 5

If 4 and 3 are true, you get
1100 in binary = 1 × 23 + 1 × 22 + 0 × 21 + 0 × 20
= 1 × 8 + 1 × 4 + 0 × 2 + 0 × 1
= 12

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

#4 Espyo

Espyo

    Newbie

  • Members
  • Pip
  • 19 posts
  • Gender:Male
  • Location:Portugal
  • Interests:Games... And such.<br />About Casio... Programming! Woo yeah!

  • Calculators:
    Casio FX 1.0 Plus

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
...

#5 TyYann

TyYann

    Casio Freak

  • Members
  • PipPipPipPip
  • 107 posts
  • Gender:Male
  • Location:Seoul, South Korea

  • Calculators:
    Casio fx-4000P (since 1988)
    Casio CFX-9850GB PLUS
    Sharp EL-9900
    Casio fx-9860G SD
    Casio fx-9860GII SD
    and a 25-column abacus...

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 22 = 0100 in binary.
You have a binary value.
You test
If value AND 22 ≠ 0 Then ... (your condition is true)

Examples in binary :
value : 1100 (=12)

condition 3 : 0100 (22)

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 22 = 4.
You have a value.
You divide your value by 22+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 22

Which gives
If 2Int (Value÷22+1)≠Int(Value÷22) Then your condition is true.

Here's what happens.
When you divide by 2n in binary, you move all the digits n steps to the right (exactly the same as you divide by 10n in decimals)
Since you're not working in binary, you need to work with integers...

Examples :
Condition 3 is 22 = 0100 in binary.
value : 1100 (=12)

Value divided by 22+1 :
0001
Then multiplied by 2 :
0010

Compare to Value divided by 22 :
0011
They are different so your condition is true.

Other example :
Condition 3 is 22 = 0100 in binary.
value : 1010 (=10)

Value divided by 22+1 :
0001
Then multiplied by 2 :
0010
Compare to Value divided by 22 :
0010

They are equal, your condition is not true.

#6 Espyo

Espyo

    Newbie

  • Members
  • Pip
  • 19 posts
  • Gender:Male
  • Location:Portugal
  • Interests:Games... And such.<br />About Casio... Programming! Woo yeah!

  • Calculators:
    Casio FX 1.0 Plus

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!

#7 Espyo

Espyo

    Newbie

  • Members
  • Pip
  • 19 posts
  • Gender:Male
  • Location:Portugal
  • Interests:Games... And such.<br />About Casio... Programming! Woo yeah!

  • Calculators:
    Casio FX 1.0 Plus

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

#8 E_net4

E_net4

    Casio Freak

  • Members
  • PipPipPipPip
  • 189 posts
  • Gender:Male
  • Location:Output(&quot;Error: Coord type not specified&quot;);
  • Interests:Programming 'n' stuff...

  • Calculators:
    CASIO fx 9860G SD

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.

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users