C++のプログラムがわかりません

このQ&Aのポイント
  • C++のプログラムがわかりません
  • この記事では、C++のプログラムの理解に関する質問について解説します。
  • 特に、バイナリビットパターンとバイト単位での表示に関して詳しく説明します。
回答を見る
  • ベストアンサー

C++のプログラムがわかりません。

C++のプログラムがわかりません。 #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--){ for(i=128;i;i>>=1) if(i&c[j]) cout<<"1" else cout<<"0"; cout<<"\n"; } } int main() { bits ob(1991.829); ob.show_bits(); return 0; } このプログラムはdouble値に含まれるバイナリビットパターンをバイト単位で表示しているらしいのですが、 バイト単位のビットパターン 7: 01000000 バイト単位のビットパターン 6: 10011111 バイト単位のビットパターン 5: 00011111 バイト単位のビットパターン 4: 01010000 バイト単位のビットパターン 3: 11100101 バイト単位のビットパターン 2: 01100000 バイト単位のビットパターン 1: 01000001 バイト単位のビットパターン 0: 10001001 にどうしてなるかわかりません。 1991.829をどのように区切ればこのような結果になるのでしょうか? C言語は多少やっていますが、バイナリやビットパターンなどの理解が浅いのでその辺りをわかりやすくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • SortaNerd
  • ベストアンサー率43% (1185/2748)
回答No.1

どうやら「浮動小数点数」をご存じないようですね。 まずこちらをご覧ください。 http://ja.wikipedia.org/wiki/%E6%B5%AE%E5%8B%95%E5%B0%8F%E6%95%B0%E7%82%B9%E6%95%B0 1991.829の場合で言うと、 まずこの数値を2進数に直しますと、 11111000111.110101000011100101011000000100000110001001… となります。中途半端な数なので循環小数になってしまっています。(悪問ですね。) 続いて、小数点の位置を見ます。 1 . 1111000111110101000011100101011000000100000110001001… ここを基準として、10桁ずれたところにあります。この10は後で使うので覚えておきます。 次に、二進数には数字が1と0しかないので、最上位の数字は0でないので常に1です。 常に同じなら書く必要はないのでとってしまいます。 1111000111110101000011100101011000000100000110001001 一方、先ほどの10ですが、これを指数と呼びます。 説明は面倒なので割愛しますが、double型ではこれに1023を足した数値を記録することになっています。 10+1023(10進数) =00000001010+01111111111=10000001001(二進数) です。 最後に符号をつけます。プラスなら0、マイナスなら1です。 合わせて、 0 10000001001 1111000111110101000011100101011000000100000110001001 となり、8ビットで区切れば、 01000000 10011111 00011111 01010000 11100101 01100000 01000001 10001001 です。

tkdtoto7
質問者

お礼

お礼が遅れましたが、ご回答ありがとうございました。 すごくわかりやすい説明で納得できました。 (double型では1023を足すってのは多分約束事ですよね?)

関連するQ&A

  • 共用体のサンプルコード : 内容がわかりません

    #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; } このコードが何をしているのか解説していただけないでしょうか?

  • 下記、プログラム内の「char *」の役割

    C++初心者です。 縦長になってしまいますが、 『 #include <iostream.h> void show(int); void show(double); void show(char *);   ←左記の記述の使い方 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char *z) { cout << z << endl; } 』 のプログラムにおいて、「char *」の使い方がいまいち理解できません。 上記プログラムですとエラーが表示されないのですが、下記のプログラムだとエラーが発生します。 『 #include <iostream.h> void show(int); void show(double); void show(char);   //←---------上記と違う行 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char z) {  //←---------上記と違う行 cout << z << endl; } 』 なぜ、ポインタ(*)を付けないといけないのか分かりやすく教えていただけましょうか。

  • C++のプログラムが走りません。

    <<条件は以下です。>> データメンバ x:xの座標(ただし 0-1000とする) y:yの座標(ただし 0-1000とする) z:zの座標(ただし 0-1000とする) メンバ関数 void setX(int a): xの座標を設計する(範囲外の値は、error messageを出力する) void setY(int b): Yの座標を設計する(範囲外の値は、error messageを出力する) void setZ(int c) Zの座標を設計する(範囲外の値は、error messageを出力する) int getX(): Xの座標値を得る int getY(): Yの座標値を得る int getZ(): Zの座標値を得る << 不明な点は、 if文で、どう戻るかがわからないことと、 最後の結果表示の部分です。 以下が、作成したプログラムです。ネットで検索して類似しているプログラムを参考にしたんですが。 よろしくお願いします。>> #include <iostream> using namespace std; class Zahyou{ private: int getX();//Xの座標値を得る int getY();//Yの座標値を得る int getZ();//Zの座標値を得る int a,b,c; public: void setX(int a);//Xの座標を設計する void setY(int b);//Yの座標を設計する void setZ(int c);//Zの座標を設計する void show(); }; void Zahyou::show(){ cout << "The result is: (" << a << "," << b << "," << c << ")"; } void Zahyou::setX(int a){ cout << " Please Input X " << "\n "; cin >> a ; if(a < 0 && a > 1000){ cout << "The value of X is a mistake, Please input X again! " << " \n "; } } void Zahyou::setY(int b){ cout << "Please Input Y " << "\n "; cin >> b ; if(b < 0 && b > 1000){ cout << "The value of y is a mistake, Please input X again! " << " \n "; cin >> b ; } } void Zahyou::setZ(int c){ cout << " Please Input Z " << "\n "; cin >> c ; if(c < 0 && c > 1000){ cout << "The value of Z is a mistake, Please input X again! " << " \n "; cin >> c ; } } int main(){ Zahyou zahyou1; zahyou1.getX(); zahyou1.getY(); zahyou1.getZ(); zahyou1.show(); return 0; }

  • C++で分からないプログラムがあるんですが

    #include <iostream> #include <cmath> using namespace std; int main() { static const int N = 2; double va[N]={3,-4}; double vb[N]={4,3}; double a,b; double p; for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) { } } cout << "va + vb = (" ; for (int i = 0; i < N; ++i) { cout << va[i] + vb[i]; if (i < N - 1) { cout << ", "; } } cout << ")" << '\n'; cout << "va - vb = (" ; for (int i = 0; i < N; ++i) { cout << va[i] - vb[i]; if (i < N - 1) { cout << ", "; } } cout << ")" << '\n'; p = 0; for (int i = 0; i < N; ++i) { p += va[i] * vb[i]; } cout << "va・vb = " << p << '\n'; a = 0; for (int i = 0; i < N; ++i) { a += va[i] * va[i]; } a = sqrt(a); b = 0; for (int i = 0; i < N; ++i) { b += vb[i] * vb[i]; } b = sqrt(b); if (a * b != 0) { cout << "cosθ = " << p / (a * b) << '\n'; } return 0; } これで、ベクトルの加減とベクトルの内積とcosθが出るんですが、2つのベクトルを適当に初期化しないといけないんですが、初期化ってこれで初期化ってできてますか?

  • C++ ソートのやり方

    僕が作ったプログラムで、これはバブルソートなのかわからないので教えてください。 また、ほかのソートの仕方も教えてください。 よろしくお願いします。 汎用関数を使っているのでわかりにくいかもしれないですがお願いします。 #include <iostream> using namespace std; template <class X>void Sort(X *data, int size) { X temp; for (int i = 0; i < size; i++){ for (int j = i + 1; j < size; j++){ if (data[i]>data[j]){ temp = data[i]; data[i] = data[j]; data[j] = temp; } } } } int main() { int i[10]{1, 4, 3, 5, 2, 10, 2, 7, 6, 8}; char c[10]{'c', 'b', 'z', 'a', 'x', 'y', 'j', 'n', 'm', 'r'}; Sort(c, 10); Sort(i, 10); for (int j = 0; j < 10; j++){ cout << i[j] << ' '; } cout << endl; for (int j = 0; j < 10; j++){ cout << c[j] << ' '; } cout << endl; getchar(); return 0; }

  • Cのプログラムがどうしても動きません

    Cを勉強中なのですが、以下のプログラムがうまくいきません。 (studentは構造体で定義した型です。) iが0でない5の倍数の時にreallocでメモリを増やそうと思ったのですが、 「21行目」(reallocの行)で記述エラーを発見しました。 「lvalue」を付け忘れています。 と表示されます。 どこが間違っているのでしょうか?教えてくださいm(_ _)m #include<stdio.h> typedef struct{ char name[20]; int year; char sex[6]; }student; void read_data(int,student*); void write_data(int,student*); int main(void){ student data[5]; int i=0,j=0; do{ read_data(i,data); i++; if(i%5==0 && i!=0){ data=realloc(data,(sizeof(student))*(i+5)); } }while(data[i-1].year!=-1); } for(j=0;j<i-1;j++){ write_data(j,data); } free(heap); return 0; } void read_data(int i,student *data){ printf("%d人目\n",i); printf("名前?\n",i); scanf("%s",(data[i].name)); printf("年齢?\n",i); scanf("%d",&(data[i].year)); printf("性別?\n",i); scanf("%s",(data[i].sex)); return; } void write_data(int j,student *data){ printf("%d人目\t",j+1); printf("名前:%s\n",data[j].name); printf("年:%d\n",data[j].year); printf("性:%s\n",data[j].sex); return; }

  • C++超初心者質問

    Visual C++ 2008 Express Edition Ver9.0.30729.1 SP を利用し、以下のプログラムを実行するための手順を教えてください。 Visual C++ 6.0では、ファイル>新規作成→ファイルTAB>C++ソースファイルのように、上記の場合はどのように進んだらよいのでしょう? ファイル>新規作成>プロジェクト→Win32 コンソール アプリケーション??? とすると、↓のエラーが出てしまいます。尚、VC2008をインストールしてから何も触っていません。 http://okwave.jp/qa4665322.html ソース #include<iostream> using namespace std; class coord{ int x,y; public: coord(){x=0;y=0;} coord(int i,int j){x=i,y=j;} void get_xy(int &i,int &j){i=x;j=y;} friend coord operator+(coord ob1,int i); friend coord operator+(int i,coord ob1); }; coord operator+(coord ob1,int i){ coord temp; temp.x=ob1.x+i; temp.y=ob1.y+i; return temp; } coord operator+(int i,coord ob1){ coord temp; temp.x=ob1.x+i; temp.y=ob1.y+i; return temp; } int main(){ char c[3]; // 画面固定のため coord o1(10,10); int x,y; o1=o1+10;; o1.get_xy(x,y); cout<<"(o1+10)X:"<<x<<",Y:"<<y<<"\n"; o1=99+o1;; o1.get_xy(x,y); cout<<"(99+o1)X:"<<x<<",Y:"<<y<<"\n"; cout<<"\nエンターで抜けます"<<endl; // 画面固定のため gets(c); return 0; }

  • !!の意味がわかりません

    以下のような引数をビット表示するテンプレート関数があったのですが、 !!( val & 1 ) の意味がわかりません。!で反転して!でまた反転したら 元通りになるだけだと思うのですが、 どういう意図でそうなっているのでしょうか? template <typename T> void print_bits ( T val ) { unsigned int n_bits = sizeof ( val ) * CHAR_BIT; for ( unsigned i = 0; i < n_bits; ++i ) { std::cout<< !!( val & 1 ); val >>= 1; } } 強制的に型変換を行っているような気もするのですが、 よく理解できません。 よろしくお願いいたします。

  • C++についての質問です

    プログラミング初心者です 以下の通りに正方行列の積を求めるプログラムを作成したのですが、うまくいきません。 #include<stdio.h> #define DTM 20 void InputMatrix(double[][DTM], int, char); void PrintMatrix(double[][DTM], int, char); void MatrixMulti(double[][DTM], double[][DTM], double[][DTM], int); int main(void) { double matrixA[DTM][DTM]; double matrixB[DTM][DTM]; double matrixC[DTM][DTM]; int n; printf("正方行列の積を求めるプログラムです\n"); printf("正方行列の次元を入れてください(<=20):"); scanf_s("%d", &n); InputMatrix(matrixA, n, 'A'); InputMatrix(matrixB, n, 'B'); MatrixMulti(matrixA, matrixB, matrixC, n); printf("\n行列 C =A×B\n"); PrintMatrix(matrixC, n, 'C'); return 0; } void InputMatrix(double a[][DTM], int n, char ch) { int i, j; printf("行列 %cの入力\n", ch); for (i = 0; i < n;i++) { for (j = 0;j < n;j++) { printf("%c[%d][%d] =", ch, i + 1, j + 1); scanf_s("%lf", &a[i][j]); } } } void PrintMatrix(double a[][DTM], int n, char ch) { int i, j; printf("行列 %c の出力\n", ch); for (i = 0;i < n;i++) { for (j = 0;j < n;j++) { printf("%5.2f\t", a[i][j]); } printf("\n"); } } void MatrixMulti(double a[][DTM], double b[][DTM], double c[][DTM], int n) { int i, j, k; for (i = 0;i < n;i++) { for (j = 0;j < n;j++) { c[i][j] = 0; for (k = 0;k < n;k++) { c[i][j] =a[i][k] * b[k][j]; } printf("%5.2f\t",c[i][j]); } printf("\n"); } }

  • C++の質問です

    C++の質問です。 c++をコンパイルしたとき、 出力されるのが次のように3つ同時にされるようにしたいのですが、 1、 名前 ○○○○ 番号 ○○○○ x= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}の平均 2、 x= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}の分散 y= {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}の標準偏差 3、 x={1,2,3,4,5,6,7,8,9,10}と y={10,9,8,7,6,5,4,3,2,1}の相関係数 下記のソースをどのように変えればいいでしょうか。 ちなみに、C言語ではなくC++なので C++形式でお願いします。 どうかお願いします。 #include <iostream> #include <cmath> using namespace std; double Mean(int *a, int size); double StandardDeviation(int *a, int size); double CoefficientOfCorrelation(int *a, int *b, int sizeA, int sizeB); int main() { int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int y[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; cout << " 標準偏差 : " << StandardDeviation(x, sizeof(x) / sizeof(int)) << endl << " 相関係数 : " << CoefficientOfCorrelation(x, y, sizeof(x) / sizeof(int), sizeof(y) / sizeof(int)) << endl; return 0; } double Mean(int *a, int size) { if (size <= 0) { return -1.0; } double d = 0.0; for (int i = 0; i < size; i++) { d += *(a + i); } return d / size; } double StandardDeviation(int *a, int size) { if (size <= 0) { return -1.0; } double mean = Mean(a, size); double d = 0.0; for (int i = 0; i < size; i++) { d += pow(*(a + i) - mean, 2); } return sqrt(d / size); } double CoefficientOfCorrelation(int *a, int *b, int sizeA, int sizeB) { if (sizeA > 0 && sizeB > 0 && sizeA != sizeB) { return -1.0; } double meanX = Mean(a, sizeA); double meanY = Mean(b, sizeB); double sdX = StandardDeviation(a, sizeA); double sdY = StandardDeviation(b, sizeB); double coeff = 0.0; for (int i = 0; i < sizeA; i++) { coeff += (*(a + i) - meanX) * (*(b + i) - meanY); } return (coeff / (sizeA * sdX * sdY)); }

専門家に質問してみよう