Ray's playground

 

The C in C++(Chapter 3 of Thinking in C++)

code
 1 #include <iostream>
 2 #include <cstdlib>
 3 using namespace std;
 4 
 5 
 6 void PrintBinary(const unsigned char val)
 7 {
 8     for(int i=7; i>=0; i--)
 9     {
10         if(val & (1 << i))
11         {
12             cout << "1";
13         }
14         else
15         {
16             cout << "0";
17         }
18     }
19 }
20 
21 int main()
22 {
23     double d = atof("12.3");
24     unsigned char* p = reinterpret_cast<unsigned char*>(&d);
25     for(int i=sizeof(double); i>0; i-=2)
26     {
27         PrintBinary(p[i-1]);
28         PrintBinary(p[i]);
29     }
30 
31     cin.get();
32 }

 

posted on 2010-10-27 21:04  Ray Z  阅读(226)  评论(0编辑  收藏  举报

导航