modif readme binary conversions recuperation methode deplacement binaires, extrait fonctions printBits dans fichier a part

This commit is contained in:
Hugo LAMY
2022-02-15 15:20:55 +01:00
parent c9a833bc00
commit 3e92c847fa
6 changed files with 63 additions and 50 deletions

37
d02/ex01/printBits.cpp Normal file
View 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 *)&num;
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 "";
}