-
Flash4 Actionscript. how can I do " ^ "
Does anyone have an actionscript that simulates the ^ - function from Flash 5 in Flash 4.
^ is bitwise XOR
4 ^ 5 = 1 ( binary: 100 XOR 101 = 001)
thanx in advance,
Rainer
-
Thank goodness for Flash 5 - it's so much easier to actually have the function. You can duplicate it in Flash 4 but it's a little messy (or at least my code is) - others may have a much more clean, efficient way but here's what I have on file:
Place the following in a Frame:
Set Variable: "op1" = 11
Set Variable: "op2" = 112
Set Variable: "counter1" = 0
Set Variable: "counter2" = 0
Set Variable: "TextField3" = op1
Set Variable: "TextField4" = op2
Comment: establish 2 ^ array
Set Variable: "twoCarot0" = 1
Set Variable: "X" = 1
Loop While (X < 10)
Set Variable: "twoCarot" & X = (Eval("twoCarot" & (X-1))) * 2
Set Variable: "X" = X + 1
End Loop
Set Variable: "I" = 1
Set Variable: "op1temp" = op1
Comment: convert operand 1 from dec to bin
Loop While (I < 256)
Set Variable: "op1Div" = Int(op1temp/2)
Set Variable: "op1Rem" = op1temp - (op1Div * 2)
Set Variable: "counter1" = counter1 + 1
Set Variable: "op1Place" & counter1 = op1Rem
If (op1Div = 0)
Set Variable: "I" = 257
End If
Set Variable: "I" = I + 1
Set Variable: "op1temp" = op1Div
End Loop
Set Variable: "J" = counter1
Comment: convert operand 2 from dec to bin
Set Variable: "I" = 1
Set Variable: "op2temp" = op2
Loop While (I < 256)
Set Variable: "op2Div" = Int(op2temp/2)
Set Variable: "op2Rem" = op2temp - (op2Div * 2)
Set Variable: "counter2" = counter2 + 1
Set Variable: "op2Place" & counter2 = op2Rem
If (op2Div = 0)
Set Variable: "I" = 257
End If
Set Variable: "I" = I + 1
Set Variable: "op2temp" = op2Div
End Loop
Set Variable: "K" = counter2
Loop While (J > 0)
Set Variable: "op1Bin" = op1Bin & Eval("op1Place" & J)
Set Variable: "J" = J-1
End Loop
Loop While (K > 0)
Set Variable: "op2Bin" = op2Bin & Eval("op2Place" & K)
Set Variable: "K" = K-1
End Loop
Comment: determine length of each binary operand
Set Variable: "op1BinLen" = Length (op1Bin)
Set Variable: "op2BinLen" = Length (op2Bin)
Comment: if length of operand 1 > length of operand 2 proceed with following "If" statement
If (op1BinLen > op2BinLen)
Set Variable: "binLen" = op1BinLen
Set Variable: "lenDiff" = op1BinLen - op2BinLen
Set Variable: "counter3" = op1BinLen
Set Variable: "counter4" = op2BinLen
Loop While (counter3 >op2BinLen)
Set Variable: "op1op2" & counter3 = 1
Set Variable: "counter3" = counter3 - 1
End Loop
Loop While (counter4 > 0)
Set Variable: "placeTest1" = Eval("op1Place" & counter4)
Set Variable: "placeTest2" = Eval("op2Place" & counter4)
If (placeTest1 = placeTest2)
Set Variable: "op1op2" & counter4 = 0
Else
Set Variable: "op1op2" & counter4 = 1
End If
Set Variable: "counter4" = counter4 - 1
End Loop
Set Variable: "counter5" = op1BinLen
Loop While (counter5 > 0)
Set Variable: "TextField6" = TextField6 & Eval("op1op2" & counter5)
Set Variable: "counter5" = counter5 - 1
End Loop
End If
Comment: if length of operand 2 > length of operand 1 proceed with following "If" statement
If (op2BinLen > op1BinLen)
Set Variable: "binLen" = op2BinLen
Set Variable: "lenDiff" = op2BinLen - op1BinLen
Set Variable: "counter3" = op2BinLen
Set Variable: "counter4" = op1BinLen
Loop While (counter3 >op1BinLen)
Set Variable: "op1op2" & counter3 = 1
Set Variable: "counter3" = counter3 - 1
End Loop
Loop While (counter4 > 0)
Set Variable: "placeTest1" = Eval("op1Place" & counter4)
Set Variable: "placeTest2" = Eval("op2Place" & counter4)
If (placeTest1 = placeTest2)
Set Variable: "op1op2" & counter4 = 0
Else
Set Variable: "op1op2" & counter4 = 1
End If
Set Variable: "counter4" = counter4 - 1
End Loop
Set Variable: "counter5" = op2BinLen
Loop While (counter5 > 0)
Set Variable: "TextField6" = TextField6 & Eval("op1op2" & counter5)
Set Variable: "counter5" = counter5 - 1
End Loop
End If
Comment: if length of operand 1 = length of operand 2 proceed with following "If" statement
If (op2BinLen = op1BinLen)
Set Variable: "binLen" = op1BinLen
Set Variable: "lenDiff" = op2BinLen - op1BinLen
Set Variable: "counter4" = op1BinLen
Loop While (counter4 > 0)
Set Variable: "placeTest1" = Eval("op1Place" & counter4)
Set Variable: "placeTest2" = Eval("op2Place" & counter4)
If (placeTest1 = placeTest2)
Set Variable: "op1op2" & counter4 = 0
Else
Set Variable: "op1op2" & counter4 = 1
End If
Set Variable: "counter4" = counter4 - 1
End Loop
Set Variable: "counter5" = op1BinLen
Loop While (counter5 > 0)
Set Variable: "TextField6" = TextField6 & Eval("op1op2" & counter5)
Set Variable: "counter5" = counter5 - 1
End Loop
End If
Set Variable: "TextField1" = op1Bin
Set Variable: "TextField2" = op2Bin
Comment: convert bin to dec
Set Variable: "Y" = binLen
Set Variable: "decNumber" = 0
Loop While (Y > 0)
Set Variable: "decNumber" = decNumber +( Eval("op1op2" & Y) * (Eval("twoCarot" & (Y-1))))
Set Variable: "Y" = Y-1
End Loop
Set Variable: "TextField5" = decNumber
I have 6 TextFields to display the results (TextField1, TextField2...) I used the division method for determining the binary equivalent of decimal and then reversed it to display the resulting decimal for the XOR. Hope the variables are clear (op1, op2 = operand 1 & operand 2, op1Div = operand 1 Division, op1Rem = operand 1 Remainder etc.)
Cheers,
Markus B
[Edited by Markus B on 07-11-2001 at 03:20 AM]
-
Senior Member
Now, this is some coding!
-
Hi there,
thanx for the code. it is wonderful how people on the net share their knowledge.
I found another script on http://www.were-here.com and built my own around it.
For all those who need to produce Flash4 movies with the Flash5 tool - i for one - I gave the code in Flash5 Actionscript sysntax. the script is by sx and I added my own comments. You can just copy this in your actionscript window. replace the values of num1 and num2 as you like.
-----------------------------------------------------------
// the first number to be compared
num1 = 1234;
// the second number to be compared
num2 = 23;
//
// Convert num1 and num2 to binary.
// binary values will be held in br1 and br2.
//
// the counter. has either 1 or 2.
cnt = 1;
while (Number(cnt)<3) {
set ("br" add cnt, "");
while (eval("num" add cnt)>0) {
if (eval("num" add cnt)/2 == int(eval("num" add cnt)/2)) {
set ("br" add cnt, "0" add eval("br" add cnt));
} else {
set ("br" add cnt, "1" add eval("br" add cnt));
}
set ("num" add cnt, int(eval("num" add cnt)/2));
}
set ("cnt", Number(cnt)+1);
}
//
// fill up the shorter brx with zeros on the left
// of the string. so that both have the same length
//
if (length(br1)<length(br2)) {
while (length(br2)>length(br1)) {
set ("br1", "0" add br1);
}
} else if (length(br2)<length(br1)) {
while (length(br1)>length(br2)) {
set ("br2", "0" add br2);
}
}
//
// Binary compare br1 and br2. store the result in xor.
// at any given point in the string xor will be 1 if and
// only if exactly one of br1 and br2 are 1 at this point
// in the string.
//
// the resulting binary number
xor = "";
// another counter. used for running through br1 and br2
tmp = length(br1);
while (Number(tmp)>0) {
if (Number(substring(br1, tmp, 1)) == Number(substring(br2, tmp, 1))) {
set ("xor", "0" add xor);
} else if (Number(substring(br1, tmp, 1)) == 1 or Number(substring(br2, tmp, 1)) == 1) {
set ("xor", "1" add xor);
} else {
set ("xor", "0" add xor);
}
tmp = tmp-1;
}
//
// convert xor back to a decimal number
// the result will be held in finxor
//
finxor = 0;
tmp = length(xor);
multiplier = 1;
while (Number(tmp)>0) {
set ("finXor", Number(finXor)+Number((substring(xor, tmp, 1)*multiplier)));
multiplier = multiplier*2;
tmp = tmp-1;
}
// sx who wrote the original script says:
// "you see... it wasn't that hard... "
-----------------------------------------------------------
Greetings,
Rainer
-
Senior Member
... and actual page to that "simplier" script is on:
http://64.224.111.148/forums/showthr...threadid=76505
hope it helps...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|