home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_01 / allison / tbits.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-07  |  2.6 KB  |  83 lines

  1. // tbits.cpp
  2. #include <iostream.h>
  3. #include <iomanip.h>
  4. #include <stddef.h>
  5. #include <limits.h>
  6. #include "bits.h"
  7.  
  8. main()
  9. {
  10.     const size_t SIZE = CHAR_BIT * sizeof(unsigned long);
  11.     unsigned long n = 0x12345678;
  12.     bits<SIZE> x(n), y(string("10110")), z(x);
  13.  
  14.     cout << "Initial x: " << x << endl;
  15.     cout << "Initial y: " << y << endl;
  16.     cout << "Initial z: " << z << endl;
  17.     cout << "Enter new z: ";
  18.     cin >> z;
  19.     cout << "New z: " << z << endl;
  20.     cout << "z == " << z.to_ulong() << endl;
  21.     cout << "y == " << y.to_ushort() << endl;
  22.     cout << "x == " << x.to_ulong() << endl;
  23.  
  24.     cout << "x: " << x << " (" << x.count()
  25.          << " bits set)" << endl;
  26.     cout << "x == 0x12345678L? " << (x == 0x12345678L) << endl;
  27.     cout << "x: " << x << endl;
  28.     cout << "x: " << hex << setfill('0')
  29.          << setw(sizeof(unsigned long)*2)
  30.          << x.to_ulong() << dec << endl;
  31.     cout << "x <<= 6 == " << (x <<= 6) << endl;
  32.     cout << "x >>= 6 == " << (x >>= 6) << endl;
  33.     cout << "85 == " << bits<SIZE>(85) << endl;
  34.     cout << "x ^ 85 == " << (x ^ 85) << endl;
  35.     cout << "x & 85 == " << (x & 85) << endl;
  36.     cout << "85 & x === " << (85 & x) << endl;
  37.     cout << "~x == " << (~x) << " == "
  38.          << (~x).to_ulong() << endl;
  39.  
  40.     y = 0x55555550L;
  41.     cout << "y: " << y << " (" << y.count()
  42.          << " bits set)" << endl;
  43.     cout << "y[0]: " << hex << setfill('0')
  44.          << setw(sizeof(unsigned long)*2)
  45.          << y.to_ulong() << dec << endl;
  46.     cout << "x & y == " << (x & y) << endl;
  47.     cout << "x | y == " << (x | y) << endl;
  48.     cout << "x ^ y == " << (x ^ y) << endl;
  49.     cout << "x != y? " << (x != y) << endl;
  50.     return 0;
  51. }
  52.  
  53. /* Sample Execution:
  54. Initial x: 00010010001101000101011001111000
  55. Initial y: 00000000000000000000000000010110
  56. Initial z: 00010010001101000101011001111000
  57.  
  58. Enter new z: 101001000100001000001
  59. New z: 00000000000101001000100001000001
  60. z == 1345601
  61. y == 22
  62. x == 305419896
  63. x: 00010010001101000101011001111000 (13 bits set)
  64. x == 0x12345678L? 1
  65. x: 00010010001101000101011001111000
  66. x: 12345678
  67. x <<= 6 == 10001101000101011001111000000000
  68. x >>= 6 == 00000010001101000101011001111000
  69. 85 == 00000000000000000000000001010101
  70. x ^ 85 == 00000010001101000101011000101101
  71. x & 85 == 00000000000000000000000001010000
  72. 85 & x === 00000000000000000000000001010000
  73. ~x == 11111101110010111010100110000111 == 4257982855
  74. y: 01010101010101010101010101010000 (14 bits set)
  75. y[0]: 55555550
  76. x & y == 00000000000101000101010001010000
  77. x | y == 01010111011101010101011101111000
  78. x ^ y == 01010111011000010000001100101000
  79. x != y? 1
  80.  
  81. // End of File
  82.  
  83.