diff --git a/d02/ex01/Fixed.cpp b/d02/ex01/Fixed.cpp index 63a5fa8..f94c568 100644 --- a/d02/ex01/Fixed.cpp +++ b/d02/ex01/Fixed.cpp @@ -10,12 +10,10 @@ std::string printBitsInt(int num) { int i = 0; - std::cout << " "; for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1) { std::cout << ((num & mask) != 0); i++; -// if (i % 8 == 0 && i < 32) if (i == 1 || i == 9 || i == 24) std::cout << ' '; } @@ -31,7 +29,7 @@ std::string printBitsFloat(float num) { std::cout << ((*p & mask) != 0); i++; - if (i == 1 || i == 9) + if (i == 1 || i == 9 || i == 24) std::cout << ' '; } return ""; @@ -84,6 +82,7 @@ Fixed::Fixed(int integer) { } std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n"; this->_value = integer << this->_frac; + std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; } Fixed::Fixed(float const floater) { @@ -95,6 +94,50 @@ Fixed::Fixed(float const floater) { return; } std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n"; + this->_value = floater * (1 << this->_frac); + std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; + + this->_value = *((int *)&floater); // cast floater into int +std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; + + int sign = this->_value & (1 << 31); // set most left bits to sign +std::cout << "sign : " << printBitsInt(sign) << " (" << sign << ")\n"; + + int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent value +std::cout << "exponent: " << printBitsInt(exponent) << " (" << exponent << ")\n"; + + int decimal = (this->_value << (9)); +std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n"; + decimal = (this->_value << (9 + exponent)); +std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n"; + + int integer = (this->_value << 8) | (1 << 31); // add left 1 to integer part +std::cout << "left 1 : " << printBitsInt(integer) << " (" << integer << ")\n"; + if (sign != 0) + integer = ~integer; // reverse for negatif +std::cout << "reverse : " << printBitsInt(integer) << " (" << integer << ")\n"; + integer = ((unsigned int)integer >> 1) | sign; // add sign to integer part +std::cout << "left +/-: " << printBitsInt(integer) << " (" << integer << ")\n"; + integer >>= (30 - exponent); // extract integer part +std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n"; + +// this->_value = (unsigned int)(this->_value << 8); // shift to left to erase exponent part +// this->_value |= 1 << 31; // add the missing left 1 +//std::cout << "to left : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +// this->_value = (unsigned int)this->_value >> 1; +//std::cout << "to right: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +// this->_value |= sign; +//std::cout << "sign : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +// this->_value = this->_value >> (30 - exponent - this->_frac); // shift to right untill fixed point +//std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +//// int decimal = (unsigned int)(this->_value << (24)) >> 24; // extract decimal +////std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n"; +// this->_value >>= this->_frac; +//std::cout << "int valu: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +//// this->_value += (unsigned int)sign >> 31; +////std::cout << "val sign: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +// this->_value = (this->_value << this->_frac); +//std::cout << "val sign: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; } diff --git a/d02/ex01/fixed b/d02/ex01/fixed index e147d71..7171e14 100755 Binary files a/d02/ex01/fixed and b/d02/ex01/fixed differ diff --git a/d02/ex01/main.cpp b/d02/ex01/main.cpp index 252cc46..78e4ce0 100644 --- a/d02/ex01/main.cpp +++ b/d02/ex01/main.cpp @@ -3,7 +3,12 @@ int main( void ) { - Fixed a(1105979538); +// Fixed a(1105979538); + Fixed a(1105979); + std::cout << '\n'; + Fixed b(1000.5979f); + std::cout << '\n'; + Fixed c(-1105.979f); // Fixed a; // Fixed const b( 10 );