// File: demo.cxx // Showing details of binary numbers // // CSCI 2270 // Karl Winklmann // Fall 2002 #include #include #include void testIfEqual (unsigned int ui, int si); int main() { system ("type demo.cxx"); // show this program unsigned int ui = 2; int si = 2; // 'int' by itself means 'signed', i.e. // there can be positive and negative values testIfEqual (ui, si); getchar (); // wait for Enter key ui = ui - 7; si = si - 7; testIfEqual (ui, si); return EXIT_SUCCESS; } void testIfEqual (unsigned int ui, int si) { if (ui == si) cout << endl << "Yes, " << ui << " == " << si << endl; else cout << endl << "No, " << ui << " != " << si << endl; // showing the actual bits cout << "unsigned int = "; for (int b = 31; b >= 0; b--) cout << ((ui >> b) & 1); cout << endl; cout << "(signed) int = "; for (int b = 31; b >= 0; b--) cout << ((si >> b) & 1); } ------------------------------------------------------------------ Yes, 2 == 2 unsigned int = 00000000000000000000000000000010 (signed) int = 00000000000000000000000000000010 Yes, 4294967291 == -5 unsigned int = 11111111111111111111111111111011 (signed) int = 11111111111111111111111111111011