![]() |
Basic Explanation Of Binary Numbers And NOT O |
The information in this document applies to:
WordPerfect® 5.1 for DOS
Problem
Symptoms: The customer is using Binary Code 1101100 (26) and the NOT (!) operator in a macro. It is returning -27 instead of the expected 5 for 0010011. The macro simply reads {ASSIGN}num~!26~. Solutions: The memo below explains the principles as to why !26 is equal to -27 and not 5. The memo is prefaced how numbers work in each base numbering system, then explains the NOT (!) operator. ------------------------------------------------------------------ BASE CONVERSION ------------------------------------------------------------------ DECIMAL (Base 10; 0 to 9) The standard numbering system everyone is used to is called Decimal Numbering. It uses a numbering system that uses characters 0 to 9 to represent all numbers. This system uses a total of ten characters (0 to 9) which is why this system can also be called, "base ten" or "Decimals" (dec meaning 10). Each decimal place cycles numbers (0 to 9) before the next decimal place increases. For instance, if you were to add a 1 to the number of 19, the "9" goes back to 0 and the "1" increases to a 2. You now end up with a "2" and a "0" which means 20. Each decimal place, in the numbering scale, has a name. For instance, the 2 in the number "20" is in the "tens" place, saying that you have 2 tens. The 0 means that you now have 0 "ones." So, 2 tens plus 0 ones adds up to 20. If you were to add another 1 to the number 20, you would have 2 tens plus 1 ones which equals 21. Take, for example the number 39,521 (the value of the decimal place is represented below the example decimal number): 3 9 5 2 1 ----- ---- --- -- - 10000 1000 100 10 1 This example says you have 3 "ten thousands," 9 "thousands," 5 "hundreds," 2 "tens," and 1 "ones." To add up these set of numbers is illustrated below: 3*(10,000) 9*(1,000) 5*(100) 2*(10) + 1*(1) ------------ = 39,521 HEXADECIMAL (Base 16; 0 to F) Hexadecimal numbering uses a numbering system that uses characters 0 to F (0123456789ABCDEF) to represent all numbers. This system uses a total of sixteen characters (0 equals 0 and F equals 15) which is why this system can also be called, "base 16" or "Hex" for short. Each digit cycles numbers (0 to F) before the next greater digit increases. For instance, for decimals (base 10) the decimal place counts to 9 until the next greater digit increases. In Hex (base 16), the digit counts to 15 (F) until the next greater digit increases. Take for example the number FFFF (the value of each place is represented below the number): F F F F ---- --- -- - 4096 256 16 1 Which means: 15*(4096) = 61440 15*(256) = 3840 15*(16) = 240 + 15*(1) = + 15 ---------- --------- 65535 65535 OR C D E E ---- --- -- - 4096 256 16 1 Which means: 12*(4096) = 49152 13*(256) = 3328 14*(16) = 224 + 14*(1) = + 14 ---------- --------- 52718 52718 BINARY (Base 2; 0 to 1) Binary numbering uses a numbering system that uses characters 0 to 1 to represent all numbers. This system uses a total of two characters (0 to 1) which is why this system can also be called "base two" or Binary (Bi meaning 2). Each digit cycles numbers (0 to 1) before the next greater digit increases. Each place represents a value (as do Decimals and Hexadecimals). A chart is represented below for a 32 bit binary number. The Binary numbering system is used in CPU's (Central Processing Units) to carry out all calculating and processing. A CPU contains many "switches" that are either ON or OFF. To a CPU, "ON" means 1 and "OFF" means 0. The CPU understands series of 1's and 0's to mean character values, numbers, and processing instructions. If the CPU wanted to express to you the decimal number "11", it would adjust its switches to show you a series of 1's and 0's that would look like 1011. Take, for example, the binary number of 1011. 1 0 1 1 - - - - 8 4 2 1 Which means: 8 2 + 1 ---- 11 Binary Chart Bit Value 1 1*----------------------------------------- 2 2*----------------------------------------| 3 4*---------------------------------------|| 4 8*--------------------------------------||| 5 16*------------------------------------|||| 6 32*-----------------------------------||||| 7 64*----------------------------------|||||| 8 128*--------------------------------||||||| 9 256*-------------------------------|||||||| 10 512*------------------------------||||||||| 11 1024*----------------------------|||||||||| 12 2048*---------------------------||||||||||| 13 4096*--------------------------|||||||||||| 14 8192*-------------------------||||||||||||| 15 16384*-----------------------|||||||||||||| 16 32768*----------------------||||||||||||||| 17 65536*---------------------|||||||||||||||| 18 131072*-------------------||||||||||||||||| 19 262144*------------------|||||||||||||||||| 20 524288*-----------------||||||||||||||||||| 21 1048576*---------------|||||||||||||||||||| 22 2097152*--------------||||||||||||||||||||| 23 4194304*-------------|||||||||||||||||||||| 24 8388608*------------||||||||||||||||||||||| 25 16777216*----------|||||||||||||||||||||||| 26 33554432*---------||||||||||||||||||||||||| 27 67108864*--------|||||||||||||||||||||||||| 28 134217728*------||||||||||||||||||||||||||| 29 268435456*-----|||||||||||||||||||||||||||| 30 536870912*----||||||||||||||||||||||||||||| 31 1073741824*--|||||||||||||||||||||||||||||| 32 2147483648*-||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||| ******************************** 00000000000000000000000000000000 WordPerfect DOS 5.1 macros use 32 bit precision, which means WP is accurate up to a 32 digit binary number. The largest positive number that WP macros can use is 01111111111111111111111111111111, which is 2,147,483,647 (see WP 5.1 DOS new Ref. Manual Appendix J, pg 765 "Macro and Merge, Expressions"). Notice that all digits of the 32 bit binary number are 1 (or ON) except the left most digit (32nd) which is a 0 (OFF.) If all the bits are ON, including the 32nd bit (which would be 1111111111111111111111111111111), then (according to the rules of the Binary numbering system) this binary number would equal a -1. Anytime the left most bit is ON, it denotes the number represented is a negative number. When the left most bit is ON, any digit that is OFF subtracts that value from -1. If the left most digit is OFF, then any digit that is ON adds to the value of 0. If you were to write a -2 in binary, it would be 11111111111111111111111111111110. Notice that bit 1 (the value for 1 is OFF. The 1 is then subtracted from -1. Also, if you want to write a -9 in binary, it would be 11111111111111111111111111110111. Notice that bit 4 (which represents a value of 8) is OFF. Thus, -1 minus 8 equals -9. ------------------------------------------------------------------ OPERATORS AND EXPRESSIONS: ------------------------------------------------------------------ The NOT (!) operator: A customer wanted to take the NOT of 26 (!26) and expected a 5 back (like {ASSIGN}a~!26~). He was getting a -27 back. The number 26 looks like: 00000000000000000000000000011010 The customer thought taking the NOT of 26 would be a 5: 00000000000000000000000000000101 In reality it returned a -27: 11111111111111111111111111100101 Notice that bits 2, 8, and 16 are off, which are subtracted from -1. Thus, -1 minus a total of 26 (2+8+16) equals -27. Also notice all the bits that were OFF are now ON and all the bits that were ON are now OFF (compare the first and third examples above). The AND (&) operator: Example: {IF}({VARIABLE}1~=5)&({VARIABLE}2~=1)~. The logic here is, if variable 1 is 5 and variable 2 is equal to 1, then the statement would be taking the AND of -1 and -1 which is equal to -1 (because the statement would read {IF}(-1&-1)=-1~). Since -1 means True, then the whole statement is true because True and True equals True. What the statement is saying is {IF}{VARIABLE}1~=5~ (that would return True i.e., -1) and {IF}{VARIABLE}2~=1~ (that would return true i.e., -1), then take the AND of -1 and -1. You could read the statement as -1&-1=-1. Since -1 equals 1111111111111111, the AND is taking the AND of the following: -1 1111111111111111 -1 1111111111111111 = ________________ -1 1111111111111111 The way you read the above example is this: look a the last column on the right and say, "is one on the top equal to 1 AND is the one the bottom equal to 1?" It is, so you put a 1 on the bottom where you write the answer. You would continue this process with all the rest of the columns going right to left (total of 16 columns) until you are done. You end up with 1111111111111111, which is true. The OR (|) operator: Let's say variable 1 equals 5 and variable 2 equals 10. And let's say you want to make a logical OR out of this. You want the statement to be true if variable 1 is equal to 5 OR variable 2 is equal to 1. If you write the command this way: {IF}({VARIABLE}1~=5)|({VARIABLE}2~=1)~ the command would return true if variable 1 equals 5 OR variable 2 equals 1. The statement can be translated as: -1|0=-1. Since variable 1 equals 5 it returns true (-1) and since variable 2 equals 10, the statement would return false (0). Again, what is happening here is this: -1 1111111111111111 0 0000000000000000 = ________________ -1 1111111111111111 You would then start the process of saying (in the right most column) "is the top number a 1 OR the bottom number a 1?" This would be true, because the top or the bottom is a 1 (i.e., is any one of them a 1?). Since it is true, you write a 1 on the answer line. What is mentioned in the manual and here, in this explanation, gives the user a lot of flexibility in their expression logic, especially for macros. |
Answer:
Details:
Product specifications, packaging, technical support and information (*Specifications*) refer to theUnited States retail English version only. Specifications for other versions may vary. All Specifications, claims, features, representations, and/or comparisons provided are correct to the best of our knowledge of the date of publication, but are subject to change without notice.OUR MAXIMUM AGGREGATE LIABILITY TO YOU AND THAT OF OUR DEALERS AND SUPPLIERS IS LIMITED. IT SHALL NOTEXCEED THE AMOUNT YOU PAID TO ACCESS THE INFORMATION. SEE LEGAL DISCLAIMER. |