Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits.h>
- #include <stdlib.h>
- using namespace std;
- union S {
- int16_t s;
- uint16_t u;
- };
- unsigned int _Not(unsigned int);
- unsigned int _Neg(unsigned int);
- void _toBinary(uint16_t value, char *out) {
- int size = 16;
- for (int i = 0; i < size; i++) {
- out[size - i - 1] = (char) (value % 2 + 48);
- value /= 2;
- }
- out[size] = '\0';
- }
- void Dump(const void *mem, unsigned int n) {
- const char *p = reinterpret_cast< const char *>(mem);
- for (unsigned int i = 0; i < n; i++) {
- cout << hex << int(p[i]) << " ";
- }
- cout << endl;
- }
- int main() {
- // Ex 2
- cout << "Ex 2:" << endl;
- S d = {-34};
- cout << hex << "U Hex d = " << d.u << endl;
- cout << dec << "U Dec d = " << d.u << endl;
- cout << dec << "S Dec d = " << d.s << endl;
- //Ex 3
- cout << endl;
- cout << "Ex 3:" << endl;
- char *b_out = new char[17];
- cout << hex << "Max signed hex = " << UINT16_MAX << endl;
- cout << dec << "Max unsigned dec = " << UINT16_MAX << endl;
- cout << dec << "Max signed dec = " << INT16_MAX << endl;
- _toBinary(INT16_MAX, b_out);
- cout << "Max signed binary = " << b_out << endl;
- _toBinary(UINT16_MAX, b_out);
- cout << "Max unsigned binary = " << b_out << endl;
- cout << hex << "Min signed hex = " << 0 << endl;
- cout << dec << "Min unsigned dec = " << 0 << endl;
- cout << dec << "Min signed dec = " << INT16_MIN << endl;
- _toBinary(INT16_MIN, b_out);
- cout << "Mix signed binary = " << b_out << endl;
- //Ex 4
- unsigned int a = 0;
- unsigned int b = 0;
- cout << endl << "Ex 4:" << endl;
- cout << "Enter a and b:" << endl;
- cin >> a >> b;
- cout << "a & b = " << (a & b) << endl;
- cout << "a | b = " << (b | a) << endl;
- cout << "a ^ b = " << (a ^ b) << endl;
- cout << "not a = " << (_Not(a)) << endl;
- cout << "neg b = " << (_Neg(b)) << endl;
- cout << "a << = " << (a << 1) << endl;
- cout << "b << = " << (b << 1) << endl;
- cout << "a >> = " << (a >> 1) << endl;
- cout << "b >> = " << (b >> 1) << endl;
- //Ex 5
- cout << endl;
- int c = 0;
- int f = 0;
- cout << "Ex 5:" << endl;
- cout << "Enter c and f:" << endl;
- cin >> c >> f;
- cout << "c & f = " << (c & f) << endl;
- cout << "c | f = " << (c | f) << endl;
- cout << "c ^ f = " << (c ^ f) << endl;
- cout << "not c = " << (int) _Not(c) << endl;
- cout << "not f = " << (int) _Not(f) << endl;
- cout << "neg c = " << (int) _Neg(c) << endl;
- cout << "neg f = " << (int) _Neg(f) << endl;
- cout << "c << = " << (c << 1) << endl;
- cout << "f << = " << (f << 1) << endl;
- cout << "c >> = " << (c >> 1) << endl;
- cout << "f >> = " << (f >> 1) << endl;
- //Ex 6
- cout << endl;
- cout << "Ex 6" << endl;
- cout << "Char to int: " << endl;
- char ac[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
- for (int i = 0; i < 10; i++) {
- cout << (ac[i] & 0b00001111) << " | ";
- }
- cout<<endl;
- int an[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- for (int i = 0; i < 10; i++) {
- int tmp = (an[i] | 0b00110000);
- cout << "Code: " << tmp << "; Char: " << ((char) tmp) << endl;
- }
- cout << endl;
- cout << "Int to char(ascii): " << endl;
- char aC[] = {'a', 'b', 'v', 'G', 'M', 'N', 'Z', 'p', 'k', 'l'};
- for (int i = 0; i < 10; i++) {
- cout << "Before: " << aC[i] << "; After: " << (char) (aC[i] ^ 0b00100000) << endl;
- }
- //ex 8
- cout << endl << "Ex 8 " << endl;
- union Words {
- int32_t i;
- char c[4];
- } words;
- words.i = 0x12345678;
- cout << (((unsigned) words.c[3] << 24) + ((unsigned) words.c[2] << 16) + ((unsigned) words.c[1] << 8) +
- (unsigned) words.c[0]) << endl;
- cout << words.i << endl;
- char *a_e = "abcd";
- char *a_r = "абвг";
- wchar_t *a_L = L"abcd";
- wchar_t *a_r_L = L"абвг";
- Dump((void *) a_e, sizeof(char) * 5);
- Dump((void *) a_r, sizeof(char) * 5);
- Dump((void *) a_L, sizeof(wchar_t) * 5);
- Dump((void *) a_r_L, sizeof(wchar_t) * 5);
- //ex9
- cout << endl << "Ex 9" << endl;
- cout << hex << INT_MAX << endl;
- cout << hex << INT_MAX + 1 << endl;
- cout << hex << (unsigned int) UINT_MAX << endl;
- cout << hex << (((unsigned int) UINT_MAX) + 1) << endl;
- // ex 10
- cout << endl << "Ex 10" << endl;
- cout << "Char: " << sizeof(char) << endl <<
- "Bool: " << sizeof(bool) << endl <<
- "Wchar_t: " << sizeof(wchar_t) << endl <<
- "Short: " << sizeof(short) << endl <<
- "Int: " << sizeof(int) << endl <<
- "Long: " << sizeof(long) << endl <<
- "Long long: " << sizeof(long long) << endl <<
- "Float: " << sizeof(float) << endl <<
- "Double: " << sizeof(double) << endl <<
- "Long double: " << sizeof(long double) << endl <<
- "Size_t: " << sizeof(size_t) << endl <<
- "Ptrdiff_t: " << sizeof(ptrdiff_t) << endl <<
- "Void *: " << sizeof(void *) << endl;
- }
- unsigned int _Not(unsigned int value) {
- int size = sizeof(unsigned int) * 8;
- int mask = 1;
- for (int i = 0; i < size; i++) {
- value ^= mask;
- mask = mask << 1;
- }
- return value;
- }
- unsigned int _Neg(unsigned int value) {
- unsigned int Result = 0;
- bool Carret = false;
- int size = sizeof(unsigned int) * 8;
- int mask = 1;
- for (int i = 0; i < size; i++) {
- if (Carret) {
- if (value % 2 == 0)
- Result += mask << i;
- } else {
- if (value % 2 == 1) {
- Carret = true;
- Result += mask << i;
- }
- }
- value /= 2;
- }
- return Result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement