--------------------------------------------------------- DECIMALE VS BINARY --------------------------------------------------------- 11010101 ``` 1*10000000 | (128) | 2^7 | 128 *1 | 0 *2 +1 1*1000000 | (64) | 2^6 | + 64 *1 | 1 *2 +1 0*100000 | (32) | 2^5 | | 11 *2 1*10000 | (16) | 2^4 | + 16 *1 | 110 *2 +1 0*1000 | (8) | 2^3 | | 1101 *2 1*100 | (4) | 2^2 | + 4 *1 | 11010 *2 +1 0*10 | (2) | 2^1 | | 110101 *2 1*1 | (1) | 2^0 | + 1 *1 | 1101010 *2 +1 | ______ | | 213 | ``` 213 ``` 2*100 |(1100100)| 10^2 | 1100100 *2 | 0 *10 +2 1*10 | (1010)| 10^1 | + 1010 *1 | 2 *10 +1 3*1 | (1)| 10^0 | + 1 *3 | 21 *10 +3 | | | ___________ | 213 | | | 11010101 | | | 1100100 11001000 1 11010010 | * 2 + 1010 * 3 + 11 | _________ _________ ___ _________ | 11001000 11010010 11 11010101 | ``` 213 -> 11010101 ``` 213 -128 = 85 | 213 /2 = 106 r 1 85 - 64 = 2 | 106 /2 = 53 r 0 | 53 /2 = 26 r 1 21 - 16 = 5 | 26 /2 = 13 r 0 | 13 /2 = 6 r 1 5 - 4 = 1 | 6 /2 = 3 r 0 | 3 /2 = 1 r 1 1 - 1 = 0 | 1 /2 = 0 r 1 ``` 11010101 -> 213 ``` | 0*2 + 1 = 1 | 1*2 + 1 = 3 | 3*2 = 6 | 6*2 + 1 = 13 | 13*2 = 26 | 26*2 + 1 = 53 | 53*2 = 106 | 106*2 + 1 = 213 ``` --------------------------------------------------------- NEGATIVS INTEGERS --------------------------------------------------------- ``` 5 0101 0 0000 0 0000 0000 0000 0000 0000 0000 0000 0000 1010 + 1 1 0001 1 0000 0000 0000 0000 0000 0000 0000 0001 -5 1011 2 0010 2 0000 0000 0000 0000 0000 0000 0000 0010 3 0011 3 0000 0000 0000 0000 0000 0000 0000 0011 4 0100 4 0000 0000 0000 0000 0000 0000 0000 0100 -> 5 0101 5 0000 0000 0000 0000 0000 0000 0000 0101 6 0110 6 0000 0000 0000 0000 0000 0000 0000 0110 7 0111 7 0000 0000 0000 0000 0000 0000 0000 0111 ... 0111 1111 1111 1111 1111 1111 1111 1111 MAXINT 1000 0000 0000 0000 0000 0000 0000 0000 MININT ... 8 1000 -8 1111 1111 1111 1111 1111 1111 1111 1000 9 1001 -7 1111 1111 1111 1111 1111 1111 1111 1001 10 1010 -6 1111 1111 1111 1111 1111 1111 1111 1010 -> 11 1011 -5 1111 1111 1111 1111 1111 1111 1111 1011 12 1100 -4 1111 1111 1111 1111 1111 1111 1111 1100 13 1101 -3 1111 1111 1111 1111 1111 1111 1111 1101 14 1110 -2 1111 1111 1111 1111 1111 1111 1111 1110 15 1111 -1 1111 1111 1111 1111 1111 1111 1111 1111 ``` --------------------------------------------------------- FLOATS --------------------------------------------------------- https://stackoverflow.com/questions/7644699/how-are-floating-point-numbers-stored-in-memory ``` 1.....................23 8......1 seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm meaning 31 0 bit # signe exponent mantis ``` 1.175494351 * 10^-38 < ... > 3.40282347 * 10^+38 2x10^-1 = 0.2x10^0 = 0.02x10^1 = 0.2 - x 00000000 xxxxxxxx xxxxxxxx xxxxxxx special meanings (see bellow) - x 11111111 xxxxxxxx xxxxxxxx xxxxxxx // - x 00000001 xxxxxxxx xxxxxxxx xxxxxxx smallest exponent (-126) - x 01111111 xxxxxxxx xxxxxxxx xxxxxxx 0 is middle exponent (254 / 2 = 127) - x 11111110 xxxxxxxx xxxxxxxx xxxxxxx biggest exponent (+127) - x 00000000 00000000 00000000 0000000 0 (x can still be + or -) - x 11111111 00000000 00000000 0000000 +/- infinity - x 11111111 xxxxxxxx xxxxxxxx xxxxxx1 NaN (at least one non-zero mantissa digit) - x 00000000 xxxxxxxx xxxxxxxx xxxxxx1 denormalized numbers (same for mantissa) --------------------------------------------------------- CONVERSIONS --------------------------------------------------------- 5.75 ``` 5 : 5 / 2 = 2 -> 1 2 / 2 = 1 -> 0 1 / 2 = 0 -> 1 75 : .75 *2^2 = 2,88 ~= 3 (*2^x parce que c le moyen dont fait "bouger" la virgule en base 2 (*10^x) en base 10) 3 / 2 = 1 -> 1 1 / 2 = 0 -> 1 101.11 = 2^2 + 2^0 + 2^-1 + 2^-2 = 4 + 1 + 0.5 + 0.25 ``` 0.875 ``` .875 * 2^3 = 7 -> 0.111 * 2^3 = 111.0 7 / 2 = 3 -> 1 3 / 2 = 1 -> 1 1 / 1 = 0 -> 1 0.111 ``` 0.1875 ``` -1 1875 0.1875 = 1.875 * 10^-1 = 01 11101010011 0.1875 = 0011 0*2^-1 + 082^-2 + 1*2^-3 + 1*2^-4 0 + 0 + .125 + .0625 = .1875 ``` -43.625 ``` -101011.101 fixed : 1 101011 101 - 101011 2^5 + 2^3 + 2^1 + 1 32 + 8 + 2 + 1 = 43 101 2^-1 + 2^-3 .5 + .125 = .625 ``` -53.5 --> -110101.1 ``` [ fixed : ] 1 110101 1 - 110101 2^5 + 2^4 + 2^2 + 1 32 + 16 + 4 + 1 = 53 1 2^-1 .5 = .5 [ float : ] -1.101011 * 2^5 11.01011 -> 1 110.1011 -> 2 1101.011 -> 3 11010.11 -> 4 110101.1 -> 5 1 101 101011 - 2^2 + 1 4 + 1 = 5 101011 ->1101011 ``` .85 ``` .85 * 2 = 1.7 [1] [.7] .7 * 2 = 1.4 [1] [.4] .4 * 2 = 0.8 [0] [.8] .8 * 2 = 1.6 [1] [.6] .6 * 2 = 1.2 [1] [.2] .2 * 2 = 0.4 [0] [.4] .4 * 2 = 0.8 [0] [.8] .8 * 2 = 1.6 [1] [.6] .6 * 2 = 1.2 [1] [.2] .2 * 2 = 0.4 [0] [.4] .4 * 2 = 0.8 [0] [.8] .8 * 2 = 1.6 [1] [.6] .6 * 2 = 1.2 [1] [.2] .2 * 2 = 0.4 [0] [.4] .4 * 2 = 0.8 [0] [.8] .8 * 2 = 1.6 [1] [.6] .6 * 2 = 1.2 [1] [.2] .2 * 2 = 0.4 [0] [.4] .4 * 2 = 0.8 [0] [.8] .8 * 2 = 1.6 [1] [.6] .6 * 2 = 1.2 [1] [.2] .2 * 2 = 0.4 [0] [.4] .4 * 2 = 0.8 [0] [.8] ... -> 11011001100110011001100... 1 1 0 1 1 0 0 1 1 0 0 1 1 2^-1 + 2^-2 + 0 + 2^-4 + 2^-5 + 0 + 0 + 2^-8 + 2^-9 + 0 + 0 + 2^-12 + 2^-13 .5 + .25 + .0625 + .03125 + .00390625 + .001953125 + .000244140625 + .0001220703125 = .8499755859375 .5 .75 .8125 .84375 .84765625 .849609375 .849853515625 .8499755859375 ``` .453125 ``` .453125 *2 = 0.90625 [o] [.90625] .90625 *2 = 1.8125 [1] [.8125] .8125 *2 = 1.625 [1] [.625] .625 *2 = 1.25 [1] [.25] .25 *2 = 0.5 [0] [.5] .5 *2 = 1 [1] [] -> .011101 ``` --------------------------------------------------------- REPRESENTATION FLOATS vs INTEGERS --------------------------------------------------------- ``` integer : 1 (1) floater : 0 01111111 00000000000000000000000 (1) integer : 1010 (10) floater : 0 10000010 01000000000000000000000 (10) integer : 1100100 (100) floater : 0 10000101 10010000000000000000000 (100) integer : 1111101000 (1000) floater : 0 10001000 11110100000000000000000 (1000) integer : 00000000000000000000000000000010 (2) floater : 0 10000000 00000000000000000000000 (2) 100010.1 (34.5) floater : 0 10000100 00010 100000000000000000 (34.5) 100010.100011001100110011 (34.55) floater : 0 10000100 00010 100011001100110011 (34.55) 1.0001100110011001101 (1.1) floater : 0 01111111 00011001100110011001101 (1.1) 100010.01 (34.25) floater : 0 10000100 00010 010000000000000000 (34.25) 10101.0011100001010001111 (21.22) floater : 0 10000011 0101 0011100001010001111 (21.22) 110.11001100110011001101 (6.8) floater : 0 10000001 10 110011001100110011010 (6.8) ``` --------------------------------------------------------- FLOATS -> FIXED --------------------------------------------------------- par multiplications binaires : ``` 100010.01 (34.25) 00100010 01000000 (8768) 00000001 00000000 (256) 100010.01000000 34.25 * 1 00000000.00000000 * 256.00 ----------------------- -------- 100010 01000000 8768.00 34.25 * (1 << _frac) ``` par decalage binaire : ``` 0 10000100 00010 010000000000000000 (34.25) float 10000100 (132) decaler la virgule de 132 - 127 = 5 100010.01 (34.25) fixe this->_value = *((int *)&floater); // access float adress content as int int sign = this->_value & (1 << 31); // extract sign int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent int integer = (this->_value << 8) | (1 << 31); // add left 1 integer = (unsigned int)integer >> (31 - this->_frac - exponent);// align to right if (sign != 0) integer = (~integer + 1); // reverse negatif integer = (integer << (30 - this->_frac - exponent)) | sign; // add sign integer >>= (30 - this->_frac - exponent); // align right std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n"; ```