mIRC Homepage
Posted By: Clubfoot $biton $bitoff $log - 07/08/03 07:56 AM
could anyone explain to me in any amount of detail how these identifiers work?
Posted By: Raccoon Re: $biton $bitoff $log - 07/08/03 08:57 AM
$biton(A,N)
Returns the A value with the Nth bit turned on.
$bitoff(A,N)
Returns the A value with the Nth bit turned off.
$log(N)
Returns the natural logarithm of N.

$biton() and $bitoff() take a decimal value (a base 10 normal number) and returns the result if binary (base 2) bit N is turned into a 1 or a 0.

For Example:
base10 42 == base2 101010
To do this conversion, use $base(42,10,2)
$biton(42,1) == 43 101011
$bitoff(43,1) == 42 101010
Positions are counted backwards from right to left,
so 1 is the right-most position in red.

As for $log(N), you will simply have to take Geometry, Trig or Calculus to learn that. (i forget)

- Raccoon
Posted By: Hammer Re: $biton $bitoff $log - 07/08/03 10:04 AM
$biton() and $bitoff() require a bit of knowledge of binary (but submitted in decimal).
Code:

  0000 =  0     0100 =  4     1000 =  8     1100 = 12
  0001 =  1     0101 =  5     1001 =  9     1101 = 13
  0010 =  2     0110 =  6     1010 = 10     1110 = 14
  0011 =  3     0111 =  7     1011 = 11     1111 = 15

So, let's say we want to start with 3 (0011) and see what that would be if we turned ON the 4th bit.

//echo -a * $biton(3, 4)
* 11


Notice that 3 is 0011 and we turned on the 4th bit, which becomes 1011, which is 11 from the chart above.

If we started with 15 (1111) and wanted to see what the value would be if we turned OFF the 3rd bit:

//echo -a * $bitoff(15,3)
* 11


Starting with 1111 and turning off the 3rd bit gives 1011, which is 11.

These are basically useless as they are given here, but become very useful when you start using variables that you don't know the value ahead of time during any given execution of $biton or $bitoff.


$log() is a mathematical function, just as $sin(), $cos(), $tan(), etc. are. $log(N) returns the natural logarithm of the value you give it.

Assume that X raised to the Yth power equals Z. Translated into mIRC scripting, this is %Z = $calc(%X ^ %Y). But what if we wanted to find %Y if we knew %X and %Z? That's where $log() comes in.

A natural logarithm uses as its base (%X) the constant e (which is approximately equal to 2.7182818). Exponential and logarithmic functions with base e occur in many practical applications, including those involving growth and decay, continuous compounding of interest, alternating currents and learning curves.

If you have need of this function, you'll know it and mIRC provides a way to use it; if you don't, you can safely ignore it. laugh
Posted By: SladeKraven Re: $biton $bitoff $log - 07/08/03 10:10 AM
Anything else you want to add to that Hammer? Got enough information there? LOL..
Posted By: Hammer Re: $biton $bitoff $log - 07/08/03 11:05 AM
Well, I had intended to delve into using & (bitwise comparison) for use in IF conditions to check to see if a bit is turned on, as well as $and(), $or() and $xor(), but since that information wasn't requested, I didn't go into them. However, for completeness' sake, I will do so now.

For an AND result to be true, both inputs (or bits) must be true (1). Otherwise, the result is false (0). This is useful in checking if a bit is turned on (all bits 0 except the bit you want to check - if you get a non-zero answer back, then the bit was on). It's also the way you turn off a bit (all bits 1 except the bit to turn off).

1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0

For an OR result to be true, at least one input must be true. If both are 0, then the result is false. This is useful in turning on a bit (all bits 0 except the bit you want to set - if you get the same answer back, then the bit was already on).

1 OR 1 = 1
1 OR 0 = 1
0 OR 1 = 1
0 OR 0 = 0

For an XOR (eXclusive OR) result to be true, one and only one input must be true. If neither or both are true, the result is false. This is useful in toggling bit states (all bits 0 except the bit you wish to toggle).

1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0

Just to refresh your memory of the bits and their decimal values we'll be working with throughout, here they are again:

1 = 0001
2 = 0010
4 = 0100
8 = 1000

and our base number is: 5 = 0101

[color:#00007F]if (N & M)


//if (5 & 1) echo -a * Bit is on | else echo -a * Bit is off
* Bit is on (because the 1st bit - which is worth 1 in decimal - is turned on)

//if (5 & 2) echo -a * Bit is on | else echo -a * Bit is off
* Bit is off (because the 2nd bit - which is worth 2 in decimal - is turned off)

//if (5 & 4) echo -a * Bit is on | else echo -a * Bit is off
* Bit is on (because the 3rd bit - which is worth 4 in decimal - is turned on)

//if (5 & 8) echo -a * Bit is on | else echo -a * Bit is off
* Bit is off (because the 4th bit - which is worth 8 in decimal - is turned off)

$and(N,M)

//echo -a * $and(5,14)
* 4

//echo -a * $and(5,13)
* 5

//echo -a * $and(5,11)
* 1

//echo -a * $and(5,7)
* 5

$or(N,M)

//echo -a * $or(5,1)
* 5

//echo -a * $or(5,2)
* 7

//echo -a * $or(5,4)
* 5

//echo -a * $or(5,8)
* 13

$xor(N,M)
Starting with 5 (0101) and toggling bits as we go:


//echo -a * $xor(5,1)
* 4 (0100 - toggled bit 1 off)

//echo -a * $xor(5,2)
* 7 (0111 - toggled bit 2 on)

//echo -a * $xor(5,4)
* 1 (0001 - toggled bit 3 off)

//echo -a * $xor(5,8)
* 13 (1101 - toggled bit 4 on)[/color]

:tongue:
Posted By: SladeKraven Re: $biton $bitoff $log - 07/08/03 11:11 AM
Are you quite done now? LOL grin
Nice long post(s).
Posted By: Raccoon Re: $biton $bitoff $log - 07/08/03 01:57 PM
Quote:
Assume that X raised to the Yth power equals Z. Translated into mIRC scripting, this is %Z = $calc(%X ^ %Y). But what if we wanted to find %Y if we knew %X and %Z? That's where $log() comes in.


*scratches his head*

So what if I have %x ^ %y = %z, where %x = 2 and %z = 256, how do I solve for %y (8) using $log?

- Raccoon
Posted By: Hammer Re: $biton $bitoff $log - 07/08/03 02:36 PM
As I said before, $log() is a natural logarithm; that means it doesn't use %x = 2, nor %x = 10, but rather %x = e...which is calculated as an infinite series, like this:

e = 1 + 1/(1!) + 1/(2!) + 1/(3!) + 1/(4!) + 1/(5!) + 1/(6!) + 1/(7!) + 1/(8!) + 1/(9!) + •••

or more simply,

e = 2.71828182845904523536028747135266249775724709369995

This is your %x (base). This number appears very, very often in calculus.

That is why $log(256) returns 5.545177 instead of 8, which would be true of a base-2 logarithm. If you needed a base-2 logarithm, you might be able to $com over to Excel.Application (or ADODB.Connection and use a connection string to get to an Excel worksheet!) and use Excel's =Log(256,2) to return 8.
Posted By: root Re: $biton $bitoff $log - 07/08/03 04:20 PM
Jesus christ hammer, let me get a <expletive deleted> pen cuz i ran out of paper printing those posts. Got enough detail there? I feel like i could write a <insert word here> with that already O.o

indepth
Posted By: Raccoon Re: $biton $bitoff $log - 07/08/03 04:34 PM
You post the word '<expletive deleted>' but censor the word 'book'?
Posted By: Clubfoot Re: $biton $bitoff $log - 07/08/03 04:41 PM
excellent information, thanks very much guys
Posted By: codemastr Re: $biton $bitoff $log - 07/08/03 05:12 PM
Well honestly, you have a TON of great info there, which is why I was suprised to see you say use $com to use a base 2 log... There is an incredibly simple formula that allows you to take the log of any base.

To take the log2(N) (log base 2 of N) you do log(N)/log(2)

Or more generally logB(N) = log(N)/log(B)

Thats the "change of base" formula, it's very easy to use, and you can easily make an mIRC alias for it:

alias nlog { return $calc($log($1)/$log($2)) } (Note that due to mIRC's methods of rounding, it won't give 100% precise answers. But, anyway,
For Racoons question,

Quote:

So what if I have %x ^ %y = %z, where %x = 2 and %z = 256, how do I solve for %y (8) using $log?

$calc($log(256)/$log(2)) = 8. Or, using the alias I included just $nlog(256,2) As I said, due to mIRC's rounding method it doesn't give you an exact answer, mIRC says it is 8.000001, but if you do that same expression in calc.exe, you get 8.
Posted By: Raccoon Re: $biton $bitoff $log - 07/08/03 05:39 PM
Thanks codemastr, that's actually quite handy.

As for the rounding issue, one can simply $round($calc($log($1)/$log($2)),5) or even less decimal places as desired.

However, it would be nice if Khaled added this function internally, as it does pose a problem when working with even moderately large numbers.
Posted By: Clubfoot Re: $biton $bitoff $log - 07/08/03 07:43 PM
oh interesting, $chr($bitoff($asc(x),6)) turns a letter into a capital
Posted By: KingTomato Re: $biton $bitoff $log - 07/08/03 08:03 PM
too bad its not failsave on any other character >:\
Posted By: codemastr Re: $biton $bitoff $log - 07/08/03 08:13 PM
Yeah, imho mIRC needs a way to do more accurate calculations. I wrote a little script that converts a decimal into a fraction, and it works great, so I was testing it out and I tried $dec2frac(.0001) And I got 1/9951 as a result. When I first saw it, I thought I had a bug or something, because clearly, 1/9951 is NOT .0001. But, if you go and type $calc(1/9951) you do get .0001 as a result because mIRC has such imprecise rounding. It really makes dealing with any math that requires a lot of accuracy impossible.
Posted By: Hammer Re: $biton $bitoff $log - 08/08/03 12:38 AM
codemastr: The reason I didn't include that formula is because I didn't know that formula. I am no mathematician; in fact, I have never used a logarithm in my life, never having ever found a need for it. The information I posted was from the research I did in order to write the post, not from any first-hand personal acquaintance with the subject itself.

I am certainly glad there are folks around, such as yourself, who understand such things and can clarify any mistakes I make. laugh
Posted By: codemastr Re: $biton $bitoff $log - 08/08/03 12:41 AM
Heh, never used logarithms? Certainly coulda fooled me! smile
© mIRC Discussion Forums