User Tools

Site Tools


hpl2:amnesia:script_language_reference_and_guide:constants_and_enumerations

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
hpl2:amnesia:script_language_reference_and_guide:constants_and_enumerations [2013/01/13 23:25]
thegreatcthulhu [Enumerations]
hpl2:amnesia:script_language_reference_and_guide:constants_and_enumerations [2013/01/13 23:36]
thegreatcthulhu
Line 388: Line 388:
   * 0 - the lock is left unlocked   * 0 - the lock is left unlocked
  
-All of that information is encoded in //a single byte//. This is one of the reasons to use flags - using them saves memory (a single byte vs several state variables). The other reason is that flags can be (and usually are) passed as parameters to functions, whereby several indicators of simple binary ​state are passed to the function simultaneously,​ through one variable. Flags often encode "​settings"​-like data, but should ​not be overused, as they can affect code readability.+All of that information is encoded in //a single byte//. This is one of the reasons to use flags - using them saves memory (a single byte vs several state variables). The other reason is that flags can be (and usually are) passed as parameters to functions, whereby several indicators of simple binary ​states ​are passed to the function simultaneously,​ through one variable. Flags often encode "​settings"​-like data, but should be used with care, as they can affect code readability ​and clarity.
  
-How to manipulate individual bits then? Well, by assigning integer numbers to variables, and by using binary logical operators, described below. However, manipulating bits while working with numbers in the decimal system is not very convenient. This is why programmers often use //​hexadecimal//​ (HEX) numbers for such tasks. The reason is: there'​s a direct correspondence between HEX and binary numbers, as the table below shows, which makes it easy to deal with binary representations.+//How to manipulate individual bits then?// Well, by assigning integer numbers to variables, and by using binary logical operators, described below. However, manipulating bits while working with numbers in the decimal system is not very convenient. This is why programmers often use //​hexadecimal//​ (HEX) numbers for such tasks. The reason is: there'​s a direct correspondence between HEX and binary numbers, as the table below shows, which makes it easy to deal with binary representations.
  
 {{ http://​farm9.staticflickr.com/​8365/​8371351734_f2f2795a54.jpg }} {{ http://​farm9.staticflickr.com/​8365/​8371351734_f2f2795a54.jpg }}
Line 396: Line 396:
 As you can see, unlike the decimal number system, which represents all numbers using 10 different symbols (0-9), the HEX number system represents those same numbers using 16 different symbols (0-F). It just so happens that each of the HEX digits perfectly corresponds to one of all possible 4-bit combinations. Any one byte can thus be represented by 2 HEX digits - all you need to do is to refer to the table above, pick two hexadecimal digits, and write them together. To get the corresponding binary number, just replace the HEX digit with its binary equivalent from the table. As you can see, unlike the decimal number system, which represents all numbers using 10 different symbols (0-9), the HEX number system represents those same numbers using 16 different symbols (0-F). It just so happens that each of the HEX digits perfectly corresponds to one of all possible 4-bit combinations. Any one byte can thus be represented by 2 HEX digits - all you need to do is to refer to the table above, pick two hexadecimal digits, and write them together. To get the corresponding binary number, just replace the HEX digit with its binary equivalent from the table.
  
-Representing bytes using HEX system - some examples: 
 <​code>​ <​code>​
 +Representing bytes using HEX system - some examples:
 +
 +----------------------
 HEX          binary HEX          binary
 ---------------------- ----------------------
Line 406: Line 408:
  ​3A ​        0011 1010  ​3A ​        0011 1010
  ​FC ​        1111 1100  ​FC ​        1111 1100
 +----------------------
 </​code>​ </​code>​
 +
 +Note that to be able to do this, you //​don'​t have to// understand the internal workings of the decimal, binary and hexadecimal number systems (although I encourage you to learn more on the web), nor do you have to know how to convert binary and HEX representations to and from decimal; all you need to know is which HEX digit corresponds to which binary sequence (and you can get that from the table). ​
  
 Since HEX digits include both numerals and characters, the script language needs a way to distinguish HEX numerical literals from other numbers and variable names. So, the language provides the ''​0x''​ prefix - when you want to express a number in the HEX format, start by typing ''​0x''​ and then immediately (with no spaces in between) follow with your hex digits: Since HEX digits include both numerals and characters, the script language needs a way to distinguish HEX numerical literals from other numbers and variable names. So, the language provides the ''​0x''​ prefix - when you want to express a number in the HEX format, start by typing ''​0x''​ and then immediately (with no spaces in between) follow with your hex digits:
hpl2/amnesia/script_language_reference_and_guide/constants_and_enumerations.txt ยท Last modified: 2015/10/06 21:06 by thegreatcthulhu