#include <iostream>
using namespace std;
void print_binary_representation(unsigned int x)
{
size_t siz = 8*sizeof(x); // sizeof returns size in bytes.
for (size_t i = siz; i>0; i--)
{
int val = (((x >> (i-1)) << (siz-1)) >> (siz-1));
cout << val;
if ( i == 32 || i == 24)
cout << ",";
}
cout << endl;
}
int main()
{
float x = 2.0;
cout << "Size of float: " << sizeof(float) << endl;
cout << "Size of unsigned int: " << sizeof(unsigned int) << endl;
do
{
void *vx = &x;
unsigned int xui = *(unsigned int*)vx;
unsigned int xui_direct = (unsigned int)x;
cout << "Representation of " << x << " as unsigned int: " << xui << ", binary: ";
print_binary_representation(xui);
cout << "Enter number: ";
cin >> x;
}while( x != 1234);
return 0;
}
using namespace std;
void print_binary_representation(unsigned int x)
{
size_t siz = 8*sizeof(x); // sizeof returns size in bytes.
for (size_t i = siz; i>0; i--)
{
int val = (((x >> (i-1)) << (siz-1)) >> (siz-1));
cout << val;
if ( i == 32 || i == 24)
cout << ",";
}
cout << endl;
}
int main()
{
float x = 2.0;
cout << "Size of float: " << sizeof(float) << endl;
cout << "Size of unsigned int: " << sizeof(unsigned int) << endl;
do
{
void *vx = &x;
unsigned int xui = *(unsigned int*)vx;
unsigned int xui_direct = (unsigned int)x;
cout << "Representation of " << x << " as unsigned int: " << xui << ", binary: ";
print_binary_representation(xui);
cout << "Enter number: ";
cin >> x;
}while( x != 1234);
return 0;
}
Output:
Size of float: 4
Size of unsigned int: 4
Representation of 2 as unsigned int: 1073741824, binary: 0,10000000,00000000000000000000000
Enter number: -4
Representation of -4 as unsigned int: 3229614080, binary: 1,10000001,00000000000000000000000
Enter number: 8
Representation of 8 as unsigned int: 1090519040, binary: 0,10000010,00000000000000000000000
Enter number: -8
Representation of -8 as unsigned int: 3238002688, binary: 1,10000010,00000000000000000000000
Enter number: 123456
Representation of 123456 as unsigned int: 1206984704, binary: 0,10001111,11100010010000000000000
Enter number: -123456
Representation of -123456 as unsigned int: 3354468352, binary: 1,10001111,11100010010000000000000
Enter number: 1234
Size of unsigned int: 4
Representation of 2 as unsigned int: 1073741824, binary: 0,10000000,00000000000000000000000
Enter number: -4
Representation of -4 as unsigned int: 3229614080, binary: 1,10000001,00000000000000000000000
Enter number: 8
Representation of 8 as unsigned int: 1090519040, binary: 0,10000010,00000000000000000000000
Enter number: -8
Representation of -8 as unsigned int: 3238002688, binary: 1,10000010,00000000000000000000000
Enter number: 123456
Representation of 123456 as unsigned int: 1206984704, binary: 0,10001111,11100010010000000000000
Enter number: -123456
Representation of -123456 as unsigned int: 3354468352, binary: 1,10001111,11100010010000000000000
Enter number: 1234
Using Visual Studio 2012 64-bit Compiler.
| Number | Sign | Exponent | Fraction | Exp-val | expected expansion of fraction |
| 4 | 0 | 10000001 | 00000000000000000000000 | 2 | 1 |
| -4 | 1 | 10000001 | 00000000000000000000000 | 2 | 1 |
| 8 | 0 | 10000010 | 00000000000000000000000 | 3 | 1 |
| -8 | 1 | 10000010 | 00000000000000000000000 | 3 | 1 |
| 123456 | 0 | 10001111 | 11100010010000000000000 | 16 | 1.883789063 |
| -123456 | 1 | 10001111 | 11100010010000000000000 | 16 | 1.883789063 |
| 2.76 | 0 | 10000000 | 01100001010001111010111 | 1 | 1.38 |
Note that in "expected expansion of fraction" column, all numbers start are of the form 1.*. So while storing the fraction portion this 1 is omitted and Fraction expands only to the part after decimal point. For e.g. 01100001010001111010111 evaluates to 0.38
example of decimal to binary fraction conversion:
| 0.38 | 0.76 | 0 |
| 0.76 | 1.52 | 1 |
| 0.52 | 1.04 | 1 |
| 0.04 | 0.08 | 0 |
| 0.08 | 0.16 | 0 |
| 0.16 | 0.32 | 0 |
| 0.32 | 0.64 | 0 |
| 0.64 | 1.28 | 1 |
| 0.28 | 0.56 | 0 |
| 0.56 | 1.12 | 1 |
| 0.12 | 0.24 | 0 |
| 0.24 | 0.48 | 0 |
| 0.48 | 0.96 | 0 |
| 0.96 | 1.92 | 1 |
| 0.92 | 1.84 | 1 |
| 0.84 | 1.68 | 1 |
| 0.68 | 1.36 | 1 |
| 0.36 | 0.72 | 0 |
| 0.72 | 1.44 | 1 |
| 0.44 | 0.88 | 0 |
| 0.88 | 1.76 | 1 |
| 0.76 | 1.52 | 1 |
Note that the pattern would repeat itself since the last row and second row match.
No comments:
Post a Comment