アプリ版:「スタンプのみでお礼する」機能のリリースについて

#include <iostream>

using namespace std;

union bits{
bits(double n);
void show_bits();
double d;
unsigned char c[sizeof(double)];
};

bits::bits(double n)
{
d = n;
}

void bits::show_bits()
{
int i, j;

for(j = sizeof(double)-1; j>=0; j--){
cout << "バイト単位のビットパターン" << j << ":";
for(i=128; i ; i >>=1)
if(i &c[j]) cout <<"1";
else cout << "0";
cout <<endl;
}
}

int main ()
{
bits ob(1991.829);
ob.show_bits();

return 0;
}

このコードが何をしているのか解説していただけないでしょうか?

A 回答 (1件)

bitsは倍精度浮動小数点数(d)と文字配列(c)の共用体で、倍精度浮動小数点数を設定(コンストラクタ)してそのビット表現を表示(show_bits関数)できる。



C++の共用体なのでコンストラクタや関数をメンバに持てる。
その例だろう。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!