Author Topic: The & operator  (Read 3513 times)

Offline MacadamiaNuts

  • Bot Destroyer
  • ***
  • Posts: 273
    • View Profile
The & operator
« on: August 11, 2007, 12:12:01 PM »
I just passed by the wiki and seeing there wasn't any page for the & operator picked this online sci calc:

http://ostermiller.org/calc/calculator.html

and listed some results:

http://www.darwinbots.com/WikiManual/index.php?title=%26

I just found the & operator can be used to create looping functions using a constant value.

For example:

*.robage 3 & 1 add .tienum store

That would loop through ties 1, 2, 3 and 4.
« Last Edit: August 11, 2007, 12:12:37 PM by MacadamiaNuts »
Sometimes you win, and sometimes you lose...

Offline Trafalgar

  • Bot Destroyer
  • ***
  • Posts: 122
    • View Profile
The & operator
« Reply #1 on: August 11, 2007, 12:42:57 PM »
I would note that that looping would only work when the value you're &ing with (the 3 in this case) is ((2^n)-1) where n is any whole number. In other words, 1, 3, 7, 15, 31, 63, 127, etc.

What the & operator does, fundamentally, is look at its two parameters as bitfields, and perform an anding operation on each pair of bits, e.g.:

If you & 3 with 5:
3 is 0011 in binary.
5 is 0101 in binary.
3 & 5 is 0001 in binary, which is 1.

In binary, the bits (which are like digits in decimal (base 10) numbers) can only be either 0 or 1. Much like how 152 in decimal means a number is composed of 1*100 + 5*10 + 2, in binary 0111 means the number is composed of 1*4 + 1*2 + 1*1.

The reason you can do looping with 3 or 7 or any other ((2^n)-1) number is because they do not have any 0s in their bits (below their highest 1 bit). What I mean is:

3     00000011
7     00000111
15   00001111
31   00011111
63   00111111
127 01111111
255 11111111

So when you & a number with 3, everything but the lowest 2 bits in the other number are erased.

(The explanation for my "1 rnd -- &" trick in the other thread is that 0-1 becomes -1, and -1 has ALL its bits on because of how negative numbers are represented. Thus, &ing a number with -1 does nothing to it, whereas &ing a number with 0 results in 0 regardless of the other number.)

& is usually used for getting just a portion of a number, like your looping example, mainly because it is faster than mod (and doesn't go wonky when fed a negative number), but it is also used for checking a variable to see if it has a particular bit set, and can be used in combination with ~ to clear a single a bit from a number without affecting the rest of the number (and it does nothing if the bit isn't set).

For example:

"15 1 ~ &" results in 14
"14 1 ~ &" also results in 14
"14 1 |" results in 15
"15 1 |" also results in 15
(| is the opposite of ~ &.)
"15 1 &" results in 1. (because 15 is 1111 and 1 is 0001)
"14 1 &" results in 0. (because 14 is 1110 and 1 is 0001)
"15 2 &" results in 2. (because 15 is 1111 and 2 is 0010)
"14 2 &" results in 2. (because 14 is 1110 and 2 is 0010)
"13 2 &" results in 0. (because 13 is 1100 and 2 is 0010)

("A B ~ &" in DarwinBots would normally be written "A &~ B" in C/C++ or other similar languages, although it is actually doing "A & ( ~B )")
« Last Edit: August 11, 2007, 12:45:15 PM by Trafalgar »