Difference between revisions of "Bitwise Command"

From WikiManual
Jump to: navigation, search
 
m (Fixed typos)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
Bitwise commands take the latest value of the stack and work with it bit for bit.
 +
 +
===[[Operators]]:===
 
{{Bitwise Command}}
 
{{Bitwise Command}}
  
===Bitwise Commands===
+
Bit operators interact with the 32 bits directly, avoiding the issue of what the bits actually represent.
Bitwise commands take the latest value of the stack and work with it bit for bit.
+
Integer Numbers in the stack are constructed as a string of 32 bits using [http://en.wikipedia.org/wiki/Two%27s_compliment Two's complement].
 +
 
 +
===usages:===
  
You could use them to use a variable as multiple booleans instead of one integer.
+
You could use them to use a variable as multiple booleans instead of one integer. A Queen bot could store binary information of 32 other bots.
  
 
You can use them to read/write the sign of one integer.
 
You can use them to read/write the sign of one integer.
  
You can use them ro randomize variables.
+
You can use them to randomize variables more randomly.
 +
 
 +
You can use them for a very smooth rotating memory to search for food: changing the first bit equals 1/2 turn, the second equals 1/4 turn ... the 32th equals a 1/65536 turn, you just toggle the bits and go trough the bit positions and check if targeting gets better (toggle to next right bit) or worse (to next left bit).
  
 
===Energy costs:===
 
===Energy costs:===
Since [[version]]: 2.4 {{is that true?}} you can set an [[Energy]] cotst per bitwise command instance. default is 0.
+
Since [[FTP|version]]: 2.4 {{is that true?}} you can set an [[Energy]] cost per bitwise command instance. The default is 0.
 +
 
 +
===Two's complement:===
 +
The first bit stores the sign of the integer.
 +
:0 = "+"
 +
:1 = "-"
 +
 
 +
Positive numbers start with "0" counting higher by turning 0's into 1's from right to left, negative numbers start with "-1" counting lower by turning 1's into 0's from right to left.
 +
 
 +
Negative numbers are always "-1" lower because there is no "-0" and the "compliment" of "0" ,[[~]]0, is "-32768".
 +
 
 +
The following table shows what each bit adds to the integer if the bit is 1. If it is 0 it adds "0" .So '''that is basically what each bit represents:'''
 +
 
 +
1111000011110000
 +
                /    1
 +
              /    2
 +
              /    4
 +
            /    8
 +
            /    16
 +
          /    32
 +
          /    64
 +
        /  128
 +
        /  256
 +
      /  512
 +
      / 1.024
 +
    / 2.048
 +
    / 4.096
 +
  / 8.192
 +
  / 16.384
 +
/-32.768
 +
<<<Examples:>>>
 +
0111111111111111 equals "+32767" 
 +
0000000000000011 equals "+3"
 +
0000000000000000 equals "+0" (32*0)
 +
1111111111111111 equals "-1" (all the above numbers sum up)
 +
1111111111111100 equals "-4"
 +
1000000000000000 equals "-32768"
 +
 
 +
You can negate all numbers by inverting all bits and adding "+1", but -32768 and 0 will stay the same because of an overflow that gets capped but can be ignored anyways because '''Two's complement arranges all numbers from -32768 to 32767 on a circle''' and not on an endless straight line.

Latest revision as of 21:37, 14 February 2014

Bitwise commands take the latest value of the stack and work with it bit for bit.

Operators:

Bitwise Command:

~ Complement
& AND
| OR
^ XOR
++ increment stack value
-- deincrement stack value
- negate stack value
<< bit shift left
>> bit shift right

Bit operators interact with the 32 bits directly, avoiding the issue of what the bits actually represent. Integer Numbers in the stack are constructed as a string of 32 bits using Two's complement.

usages:

You could use them to use a variable as multiple booleans instead of one integer. A Queen bot could store binary information of 32 other bots.

You can use them to read/write the sign of one integer.

You can use them to randomize variables more randomly.

You can use them for a very smooth rotating memory to search for food: changing the first bit equals 1/2 turn, the second equals 1/4 turn ... the 32th equals a 1/65536 turn, you just toggle the bits and go trough the bit positions and check if targeting gets better (toggle to next right bit) or worse (to next left bit).

Energy costs:

Since version: 2.4 you can set an Energy cost per bitwise command instance. The default is 0.

Two's complement:

The first bit stores the sign of the integer.

0 = "+"
1 = "-"

Positive numbers start with "0" counting higher by turning 0's into 1's from right to left, negative numbers start with "-1" counting lower by turning 1's into 0's from right to left.

Negative numbers are always "-1" lower because there is no "-0" and the "compliment" of "0" ,~0, is "-32768".

The following table shows what each bit adds to the integer if the bit is 1. If it is 0 it adds "0" .So that is basically what each bit represents:

1111000011110000
               /     1
              /     2
             /     4
            /     8
           /    16
          /    32
         /    64
        /   128
       /   256
      /   512
     / 1.024
    / 2.048
   / 4.096
  / 8.192
 / 16.384
/-32.768
<<<Examples:>>>
0111111111111111 equals "+32767"  
0000000000000011 equals "+3"
0000000000000000 equals "+0" (32*0) 
1111111111111111 equals "-1" (all the above numbers sum up)
1111111111111100 equals "-4"
1000000000000000 equals "-32768"

You can negate all numbers by inverting all bits and adding "+1", but -32768 and 0 will stay the same because of an overflow that gets capped but can be ignored anyways because Two's complement arranges all numbers from -32768 to 32767 on a circle and not on an endless straight line.