寻找double-∞C++

来源:互联网 发布:knockout.js easyui 编辑:程序博客网 时间:2024/05/16 07:10

   写上一个文章的时候,发觉自己不会构造double的-∞,于是,决心睾一睾.直到昨天,睾出来了.过程中,感谢回帖指点的朋友,还有昨天的我.

   过程中,写了自己的第一个复制内存的函数.以前都没有写过,这次写,希望下次能写得更好.

  回到主题,这个问题,体现出了,其实我还是没有掌握little endian.直到这个过程结束,才算掌握了.

  不多说了,写完了回去继续学习.坚持吧,虽然烦心事很多很多,但,该做的,还是要做好.不然,日子是白过.

  贴.

// testingDouble.cpp -- 2011-07-10-20.08//Complete at 2011-07-11-23.33//Thanks for ardent friends on csdn.net!#include "stdafx.h"#include <iostream>//Check out if these two memory blocks is overlaped.bool isOverlapped (const char * const pSource, const char * const pTarget, const int size) ;char * copyMemory (const char * const pSource, const int size, const char * pTarget) ;void printfBitMode (const char * const pValue, const int size) ;int _tmain(int argc, _TCHAR* argv[]){union{long long int llValue ;double dblValue ;} example ;//Little endian. //example.value should be 1 1111111111 0000000000000000000000000000000000000000000000000000example.llValue = 0xFFF0000000000000 ;int size = sizeof (example.dblValue) ;char * pTarget = new char[size] ;pTarget = copyMemory(reinterpret_cast<char *> (&example.dblValue), size, pTarget) ;printfBitMode(pTarget, size) ;std ::cout << example.dblValue << std ::endl ;delete []pTarget ;return 0 ;}bool isOverlapped (const char * const pSource, const char * const pTarget, const int size){//Case 1://pSource:___________//pSource:____...if (pTarget == pSource)return true ;//Case 2://pSource:___________//pTarget: ______...else if (pSource < pTarget && pTarget <= pSource + size - 1)return true ;//Case 3://pSource:___________//pTarget:____...else if (pTarget < pSource && pTarget + size - 1 >= pSource)return true ;elsereturn false ;}char * copyMemory (const char * const pSource, const int size, const char * pTarget){if (NULL == pSource || NULL == pTarget || size <= 0)return NULL ;if (isOverlapped(pSource, pTarget, size))return NULL ;char * newPSource = const_cast <char *> (pSource) ;char * newPTarget = const_cast <char *> (pTarget) ;for (int i = 0; i < size; ++i)*(newPTarget + i) = *(newPSource + i) ;return const_cast<char *> (pTarget) ;}void printfBitMode (const char * const pValue, const int size){using std ::cout ;using std ::endl ;for (int i = 0; i < size; ++i){if (*(pValue + i) & 0x80)cout << '1' ;elsecout << '0' ;if (*(pValue + i) & 0x40)cout << '1' ;elsecout << '0' ;if (*(pValue + i) & 0x20)cout << '1' ;elsecout << '0' ;if (*(pValue + i) & 0x10)cout << '1' ;elsecout << '0' ;if (*(pValue + i) & 0x08)cout << '1' ;elsecout << '0' ;if (*(pValue + i) & 0x04)cout << '1' ;elsecout << '0' ;if (*(pValue + i) & 0x02)cout << '1' ;elsecout << '0' ;if (*(pValue + i) & 0x01)cout << '1' ;elsecout << '0' ;cout << ' ' ;}cout << endl ;}
原创粉丝点击