Log in

View Full Version : New Script: Binary Logic


jmac698
24th August 2011, 03:40
This is a utility to perform binary logic in Avisynth Script:

#Binary Logic Ver. 1.1 by jmac698
#Perform binary logic functions and/or
#1.1: handle negative arguments, simplify code (thanks to suggestions by Gavino)
a1=5
b1=1
op1="AND"
c1=binbool(a1,b1,op1)
a2=4
b2=2
op2="|"
c2=binbool(a2,b2,op2)
messageclip(string(a1)+op1+string(b1)+"="+string(c1)+", "+string(a2)+op2+string(b2)+"="+string(c2))

function binbool(int a, int b, string op){
#Find the binary AND or OR on a and b
op=lcase(op)
op=op=="or"?"||" : \
op=="and"?"&&" : \
strlen(op)==1?op+op:op
binbool2(abs(a), abs(b), op, 0)
}

function binbool2(int a, int b, string op, int currresult){
m=max(a,b)
pwr2=int(pow(2,floor(log(m)/log(2))))
bit_a=floor(a/pwr2)
bit_b=floor(b/pwr2)
t=eval(string(bool(bit_a))+op+string(bool(bit_b)))?pwr2:0
currresult=currresult+t
a=a-bit_a*pwr2
b=b-bit_b*pwr2
a+b==0?currresult:binbool2(a, b, op, currresult)
}

function bool(int n){
#Return false if n=0, true otherwise
n!=0
}

If you can think of a simpler way to program this, let me know. It does the minimum loops necessary.

Gavino
24th August 2011, 09:26
If you can think of a simpler way to program this, let me know.
It would be far simpler - trivial, even - to do this as a plugin.
Doing it in script language is an interesting exercise, but somewhat masochistic.

n==0?false:true
You could just write n!=0 on its own.

Perhaps binbool should check for and reject negative arguments.

jmac698
24th August 2011, 10:55
Updated. I don't think I could have written a C plugin faster, and still no one has written a tutorial for C++ plugin development. But even better - this should have been in avisynth. I should make a suggestion for 2.6.

StainlessS
24th August 2011, 11:24
Updated. I don't think I could have written a C plugin faster, and still no one has written a tutorial for C++ plugin development. But even better - this should have been in avisynth. I should make a suggestion for 2.6.

See Below from Avisynth SDK, file non-"clipsample.htm"
-----------------------------
Non-clip Sample
Non-clip functions in plugins
Unlike usual clip functions, non-clip functions return not frames of clip but single (scalar) value as AVSValue (variant of float, integer, boolean, string).
Numeric function example
Here's a sample from script.cpp, edited for external use (by foxyshadis, see discussion):

AVSValue Sin(AVSValue args, void* user_data, IScriptEnvironment* env) { return sin(args[0].AsFloat()); }

extern "C" __declspec(dllexport) const char* __stdcall
AvisynthPluginInit2(IScriptEnvironment* env) {
env->AddFunction("sin", "f", Sin, 0);
return "sin";
}

You don't even need a class/constructor, come to think of it. Include avisynth.h and that could be a whole plugin right there. (If not a terribly useful one.)
------------------------

This is just a small excerpt from the file.
The active part is "return sin(args[0].AsFloat());"

IanB
24th August 2011, 12:39
Probably something many have not noticed in the 2.6.0.2 release notes :-
Add BitAnd(), BitNot(), BitOr() and BitXor() script functions.

Guest
24th August 2011, 13:59
and still no one has written a tutorial for C++ plugin development The creator of Avisynth, Ben Rudiak-Gould, wrote one. It is available at the mirror of his original site that I maintain:

http://neuron2.net/www.math.berkeley.edu/benrg/avisynth-extensions.html

Also, see here, of course:

http://forum.doom9.org/showthread.php?t=48261

StainlessS
24th August 2011, 15:01
@neuron2

I'm pretty sure jmac698 means a C++ tutorial for C programmers, to explain all the fluffy stuff of C++.

EDIT: I did start doing something along those lines in response to a PM session with jmac698, A guide to Avisynth C++ interface
for C programmers, by a C++ novice. C++ guys are more likely
to forget the differences, and may perhaps be less than best (oh no there's that word, forgive me) for such a task.

EDIT: A tutorial on the C++ interface for C programmers.

Guest
24th August 2011, 15:47
I'm pretty sure jmac698 means a C++ tutorial for C programmers Huh? He said this: "tutorial for C++ plugin development ".

jmac698
24th August 2011, 16:19
He's knows what I mean, I was asking him to explain it to me before. I don't think I've seen that guide though...
http://sourceforge.net/projects/avisynth2/files/AviSynth_Alpha_Releases/AVS%202.6.0%20Alpha%203%20%5B110525%5D/ looks good, do you think it's ready to use yet? Am I going to lose compatibility with plugins?
I meant to check it out but I forgot about the new release *shame* :)

Guest
24th August 2011, 19:59
Use 2.5.8. 2.6.0 is not quite ready for prime time, IMHO.