-
Notifications
You must be signed in to change notification settings - Fork 0
/
float_in_mem.cpp
56 lines (51 loc) · 2.02 KB
/
float_in_mem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* float_in_mem.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abouramd <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/18 16:49:29 by abouramd #+# #+# */
/* Updated: 2023/07/20 11:03:45 by abouramd ### ########.fr */
/* */
/* ************************************************************************** */
#include <iomanip>
#include <iostream>
int convert(std::string s)
{
int dec;
int pow;
dec = 0;
pow = 1;
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == '0')
pow *= 2;
if (s[i] == '1')
{
dec += pow;
pow *= 2;
}
}
return (dec);
}
int main(void)
{
float num;
unsigned int *bytes;
num = 0.25; // Change this to the number you want to observe
bytes = (unsigned int *)(&num);
std::string bit;
for (int i = 31, j = 0; i >= 0; i--)
{
bit += (((*bytes) >> i) & 1) + '0';
if (i == 31 || i == 23)
bit += " ";
}
std::cout << "Memory representation of the float value " << "\033[1;31m[ " << num << " ]\033[0m" << ": " << bit << std::endl;
std::cout << "Sign: " << "\033[1;32m" << bit.substr(0, 1) << "\033[0m" << bit.substr(1) << std::endl;
std::cout << "Exponent: " << bit.substr(0, 2) << "\033[1;32m" << bit.substr(2, 8) << "\033[0m" << bit.substr(10) << std::endl;
std::cout << "Fraction (mantissa): " << bit.substr(0, 11) << "\033[1;32m" << bit.substr(11) << "\033[0m" << std::endl;
std::cout << "Exponent in dec: " << convert(bit.substr(2, 8)) << " (" << convert(bit.substr(2, 8)) - 127 << ")" << std::endl;
return (0);
}