# Bitwise - -

In the manual on pg 68-69. 10 expressions are discussed i understand the first 5 listed but the last 5 regarding bitwise im having difficulty understanding the example equations. Its not making sense to me. Im looking to understand them better, and to understand when i would use them. the one benefit i see to using bitwise functions is that they are a more simple way of executing rules. Let me know Thanks!

Hi,

Every number in MT Pro is stored as a 32 bit integer. Computers only understand binary which is 1 or 0

In Binary math the far right column value represents 1, the next represents 2, then 4 and so on.

So 1 is represented in binary as

0000 0000 0000 0000 0000 0000 0000 0001

2 would be

0000 0000 0000 0000 0000 0000 0000 0010

3 would be

I put them in groups of for for simplicity of reading

A bit-wise operation is merely a mathematical operation in binary.

Here are the bit wise operations

OR

1 OR 1 = 1

1 OR 0 = 1

0 OR 1 = 1

0 OR 0 = 0

AND

0 AND 0 = 0

1 AND 0 = 0

0 AND 1 = 0

1 AND 1 = 1

XOR

0 XOR 0 = 0

1 XOR 0 = 1

0 XOR 1 = 1

1 XOR 1 = 0

Now bit shifting is simply moving the bits right or left

So if you have the number 2 it will be represented in binary as

To shift it one bit left, will make it 4

100 – 4 binary

This would be represented in MT Pro as ga=ga<<1

Shift it right (the original 2) it becomes 1

ga=ga>>1

001 – 1 binary

Now a few other things to note, it is easy to convert from binary to Hex by putting them in groups of 4.

0000=0

0001=1

0010=2

0011=3

0100=4

0101=5

0110=6

0111=7

1000=8

1001=9

1010=A

1011=B

1100=C

1101=D

1101=E

1111=F

So I can easily determine Hex from a number like this

0110 1111 = 6F Hex

Now one final thing on 2 bit signed integers. The far left bit is the sign bit so if it is on it represents a negative number the largest positive number would be

0111 1111 1111 1111 1111 1111 1111 1111

which is 7FFFFFFF Hex or a value of 2147483647 in decimal (used a hex to decimal calculator for this)

FFFFFFFF would represet the value of -1

FFFFFFFE represents -2

So the largest negative number would be

80000000 Hex or -2147483648 decimal

The only reason I mention the sign is so that if you look at the values using dump global variables, this is the format you will see so don’t get confused on negative values. Just use a calculate that converts decimal to hex or decimal to binary to see the real stored value. There are many of these conversion tools online and Windows calculator in Programmer mode can also do this for you.

I typically use bitwise operations if I start running out of global variable space. I can represent on/of state of up to 32 LED’s with a single global variable at the expense of using bit manipulat to stuff and extract bits.

I hope this give you an idea of the basic of bit operators.

Steve