[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppBuilderDoubleToBitString
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppBuilderDoubleToBitString
(C++ Builder) Converting a double to a String of bits
The example below shows the code for converting a double to a String. It shows that 0.0 is not equal to -0.0 in a TStringGrid.String doubleToBitString(const double& anyDouble) { static union MyUnion { unsigned int myInts[2]; double myDouble; } myUnion; //assert(sizeof(myUnion.myInts) == sizeof(myUnion.myDouble)); myUnion.myDouble = anyDouble; unsigned int myInt0 = myUnion.myInts[0]; unsigned int myInt1 = myUnion.myInts[1]; String myBitString; for (unsigned int i = 0; i<32; ++i) { if (myInt0%2==0) myBitString = "0" + myBitString; else myBitString = "1" + myBitString; myInt0>>=1; } for (unsigned int i = 0; i<32; ++i) { if (myInt1%2==0) myBitString = "0" + myBitString; else myBitString = "1" + myBitString; myInt1>>=1; } return myBitString; } void __fastcall TForm1::Button1Click(TObject *Sender) { const String bitStringOne = doubleToBitString(1.0); const String bitStringZero = doubleToBitString(0.0); const String bitStringMinusZero = doubleToBitString(-0.0); const String bitStringMinusOne = doubleToBitString(-1.0); for (int y=0; y<64; ++y) { StringGrid1->Cells[0][y+1]=y; StringGrid1->Cells[1][y+1]=bitStringMinusOne[y+1]; //AnsiString starts at index 1 StringGrid1->Cells[2][y+1]=bitStringMinusZero[y+1]; StringGrid1->Cells[3][y+1]=bitStringZero[y+1]; StringGrid1->Cells[4][y+1]=bitStringOne[y+1]; }
- include <assert>
Topic links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
