A common request I get for Console Calculator is support for a bitwise NOT operator (the ~ symbol in many programming languages).  The reason I have not yet implemented this is that CCalc has a very large intrinsic bit width (200+ bits).  A proper NOT operator cannot ignore leading zeros, so how many bits should I limit to?  Should ~0x80 be 0x7F or 0xFF7F or 0xFFFFFF7F, etc.

Anyway, you can define your own NOT function to the bit width you prefer.

not16(x)=0xFFFF-x
not32(x)=0xFFFFFFFF-x

Below are a few additional useful functions which I like to have around.

gain2db(gain)=20*log10(gain)
db2gain(db)=10^(db/20)
norm(x,y)=sqrt(x^2+y^2)
norm(x,y,z)=sqrt(x^2+y^2+z^2)
twoscomp16(x)=(0xFFFF+1+x)&0xFFFF
twoscomp32(x)=(0xFFFFFFFF+1+x)&0xFFFFFFFF
max(a,b)=a*(a>=b)+b*(b>a)
min(a,b)=a*(a<=b)+b*(b<a)

Do you have any useful custom functions you’ve defined?  Comment.


2 Comments

Michael · March 21, 2010 at 2:51 pm

Heres a few small ones I use for handling timecodes on video/audio. They can probably be simplified further, but they do the trick.

fps(x)=((x*1000)/1001)
sec2min(x)=(floor(x/60)+((x%60)/100))
min2sec(x)=(floor(x)*60)+((x-floor(x))*100)
atm(x,y)=sec2min(min2sec(x)+min2sec(y))
mtm(x,y)=sec2min(min2sec(x)-min2sec(y))
tm2fr(x,y)=round(min2sec(x)*fps(y))
fr2tm(x,y)=sec2min(x/fps(y))

fps(x) -> converts frame rate to high precision floating point value, ie fps(24), ans = 23.976024

sec2min -> converts seconds to minutes in format sec.microsec, ie 1463.558s to 24.23558 ( 24:23.558 )
min2sec -> converts minutes to seconds in format min.sec+microsec, ie 24.23558 to 1463.558

atm(x,y) -> adds 2 min.sec timecodes
mtm(x,y) -> subtracts 2 min.sec timecodes

tm2fr(min.sec,int fps) -> converts min.sec timecode to frame number at int fps framerate, ie the frame number at 24:23.558 with video frame rate of 23.976fps,

> tm2fr(24.23558,24)
ans = 35090

So at that timecode and frame rate, the resultant frame will be 35090.

fr2tm(int frame number, int fps) -> converts back from frame to min.sec timecode.

> fr2tm(35090,24)
ans = 24.2354542

this will be with half a frame of the target timecode, quite hard to get this one perfect without checking the video yourself.

Jake · December 14, 2010 at 12:55 pm

You could also define a variable-width not function:

not(bits, x)=((2^bits – 1)-x)

Leave a Reply

Avatar placeholder

Your email address will not be published.