Hexadecimal Notation
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
Usage Example:
int A=0x400;
Description:
Integers in hexadecimal notation always start with "0x" or "0X" and are of base
16. Symbols used to display these numbers are 0 through 9 plus A through F,
case is irrelevant.
In other programming languages or mathematical texts you may see hexadecimal
values denoted by a multitude of other prefixes or suffixes. Almost every
programming language allows hexadecimal notation.
In some cases I prefer to denote hexadecimal numbers by a trailing h or H,
mostly for improved readability. However, denoting hexadecimal numbers with a
trailing h is not valid GALAXY code.
Hexadecimal notation is particularly useful for bitwise operations because every
hexadecimal digit represents 4 bits. The first bit is hexadecimal value 1, the
second is 2, the third is 4 and the fourth is 8. If you want to set multiple
bits in a single hexadecimal digit, simply add the hexadecimal values of the
bits you want to set to one.
More Examples:
int A=0x1; // first bit set to 1
int B=0x2; // second bit set to 1
int C=0x4; // third bit set to 1
int D=0x8; // fourth bit set to 1
int CD=0xC; // third and fourth bit set to 1
int ABD=0xB; // first, second and fourth bit set to 1
int Hex225 = 0xE1; // = 0x80 + 0x40 + 0x20 + 0x01
// = 128 + 64 + 32 +1
// = 11100001 // in binary
Integers in GALAXY
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
Integers in GALAXY are always signed 32 bit integers. Additionally, Blizzard
saw the necessity to protect us from ourselves and prohibits any constants that
would cause a numerical overflow.
Example:
int A = 0xFFFFFFFF; // Throws an Error at you
int B = 0x80000000; // This also throws an error at you
// this is the smallest possible value an integer can
// have: -2^31
so if you, for example want to use 0xFFFFFFFF (for whatever reason), you have
to use its signed equivalent, which is -1.
This is probably a good place to tell you to look at how negative integers work
in a computer.
Bitwise AND
ŻŻŻŻŻŻŻŻŻŻŻ
Usage Example:
int A = 4;
int B = 5;
int C = A & B;
Description:
A bitwise AND compares all 32 bits of A and B and sets the corresponding bit in
C to true only if the bits in A and B are both 1.
More Examples:
int A = x & -1; // A will always be equal to x
int B = x & 0; // B will always be 0
Calculating:
3523 & 1556
Decimal: 3523 // Hexadecimal: DC3 // Binary: 110111000011
Decimal: 1556 // Hexadecimal: 614 // Binary: 011000010100
1 & 0 = 0
1 & 1 = 1
0 & 1 = 0
1 & 0 = 0
1 & 0 = 0
1 & 0 = 0
0 & 0 = 0
0 & 1 = 0
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 0 = 0
Result: 010000000000b = 400h = 1024
Bitwise OR
ŻŻŻŻŻŻŻŻŻŻ
Usage Example:
int A = 4;
int B = 5;
int C = A | B;
Description:
A bitwise OR compares all 32 bits of A and B and sets the corresponding bit in
C to true only if the bit in either A or B is 1.
More Examples:
int A = x | -1; // A will always be -1;
int B = x | 0; // B will always be equal to x
Calculating:
3523 | 1556
Decimal: 3523 // Hexadecimal: DC3 // Binary: 110111000011
Decimal: 1556 // Hexadecimal: 614 // Binary: 011000010100
1 | 0 = 1
1 | 1 = 1
0 | 1 = 1
1 | 0 = 1
1 | 0 = 1
1 | 0 = 1
0 | 0 = 0
0 | 1 = 1
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 0 = 1
Result: 111111010111b = FD7h = 4055
Bitwise XOR
ŻŻŻŻŻŻŻŻŻŻŻ
Usage Example:
int A = 4;
int B = 5;
int C = A ^ B;
Description:
A bitwise XOR compares all 32 bits of A and B and flips the bits (those that
were 1 turn 0 and those that were 0 turn 1) in A that are 1 in B.
Another way to look at it is that each bit in C will only be one if either
the corresponding bit in A or the corresponding bit in B are 1, but not both
at the same time.
XOR is short for Exclusive OR.
More Examples:
int A = x ^ -1; // inverses every bit of x // A equals -x-1
int B = x ^ 0; // B equals x
int C = x ^ x; // C equals 0;
Calculating:
3523 ^ 1556
Decimal: 3523 // Hexadecimal: DC3 // Binary: 110111000011
Decimal: 1556 // Hexadecimal: 614 // Binary: 011000010100
1 ^ 0 = 1
1 ^ 1 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 0 = 1
1 ^ 0 = 1
0 ^ 0 = 0
0 ^ 1 = 1
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 0 = 1
Result: 101111010111b = BD7h = 3031
Bitwise Shift Left
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
Usage Example:
int A = 4;
int B = 2;
int C = A<**>B;
Description:
A bitwise shift right moves all bits of A to the right by B places. Bits that
are shifted outside the 32 places are lost and can not be recovered.
A bitwise shift right can also be viewed as dividing A by two to the B.
Or dividing A by two B times.
Please keep in mind that this is an operation on integers, so integer division
will be used, which truncates every decimal place.
This operation pads the result with the content of the highest bit before the
shift.
More Examples:
int A = x>>0; // A equals x
int B = x>>32; // if x was positive, B equals 0. Otherwise, B equals -1.
Calculating:
3523 >> 2 (Number of bits is 16)
1556 >> 2 (Number of bits is 16)
-144 >> 2 (Number of bits is 16)
Decimal: 3523 // Hexadecimal: 0DC3 // Binary: 0000110111000011
Decimal: 1556 // Hexadecimal: 0614 // Binary: 0000011000010100
Decimal: -144 // Hexadecimal: FF70 // Binary: 1111111101110000
0000110111000011b >> 2 = 0000001101110000b = 0370h = 880
0000011000010100b >> 2 = 0000000110000101b = 0185h = 389
1111111101110000b >> 2 = 1111111111011100b = FFDCh = -36**