Advertisement
yenqwerty

Untitled

Sep 28th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. union S {
  8.     int16_t s;
  9.     uint16_t u;
  10. };
  11.  
  12.  
  13. unsigned int _Not(unsigned int);
  14.  
  15. unsigned int _Neg(unsigned int);
  16.  
  17. void _toBinary(uint16_t value, char *out) {
  18.     int size = 16;
  19.     for (int i = 0; i < size; i++) {
  20.         out[size - i - 1] = (char) (value % 2 + 48);
  21.         value /= 2;
  22.     }
  23.     out[size] = '\0';
  24. }
  25.  
  26.  
  27. void Dump(const void *mem, unsigned int n) {
  28.     const char *p = reinterpret_cast< const char *>(mem);
  29.     for (unsigned int i = 0; i < n; i++) {
  30.         cout << hex << int(p[i]) << " ";
  31.     }
  32.     cout << endl;
  33. }
  34.  
  35. int main() {
  36.     // Ex 2
  37.     cout << "Ex 2:" << endl;
  38.     S d = {-34};
  39.  
  40.     cout << hex << "U Hex d = " << d.u << endl;
  41.     cout << dec << "U Dec d = " << d.u << endl;
  42.     cout << dec << "S Dec d = " << d.s << endl;
  43.  
  44.     //Ex 3
  45.  
  46.     cout << endl;
  47.     cout << "Ex 3:" << endl;
  48.     char *b_out = new char[17];
  49.  
  50.     cout << hex << "Max signed hex = " << UINT16_MAX << endl;
  51.     cout << dec << "Max unsigned dec = " << UINT16_MAX << endl;
  52.     cout << dec << "Max signed dec = " << INT16_MAX << endl;
  53.     _toBinary(INT16_MAX, b_out);
  54.     cout << "Max signed binary = " << b_out << endl;
  55.     _toBinary(UINT16_MAX, b_out);
  56.     cout << "Max unsigned binary = " << b_out << endl;
  57.  
  58.     cout << hex << "Min signed hex = " << 0 << endl;
  59.     cout << dec << "Min unsigned dec = " << 0 << endl;
  60.     cout << dec << "Min signed dec = " << INT16_MIN << endl;
  61.     _toBinary(INT16_MIN, b_out);
  62.     cout << "Mix signed binary = " << b_out << endl;
  63.  
  64.  
  65.     //Ex 4
  66.     unsigned int a = 0;
  67.     unsigned int b = 0;
  68.     cout << endl << "Ex 4:" << endl;
  69.     cout << "Enter a and b:" << endl;
  70.     cin >> a >> b;
  71.  
  72.     cout << "a & b = " << (a & b) << endl;
  73.     cout << "a | b = " << (b | a) << endl;
  74.     cout << "a ^ b = " << (a ^ b) << endl;
  75.     cout << "not a = " << (_Not(a)) << endl;
  76.     cout << "neg b = " << (_Neg(b)) << endl;
  77.     cout << "a << = " << (a << 1) << endl;
  78.     cout << "b << = " << (b << 1) << endl;
  79.     cout << "a >> = " << (a >> 1) << endl;
  80.     cout << "b >> = " << (b >> 1) << endl;
  81.  
  82.     //Ex 5
  83.     cout << endl;
  84.     int c = 0;
  85.     int f = 0;
  86.  
  87.     cout << "Ex 5:" << endl;
  88.     cout << "Enter c and f:" << endl;
  89.     cin >> c >> f;
  90.  
  91.     cout << "c & f = " << (c & f) << endl;
  92.     cout << "c | f = " << (c | f) << endl;
  93.     cout << "c ^ f = " << (c ^ f) << endl;
  94.     cout << "not c = " << (int) _Not(c) << endl;
  95.     cout << "not f = " << (int) _Not(f) << endl;
  96.     cout << "neg c = " << (int) _Neg(c) << endl;
  97.     cout << "neg f = " << (int) _Neg(f) << endl;
  98.     cout << "c << = " << (c << 1) << endl;
  99.     cout << "f << = " << (f << 1) << endl;
  100.     cout << "c >> = " << (c >> 1) << endl;
  101.     cout << "f >> = " << (f >> 1) << endl;
  102.  
  103.  
  104.  
  105.     //Ex 6
  106.     cout << endl;
  107.     cout << "Ex 6" << endl;
  108.     cout << "Char to int: " << endl;
  109.     char ac[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  110.  
  111.     for (int i = 0; i < 10; i++) {
  112.         cout << (ac[i] & 0b00001111) << "  |  ";
  113.     }
  114.     cout<<endl;
  115.  
  116.     int an[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  117.  
  118.     for (int i = 0; i < 10; i++) {
  119.         int tmp = (an[i] | 0b00110000);
  120.         cout << "Code: " << tmp << "; Char: " << ((char) tmp) << endl;
  121.     }
  122.  
  123.     cout << endl;
  124.     cout << "Int to char(ascii): " << endl;
  125.  
  126.     char aC[] = {'a', 'b', 'v', 'G', 'M', 'N', 'Z', 'p', 'k', 'l'};
  127.  
  128.     for (int i = 0; i < 10; i++) {
  129.         cout << "Before: " << aC[i] << "; After: " << (char) (aC[i] ^ 0b00100000) << endl;
  130.     }
  131.  
  132.     //ex 8
  133.     cout << endl << "Ex 8 " << endl;
  134.     union Words {
  135.         int32_t i;
  136.         char c[4];
  137.     } words;
  138.  
  139.     words.i = 0x12345678;
  140.  
  141.     cout << (((unsigned) words.c[3] << 24) + ((unsigned) words.c[2] << 16) + ((unsigned) words.c[1] << 8) +
  142.              (unsigned) words.c[0]) << endl;
  143.     cout << words.i << endl;
  144.  
  145.  
  146.     char *a_e = "abcd";
  147.     char *a_r = "абвг";
  148.     wchar_t *a_L = L"abcd";
  149.     wchar_t *a_r_L = L"абвг";
  150.  
  151.     Dump((void *) a_e, sizeof(char) * 5);
  152.     Dump((void *) a_r, sizeof(char) * 5);
  153.     Dump((void *) a_L, sizeof(wchar_t) * 5);
  154.     Dump((void *) a_r_L, sizeof(wchar_t) * 5);
  155.  
  156.  
  157.     //ex9
  158.  
  159.     cout << endl << "Ex 9" << endl;
  160.     cout << hex << INT_MAX << endl;
  161.     cout << hex << INT_MAX + 1 << endl;
  162.  
  163.     cout << hex << (unsigned int) UINT_MAX << endl;
  164.     cout << hex << (((unsigned int) UINT_MAX) + 1) << endl;
  165.  
  166.     // ex 10
  167.  
  168.     cout << endl << "Ex 10" << endl;
  169.     cout << "Char: " << sizeof(char) << endl <<
  170.          "Bool: " << sizeof(bool) << endl <<
  171.          "Wchar_t: " << sizeof(wchar_t) << endl <<
  172.          "Short: " << sizeof(short) << endl <<
  173.          "Int: " << sizeof(int) << endl <<
  174.          "Long: " << sizeof(long) << endl <<
  175.          "Long long: " << sizeof(long long) << endl <<
  176.          "Float: " << sizeof(float) << endl <<
  177.          "Double: " << sizeof(double) << endl <<
  178.          "Long double: " << sizeof(long double) << endl <<
  179.          "Size_t: " << sizeof(size_t) << endl <<
  180.          "Ptrdiff_t: " << sizeof(ptrdiff_t) << endl <<
  181.          "Void *: " << sizeof(void *) << endl;
  182.  
  183. }
  184.  
  185.  
  186. unsigned int _Not(unsigned int value) {
  187.     int size = sizeof(unsigned int) * 8;
  188.     int mask = 1;
  189.     for (int i = 0; i < size; i++) {
  190.         value ^= mask;
  191.         mask = mask << 1;
  192.     }
  193.     return value;
  194. }
  195.  
  196. unsigned int _Neg(unsigned int value) {
  197.     unsigned int Result = 0;
  198.     bool Carret = false;
  199.  
  200.     int size = sizeof(unsigned int) * 8;
  201.     int mask = 1;
  202.     for (int i = 0; i < size; i++) {
  203.         if (Carret) {
  204.             if (value % 2 == 0)
  205.                 Result += mask << i;
  206.         } else {
  207.             if (value % 2 == 1) {
  208.                 Carret = true;
  209.                 Result += mask << i;
  210.             }
  211.         }
  212.         value /= 2;
  213.     }
  214.     return Result;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement