bexp
10/17/2017 - 4:11 AM

## Float

``````#include <iostream>
#include <vector>
#include <bitset>
#include <math.h>
#include <climits>

using namespace std;

/*
static string ToBinaryString(float value) {

int bitCount = sizeof(float) * 8; // never rely on your knowledge of the size
char[] result = new char[bitCount]; // better not use string, to avoid ineffective string concatenation repeated in a loop

// now, most important thing: (int)value would be "semantic" cast of the same
// mathematical value (with possible rounding), something we don't want; so:
int intValue = System.BitConverter.ToInt32(BitConverter.GetBytes(value), 0);

for (int bit = 0; bit < bitCount; ++bit) {
int maskedValue = intValue & (1 << bit); // this is how shift and mask is done.
// at this point, masked value is either int 0 or 1
result[bitCount - bit - 1] = maskedValue.ToString()[0]; // bits go right-to-left in usual Western Arabic-based notation
}

return new string(result); // string from character array
}
*/

void print(int* arr, int n) {

for (int i =0;i<n;i++) {
cout << arr[i] << " ";
}

cout << endl;

}

void permuate(int* arr, int size, int n) {
if (size == 1) {
print(arr, n);
} else {
for( int i = 0; i< size; i++) {
permuate(arr, size - 1, n);

cout << "i = " << i << " n = " << size << endl;
if (size%2==1)
swap(arr[0], arr[size-1]);
else
swap(arr[i], arr[size-1]);
}
}
}

void permute(int a[], int i, int n)
{
int j;
cout << "permute " << i << endl;
if (i == n)
print(a, n);
else
{
for (j = i; j < n; j++)
{
swap(a[i], a[j]);
cout << "swap i = " << i << " j = " << j << endl;
permute(a, i+1, n);
swap(a[i], a[j]);

}

cout << "exit " << i << endl;
}
}

// Convert the 32-bit binary encoding into hexadecimal
int Binary2Hex( std::string Binary )
{
std::bitset<32> set(Binary);
int hex = set.to_ulong();

return hex;
}

string GetBinaryFloat(float flo) {
int integral = flo;

float fraction = flo - integral;

int shift = 0;
uint32_t mantissa = 0;
while(integral > 0) {
if (integral % 2 != 0) {
mantissa |= 1 << shift;
}

integral >>= 1;
shift++;
}

uint32_t exponent = 127 + shift -1;

cout << exponent << endl;

cout << mantissa << endl;
cout << bitset<32>(mantissa).to_string() << endl;
mantissa = mantissa & ~(1 << (shift - 1));
cout << mantissa << endl;
cout << bitset<32>(mantissa).to_string() << endl;

while (fraction > 0) {
fraction *= 2;
if ((int)fraction > 0) {
fraction -=1;
mantissa = (mantissa << 1) ^1 ;
} else {
mantissa = (mantissa << 1);
}

shift++;
}

mantissa = mantissa << (32 - (shift -1)  - 9);

uint32_t ieee745 = (exponent << 23 ) | mantissa;

cout << bitset<32>(ieee745).to_string() << endl;

float cross = *(float*)&ieee745;

cout << "shift " << mantissa << endl;
cout << "cross " << cross << endl;

}

// Convert the 32-bit binary into the decimal
float GetFloat32( std::string Binary )
{
int HexNumber = Binary2Hex( Binary );

bool negative  = (HexNumber >> 31) != 0;
int  exponent  =  (HexNumber >> 23) & 0xFF;
int sign = negative ? -1 : 1;

// Subtract 127 from the exponent
exponent -= 127;

// Convert the mantissa into decimal using the
// last 23 bits
int power = -1;
float total = 0.0;
for ( int i = 9; i < 32; i++ )
{
int c = Binary[ i ] - '0';
total += (float) c * (float) pow( 2.0, power );
power--;
}
total += 1.0;

float value = sign * (float) pow( 2.0, exponent ) * total;

return value;
}

// Get 32-bit IEEE 754 format of the decimal value
std::string GetBinary32( float value )
{
union
{
float input;   // assumes sizeof(float) == sizeof(int)
int   output;
}    data;

data.input = value;

std::bitset<sizeof(float) * CHAR_BIT>   bits(data.output);

std::string mystring = bits.to_string();

return mystring;
}

// To execute C++, please define "int main()"
int main() {

///int arr[] = { 1, 2, 3};
//permute(arr, 0, 3);

// Convert 19.5 into IEEE 754 binary format..
string str = GetBinary32( (float) 210.15 );
cout << "Binary equivalent of 210:" << std::endl;
cout << str << std::endl << std::endl;

// .. and back again
float f = GetFloat32( str );
cout << "Decimal equivalent of " << str << ":" << std::endl;
cout << f << std::endl;

//int power = -1;
//cout << pow( 1.0, power ) << endl;

cout << GetBinaryFloat(210.125) << endl;

return 0;
}

``````