Contents Back Forward |
Appendix A: Using binary operators |
A.1 Why binary operators? Tot uses a lot of informations coded using binary mask, for example, civilization alive in a particular game are represented by a binary number, so the number 11010001 (209 decimal) means that in that game only 4 civilizatons are active and particularly the first (barbarian), the fifth , the seventh and the eight. This kind of code came very often in Civ2 data (read Allard Höfelt's HexEditing document to see by yourself) and if you want to fully use CSPL you should take confidence with this kind of code. Without entering too much in details Iīll describe operations commonly used to manage these binary numbers, if you only want to see the final formulae just look below at the conclusion section of this appendix. | A.2 AND operator The AND operator works exactly as it works in logic (if you consider 0=FALSE and 1=TRUE) So 0 AND 0 = 0 (FALSE AND FALSE = FALSE) 0 AND 1 = 0 (FALSE AND TRUE = FALSE) 1 AND 0 = 0 (TRUE AND FALSE = FALSE) 1 AND 1 = 1 (TRUE AND TRUE = TRUE) The important thing to understand about this operator is that, if the first operand is TRUE then the result is exactly the second operand while if the first operand is FALSE then the result will be always FALSE (and viceversa) If we have a set of bits (for example a byte) we can use the AND operator bit-to-bit Example: byte1 = 01010101 AND byte2 = 00001111 ----------------------- result = 00000101 In C++ the AND operator is expressed by '&' character. |
A.3 OR operator Also the OR operator works as in logic: 0 OR 0 = 0 (FALSE OR FALSE = FALSE) 0 OR 1 = 1 (FALSE OR TRUE = TRUE) 1 OR 0 = 1 (TRUE OR FALSE = TRUE) 1 OR 1 = 1 (TRUE OR TRUE = TRUE) The important thing to understand about this operator is that, if the first operator is FALSE then the result is exactly the second operator while if the first operator is TRUE the result will be always TRUE (and viceversa) If we have a set of bits (for example a byte) we can use the OR operator bit-to-bit Example: byte1 = 01010101 OR byte2 = 00001111 ----------------------- result = 01011111 In C++ the OR operator is expressed by '|' character. |
A.4 XOR operator The XOR operator is not well known in logic, where it is called "eXclusive OR": it works exactly as OR but when the first operand and the second one are TRUE the answer is FALSE instead that TRUE as in OR operand (it translate the phrase "the first OR the second but not both") 0 XOR 0 = 0 (FALSE XOR FALSE = FALSE) 0 XOR 1 = 1 (FALSE XOR TRUE = TRUE) 1 XOR 0 = 1 (TRUE XOR FALSE = TRUE) 1 XOR 1 = 0 (TRUE XOR TRUE = FALSE) If we have a set of bits (for example a byte) we can use the XOR operator bit-to-bit Example: byte1 = 01010101 XOR byte2 = 00001111 ----------------------- result = 01011010      columns different respect to OR operator In C++ the XOR operator is expressed by '^' character. |
A.5 Yeah, but why I need this in Civ? Ok, it's time to come back to our preferred game: When you need to use these things? Letīs say you want to trigger a particular event ONLY if fourth Civ is still alive: How can you translate this request in C++ code? You know that CSPL gives you a function called GetCivInPlay; this function returns a byte which represents civs still active in the following way: the n-th bit in the byte is 0 if n-th civ is dead or 1 if n-th civ is alive; so 11010001 means that only civs nr 1,5,7 and 8 are alive (remember civ 1 is barbarian). So, youīre just interested in fourth bit of the byte but how can you extract this information in a simple way? the easiest way is to use the AND operator, putting the result of function GetCivInPlay in AND with the bit mask 00010000; try to believe:
                        5th civ is alive    5th civ is dead
If the fifth civ is dead the result will be 0 while if the fourth civ is alive the result will be a number != 0 As you can imagine if we were interested in the first civ (barbarians) the bit mask to use was 00000001 (1 decimal) if we were interested in second civ the bit mask to use was 00000010 (2 decimal) and so on. In C++ code youīll have:
{
For an example of the use of OR operator keep using GetCivInPlay function: Letīs say we donīt want to test if a particular civ is active but letīs say we want to resurrect a civ (if it is dead) The problem here is quite different: we want to SET a particular bit
                        5th civ is dead          5th civ is dead
As you can see the result of OR operator is exactly the GetCivInPlay result with the fifth bit set, this ensure you that CSPL will not touch other Civilizations, it just add the fifth one to the game. For an example of the use of XOR operator keep using GetCivInPlay function: Letīs say we want to destroy an existing civilization without touching other civs The problem is easily solved with the XOR operator: 5th civ is alive 5th civ is alive
                        5th civ is dead            5th civ is dead
As you can see the result of XOR operator is exactly the GetCivInPlay result with the fifth bit unset, this ensure you that CSPL will not touch other Civilizations, it just eliminate the fifth one from the game. |
A.6 Conclusions Ok, here I'll write briefly the use of binary operator in CSPL structures:
|