* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download additional notes
Large numbers wikipedia , lookup
List of prime numbers wikipedia , lookup
History of logarithms wikipedia , lookup
Proofs of Fermat's little theorem wikipedia , lookup
Elementary arithmetic wikipedia , lookup
Location arithmetic wikipedia , lookup
Approximations of π wikipedia , lookup
One/Two’s complement/Hex One’s Binary Decimal 0111 0110 0101 0100 0011 0010 0001 0000 1111 1110 1101 1100 1011 1010 1001 1000 +7 +6 +5 +4 +3 +2 +1 +0 -0 -1 -2 -3 -4 -5 -6 -7 Here is a quick summary of how to find the 1's complement representation of any decimal number x. 1. If x is positive, simply convert x to binary. 2. If x is negative, write the positive value of x in binary 3. Reverse each bit. Example –convert 5 to one’s complement and vice versa 5 0101 -5 0101 1010 – reverses each bit -5 1010 Reverses to 0101 which is 5 1 As an example, the ones' complement form of 00101011 (43) becomes 11010100 (−43). The range of signed numbers using ones' complement is represented by −(2N−1−1) to (2N−1−1) and +/−0. A conventional eight-bit byte is −12710 to +12710 with zero being either 00000000 (+0) or 11111111 (−0). Binary Addition To add two numbers represented in this system, one does a conventional binary addition, but it is then necessary to add any resulting carry back into the resulting sum. To see why this is necessary, consider the following example showing the case of the addition of −1 (11111110) to +2 (00000010). binary decimal 11111110 -1 + 00000010 +2 ............ ... 1 00000000 0 <-- not the correct answer 1 +1 <-- add carry ............ ... 00000001 1 <-- correct answer VERY IMPORTANT(EVERYTHING STEMS FROM CALC BELOW FOR ONE’S vs.TWO’S COMPLEMENT!!! Adding binary numbers is a very simple task, and very similar to the longhand addition of decimal numbers. As with decimal numbers, you start by adding the bits (digits) one column, or place weight, at a time, from right to left. Unlike decimal addition, there is little to memorize in the way of rules for the addition of binary bits: 0 1 0 1 1 + + + + + 0 0 1 1 1 = = = = + 0 1 1 10 1 = 11 Just as with decimal addition, when the sum in one column is a two-bit (two-digit) number, the least significant figure is written as part of the total sum and the most significant figure is "carried" to the next left column. Consider the following examples: . . . . . 1001101 + 0010010 --------1011111 11 1 <--- Carry bits -----> 11 1001001 1000111 + 0011001 + 0010110 ----------------1100010 1011101 2 Two’s Complement The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically, from 2N for an N-bit two's complement). The two's complement of the number then behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way. A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value; this system is the most common method of representing signed integers on computers. In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement. An N-bit two's-complement numeral system can represent every integer in the range −2N−1 to +2N−1−1. Two's complement 0100 0011 0010 0001 0000 1111 1110 1101 1100 1111 1111 − 0101 1111 =========== 1010 0000 + 1 =========== 1010 0001 Decimal 4 3 2 1 0 −1 −2 −3 −4 255 − 95 ===== (ones' complement) 160 + 1 ===== (two's complement) 161 Example: Convert 4 to two’s complement value 4 0100 reverse (one’s complement) 1011 + 1 (perform binary add.) ===== Which is 1011 +0001 ===== 1100 3 1100 -4 0100 4 Example -4 1100 0011 (reverse) 3 + 1 1 (add 1) -----0100 4 HEX Hexadecimal is base 16. Base 16 is where the 'numbers' you can use are zero through to the letter F (0123456789ABCDEF). i.e. the decimal value for '1' is represented in hexadecimal as '1' but the hexadecimal value of '15' (decimal) is shown as 'F' (hexadecimal) and the value of '17' (decimal) is '11' in Hexadecimal. Decimal Hex Decimal Hex Decimal Hex 1 1 11 B 30 1E 2 2 12 C 40 28 3 3 13 D 50 32 4 4 14 E 60 3C 5 5 15 F 70 46 6 6 16 10 80 50 7 7 17 11 90 5A 8 8 18 12 100 64 9 9 19 13 500 1F4 10 A 20 14 1000 3E8 4 Hex to Decimal Steps: 1. 2. 3. 4. 5. 6. 7. Get the last digit of the hex number, call this digit the currentDigit. Make a variable, let's call it power. Set the value to 0. Multiply the current digit with (16^power), store the result. Increment power by 1. Set the the currentDigit to the previous digit of the hex number. Repeat from step 3 until all digits have been multiplied. Sum the result of step 3 to get the answer number. Example 1 Convert the number 1128 HEXADECIMAL to DECIMAL MULTIPLICATION RESULT NOTES Start from the last digit of the number. In this case, the number is 1128. The last digit of that 8 x (16^0) 8 number is 8. Note that the power of 0 of any number is always 1 Process the previous, which is 2. Multiply that 2 x (16^1) 32 number with an increasing power of 16. Process the previous digit, which is 1, note that 1 x (16^2) 256 16^2 means 16 x 16 Process the previous digit, which is 1, note that 1 x (16^3) 4096 16^3 means 16 x 16 x 16 Here, we stop because there's no more digit to process This number comes from the sum of the ANSWER 4392 RESULTS (8+32+256+4096)=4392 Once discerned, notice that the above process is essentially performing this calculation: 1x(16^3) + 1x(16^2) + 2x(16^1) + 8x(16^0) 5 When doing this by hand, it is easier to start backward is because: Counting the number of digits takes extra time, and you might count wrongly. If you don't remember what a particular value of a power-of-16 is, it's easier to calculate it from the previous power value. For instance, if you don't remember what the value of 16^3 is, then just multiply the value of 16^2 (which you'll likely already have if you started backward) with 16. Example 2 Convert the number 589 HEXADECIMAL to DECIMAL MULTIPLICATION 9 x (16^0) 8 x (16^1) 5 x (16^2) RESULT 9 128 1280 ANSWER 1417 If you want to be a speed counter, it's beneficial to memorize the values of the smaller power of 16s, such as in this table POWER OF 16s 16^0 16^1 = 16 16^2 = 16x16 16^3 = 16x16x16 16^4 = 16x16x16x16 RESULT 1 16 256 4096 65536 Example 3 Convert the number 1531 HEXADECIMAL to DECIMAL (This time, let's use the table of the power-of-16s above.) MULTIPLICATION 1x1 3 x 16 5 x 256 1 x 4096 RESULT 1 48 1280 4096 6 ANSWER 5425 7 Decimal to Hex Steps: 1. Divide the decimal number by 16. Treat the division as an integer division. 2. Write down the remainder (in hexadecimal). 3. Divide the result again by 16. Treat the division as an integer division. 4. Repeat step 2 and 3 until result is 0. 5. The hex value is the digit sequence of the remainders from the last to first. Note: a remainder in this topic refers to the left over value after performing an integer division. HEXADECIMAL 0 1 2 3 4 5 6 7 8 9 A DECIMAL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 B C D E F Example 1 Convert the number 1128 DECIMAL to HEXADECIMAL NOTES DIVISION RESULT REMAINDER (in HEXADECIMAL) 1128 / 16 70 8 Start by dividing the number by 16. In this case, 1128 divided by 16 is 70.5. So the integer division result is 70 (throw out anything after the decimal point). The remainder is (70.5 - 70) multiplied with 16; or (0.5 times 16), which is 8. Then, divide the result 70 / 16 again by 16 4 6 8 (the number 70 on the DIVISION column comes from the previous RESULT). In this case, 70/16=4.375. So the integer division result is 4 (throw out anything after the decimal point) The remainder is (0.375 multiplied with 16, which is 6. Repeat. Note here that 4/16=0.25. So the integer division result is 0. 4 / 16 0 4 The remainder is (0.25-0) multiplied with 16, which is 4. Stop because the result is already 0 (0 divided by 16 will always be 0) Well, here is the answer. These numbers come from the REMAINDER column values (read from bottom to top) 468 Side note: You can get the remainder of a division using the Modulus or % operator. Ie: 1128%16=8. 9 Example 2 Convert the number 256 DECIMAL to HEXADECIMAL DIVISION RESULT REMAINDER (in HEX) 256 / 16 16 0 16 / 16 1 0 1 / 16 0 1 ANSWER 100 Example 3 Convert the number 921 DECIMAL to HEXADECIMAL DIVISION RESULT REMAINDER (in HEX) 921 / 16 57 9 57 / 16 3 9 3 / 16 0 3 ANSWER 399 10 Example 4 Convert the number 188 DECIMAL to HEXADECIMAL DIVISION RESULT REMAINDER (in HEX) 188 / 16 11 C (12 decimal) 11 / 16 0 B (11 decimal) ANSWER BC Note that here, the answer would not be 1112, but BC. Remember to write down the remainder in hex, not decimal. Example 5 Convert the number 590 DECIMAL to HEXADECIMAL DIVISION RESULT REMAINDER (HEX) 590 / 16 36 E (14 decimal) 36 / 16 2 4 (4 decimal) 2 / 16 0 2 (2 decimal) ANSWER 24E 11 Fortran IF Construction The IF construction is important because it allows the program to make decisions. There are two forms: the single IF statement, and the block-IF construction. IF Statements. The IF statement takes the form: if ( condition ) action. What this means is that, if the condition in brackets is met, then the specified action is taken. The action is any single Fortran statement. The following piece of code illustrates the most common types of condition which may be used. A simple print statement is used as the action: integer a,b,c,d c a b c d = = = = 1 2 3 4 c if if if if if if (a.eq.b) (a.ne.b) (a.gt.b) (a.lt.b) (a.ge.b) (a.le.b) print print print print print print *, *, *, *, *, *, 'a 'a 'a 'a 'a 'a = b' not equal to b' is greater than b' is less than b' is greater than OR equal to b' is less than OR equal to b' c if ( (a.eq.b) .and. (c.eq.d) ) print *, 'a = b AND c = d' if ( (a.eq.b) .or. (c.eq.d) ) print *, 'either a = b, OR c = d' if ( .not. (a.eq.b) ) print *, 'a not equal b' c It should be clear what each of the above statements implies. The Block-IF Construction The block-IF construction is more sophisticated than the IF statement, allowing blocks of statements to be controlled by decision making conditions. The general format is as follows: integer a, b, c, d c a b c d = = = = 1 2 3 4 c if (a.eq.b) then c c.......Several lines of code here. c print *, 'a = b' 12 print *, 'hello' c else if (a.eq.c) then c c.......Several more lines of code here.c c print *, 'a = c' print *, 'hello' c else if (a.eq.d) then print *, 'a = d' print *, 'hello' else print *, 'a not equal to b, c or d' endif This is a complicated example. In fact, the else statements are optional, and you may use as many of them as you like. The important thing to note is the use of the endif statement which is obligatory. In its simplest form, the block-IF would look something like this: integer a, b c a = 1 b = 2 c if (a.eq.b) then c c.......Several lines of code here. c print *, 'a = b' print *, 'hello' c endif It is also possible to nest block-IF's as follows: integer a, b, c, d c a b c d = = = = 1 2 3 4 c if (a.eq.b) then if (c.eq.d) then print *, 'a = b and c = d' print *, 'hello' endif endif T 13 Modulus The following are available from Fortran 77: ABS, AIMAG, AINT, ANINT, CMPLX, CONJG, DBLE, DIM, DPROD, INT, MAX, MIN, MOD, NINT, REAL and SIGN. In addition, CEILING, FLOOR and MODULO have been added to Fortran 90. Only the last one is difficult to explain, which is most easily done with the examples from ISO (1991) MOD MOD MOD MOD (8,5) (-8,5) (8,-5) (-8,-5) gives 3 gives -3 gives 3 gives -3 MODULO MODULO MODULO MODULO (8,5) (-8,5) (8,-5) (-8,-5) gives 3 gives 2 gives -2 gives -3 According to the book Fortran 90-95 Explained, mod(a,p) returns a-int(a/p)*p and modulo(a,p) returns a-floor(a/p)*p if a and p are real, or a-floor(a//p)*p if a and p are integers, where // is "ordinary mathematical division". The sign of the result of mod(a,p) follows the sign of a whereas the sign of the result of modulo(a,p) follows the sign of p. 14