# Bitwise Operations and Reference

``````Toggling a bit
``````

Bit 0-31

pp=1<<xx - Where xx is bit number

ga=ga^pp

Setting a bit

pp=1<<xx - xx is bit number to set

ga=ga|pp

Clearing a bit

pp=1<<xx - xx is bit number to clear
pp=pp^-1
ga=ga&pp

Testing a bit

pp=ga>>xx – xx is bit number to test
pp=pp&1

pp will be the state of the bit - either 1 or 0

Iterating through 32 bits and output their state

Iteration timer - xx iterations -use gb=32 for interation number (note number)

pp=gb
gb=gb-1
if gb<0 then exit rules, skip outgoing action

qq=ga>>gb
qq=qq&1
if qq==1 then rr=127
iif qq==0 then rr=0

Output 90 pp qq

On above if you want to iterate up from gb=0

pp=32-gb
gb=gb+1
if gb>31 then exit rules, skip outgoing action

…rest is same as above

XOR - If both are same, it is 0 otherwise 1
0^0= 0
0^1 = 1
1^0 = 1
1^1 = 0

AND - both bits need to be 1 for 1 output otherwise 0
0&0 = 0
1&0 = 0
0&1 = 0
1&1 = 1

OR - Either bits 1 to output 1

0|0 = 0
0|1 = 1
1|0 = 1
1|1 = 1

To make all 32 bits 1

pp=-1

Negative Numbers

Note that highest bit of a 32 bit signed integer is the “sign” bit.
When this bit is set, the value represents a negative number and the other bits are not represented by values of 1,2,4,8 etc.

To determine the negative value.

1 - Discard the sign bit (and make a mental note this is a negative number).
2 - Take the rest of the bits and invert them.
3 - Add 1 to the result

So for a value of FFFF FFFF
the sign bit is discarded and you have
7FFF FFFF
Inverting the above gives you
0000 0000

0000 0001

So the value of FFFF FFFF is really -1.

This is really only important in debugging since MT Pro will not show the hex or binary value. It will only show the decimal value. To make it easier, download a binary calculator. Make sure it is set for 32 bit precision. Enter the value in decimal and see the result in hex or binary.

– To convert 32 bit signed int to 7 bit signed int initial value ga
// Xor with -1
ga=ga^-1
//Xor with 7f
ga=ga^0x7f

To do it with 14 bit signed int
// Xor with -1
ga=ga^-1
//Xor with 3fff
ga=ga^0x3fff

To convert for 7bit signed int to 32 bit signed int
ga=ga<<24

2 Likes