Bitwise & modulus Operators

Author Topic: Bitwise & modulus Operators  (Read 1816 times)

Ive been reading up on Torque script latley and everything is going well however I am confused as to how bitwise operators are used and i was wondering if anyone could post an explanation.
« Last Edit: June 25, 2008, 01:10:07 AM by adc90 »

You will almost never have to use bitwise operators in Torque, unless you are dealing with raycasts and type masks.

If you mean the logical operators used with if, while and other conditional statements (&&, ||, !, etc), then I can explain those.

Bitwise operators are used to compare two binary numbers. Basically, they go through each digit in the two binary numbers (called a bit) starting from the left, and compare those 2 bits, producing some output. Since it's in binary, the digits are either going to be 0 or 1.

In regular torque, true is true and false is false. In binary, 1 means true and 0 means false. So just like 'true && true = true' but 'true && false = false', you can say '1 & 1 = 1' but '1 & 0 = 0'.

So, if you did 2&3, it will produce 2, because the number 2 in binary is '10' and the number 3 in binary is '11'. The first bits are 1 and 1, and the second bits are 1 and 0. So the first bits produce a 1 and the second bits produce a 0, giving a result of '10', which is binary for 2.

'&': produces a 1 when both bits are equal to 1 (otherwise produces a 0).
'^': produces a 0 when both bits are equal to 0 (otherwise produces a 1).
'|': produces a 1 only when both bits are different.
« Last Edit: March 04, 2008, 10:36:57 PM by exidyne »

Ok I think I got it now thanks for the help.
« Last Edit: March 06, 2008, 09:56:56 PM by adc90 »

Okay so I have run into a new problem this time with the modulus operator.  I've tried doing some test in the console but I'am still not clear as to how they are used.

echo(4%2) = 0 because 2 goes into 4 twice, no remainder.

echo(9%4) = 1 because 4 can only go into 9 twice, with 1 left over.
« Last Edit: June 26, 2008, 09:50:15 AM by Ephialtes »


Modulus returns the remainder from division,

echo(4%2) = 0 because 2 goes into 4 twice with no remainder.

echo(9%4) = 1 because 4 goes into 9 twice with 1 remainder.

You can achieve the same thing with some simple maths:

%a % %b = ((%a / %b) - mFloor(%a / %b)) * %b