modif readme binary conversions recuperation methode deplacement binaires, extrait fonctions printBits dans fichier a part
This commit is contained in:
@@ -245,7 +245,7 @@ REPRESENTATION FLOATS vs INTEGERS
|
|||||||
---------------------------------------------------------
|
---------------------------------------------------------
|
||||||
|
|
||||||
```
|
```
|
||||||
integer : 1 (1)
|
integer : 1 (1)
|
||||||
floater : 0 01111111 00000000000000000000000 (1)
|
floater : 0 01111111 00000000000000000000000 (1)
|
||||||
|
|
||||||
integer : 1010 (10)
|
integer : 1010 (10)
|
||||||
@@ -266,7 +266,7 @@ floater : 0 10000100 00010 100000000000000000 (34.5)
|
|||||||
100010.100011001100110011 (34.55)
|
100010.100011001100110011 (34.55)
|
||||||
floater : 0 10000100 00010 100011001100110011 (34.55)
|
floater : 0 10000100 00010 100011001100110011 (34.55)
|
||||||
|
|
||||||
1.0001100110011001101 (1.1)
|
1.0001100110011001101 (1.1)
|
||||||
floater : 0 01111111 00011001100110011001101 (1.1)
|
floater : 0 01111111 00011001100110011001101 (1.1)
|
||||||
|
|
||||||
100010.01 (34.25)
|
100010.01 (34.25)
|
||||||
@@ -291,7 +291,7 @@ par multiplications binaires :
|
|||||||
100010.01000000 34.25
|
100010.01000000 34.25
|
||||||
* 1 00000000.00000000 * 256.00
|
* 1 00000000.00000000 * 256.00
|
||||||
----------------------- --------
|
----------------------- --------
|
||||||
100010 01000000.0 8768.00
|
100010 01000000 8768.00
|
||||||
|
|
||||||
34.25 * (1 << _frac)
|
34.25 * (1 << _frac)
|
||||||
```
|
```
|
||||||
@@ -301,10 +301,16 @@ par decalage binaire :
|
|||||||
10000100 (132) decaler la virgule de 132 - 127 = 5
|
10000100 (132) decaler la virgule de 132 - 127 = 5
|
||||||
100010.01 (34.25) fixe
|
100010.01 (34.25) fixe
|
||||||
|
|
||||||
value = 34.25;
|
this->_value = *((int *)&floater); // access float adress content as int
|
||||||
signe = (unsigned int)value;
|
int sign = this->_value & (1 << 31); // extract sign
|
||||||
exponent = ((unsigned int)(value << 1) >> 24) - 127;
|
int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent
|
||||||
fixedd = (unsigned int)(value << 9) >> (32 - exponent - 8);
|
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";
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,39 +1,5 @@
|
|||||||
#include "Fixed.hpp"
|
#include "Fixed.hpp"
|
||||||
|
#include "printBits.cpp"
|
||||||
/*
|
|
||||||
* functions to print numbers in binary
|
|
||||||
* for the float, found help from stackoverflow :
|
|
||||||
* https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec
|
|
||||||
*/
|
|
||||||
|
|
||||||
std::string printBitsInt(int num)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1)
|
|
||||||
{
|
|
||||||
std::cout << ((num & mask) != 0);
|
|
||||||
i++;
|
|
||||||
if (i == 1 || i == 9 || i == 24)
|
|
||||||
std::cout << ' ';
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string printBitsFloat(float num)
|
|
||||||
{
|
|
||||||
int *p = (int *)#
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1)
|
|
||||||
{
|
|
||||||
std::cout << ((*p & mask) != 0);
|
|
||||||
i++;
|
|
||||||
if (i == 1 || i == 9 || i == 24)
|
|
||||||
std::cout << ' ';
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* statics variables initialisation
|
* statics variables initialisation
|
||||||
@@ -41,12 +7,13 @@ std::string printBitsFloat(float num)
|
|||||||
* for MAX integer :
|
* for MAX integer :
|
||||||
* 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1))
|
* 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1))
|
||||||
* <= ... >=
|
* <= ... >=
|
||||||
* 11111111 10000000 00000000 00000000 (-8388608)
|
* 11111111 10000000 00000000 00000000 (-8388608) ~MAX
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int const Fixed::_frac = 8;
|
int const Fixed::_frac = 8;
|
||||||
int const Fixed::_max = -1U >> (_frac +1);
|
int const Fixed::_fmax = -1U >> (_frac +1);
|
||||||
|
int const Fixed::_fmin = ~_fmax;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* default constructor / copy constructor / destructor
|
* default constructor / copy constructor / destructor
|
||||||
@@ -75,7 +42,7 @@ Fixed::~Fixed( void ) {
|
|||||||
Fixed::Fixed(int integer) {
|
Fixed::Fixed(int integer) {
|
||||||
std::cout << "Int constructor called" << '\n';
|
std::cout << "Int constructor called" << '\n';
|
||||||
|
|
||||||
if (integer < ~this->_max || integer > this->_max)
|
if (integer < this->_fmin || integer > this->_fmax)
|
||||||
std::cout << "error: integer out of range" << '\n';
|
std::cout << "error: integer out of range" << '\n';
|
||||||
else
|
else
|
||||||
this->_value = integer << this->_frac;
|
this->_value = integer << this->_frac;
|
||||||
@@ -84,10 +51,10 @@ Fixed::Fixed(int integer) {
|
|||||||
Fixed::Fixed(float const floater) {
|
Fixed::Fixed(float const floater) {
|
||||||
std::cout << "Float constructor called" << '\n';
|
std::cout << "Float constructor called" << '\n';
|
||||||
|
|
||||||
if (floater < ~this->_max || floater > this->_max)
|
if (floater < this->_fmin || floater > this->_fmax)
|
||||||
std::cout << "error: float out of range" << '\n';
|
std::cout << "error: float out of range" << '\n';
|
||||||
else
|
else
|
||||||
this->_value = floater * (1 << this->_frac);
|
this->_value = roundf(floater * (1 << this->_frac));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -122,7 +89,6 @@ float Fixed::toFloat( void ) const {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* overload "<<" -> output fixed point in float representation
|
* overload "<<" -> output fixed point in float representation
|
||||||
* took here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
std::ostream & operator<<(std::ostream & o, Fixed const & rhs)
|
std::ostream & operator<<(std::ostream & o, Fixed const & rhs)
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ private:
|
|||||||
|
|
||||||
int _value;
|
int _value;
|
||||||
static int const _frac;
|
static int const _frac;
|
||||||
static int const _max;
|
static int const _fmax;
|
||||||
|
static int const _fmin;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
BIN
d02/ex01/fixed
Executable file
BIN
d02/ex01/fixed
Executable file
Binary file not shown.
@@ -1,6 +1,9 @@
|
|||||||
#include "Fixed.hpp"
|
#include "Fixed.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
std::string printBitsFloat(float num);
|
||||||
|
std::string printBitsInt(int num);
|
||||||
|
|
||||||
int main( void ) {
|
int main( void ) {
|
||||||
|
|
||||||
Fixed a;
|
Fixed a;
|
||||||
|
|||||||
37
d02/ex01/printBits.cpp
Normal file
37
d02/ex01/printBits.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* functions to print numbers in binary
|
||||||
|
* for the float, found help from stackoverflow :
|
||||||
|
* https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec
|
||||||
|
*/
|
||||||
|
|
||||||
|
std::string printBitsInt(int num)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1)
|
||||||
|
{
|
||||||
|
std::cout << ((num & mask) != 0);
|
||||||
|
i++;
|
||||||
|
if (i == 1 || i == 9 || i == 24)
|
||||||
|
std::cout << ' ';
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string printBitsFloat(float num)
|
||||||
|
{
|
||||||
|
int *p = (int *)#
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1)
|
||||||
|
{
|
||||||
|
std::cout << ((*p & mask) != 0);
|
||||||
|
i++;
|
||||||
|
if (i == 1 || i == 9 || i == 24)
|
||||||
|
std::cout << ' ';
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user