Advertisement
avr39ripe

BV024bitOperations

Feb 3rd, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. char* encodecode(char* str, char key)
  4. {
  5.     char* origStr{ str };
  6.  
  7.     while (*str)
  8.     {
  9.         *str ^= key;
  10.         ++str;
  11.     }
  12.  
  13.     return origStr;
  14. }
  15.  
  16. int main()
  17. {
  18.     // 0xffff
  19.     // hex system 0..9, a - 10 b - 11 c -12 d -13 e -14 f -15
  20.     // ff -> f * 16 ^ 0 + f * 16 ^ 1 = 15 + 240 = 255
  21.     //uint8_t byte{0x1a}; // 16^0 * a + 16^1 * 1 = 1 * 10 + 16 * 1 = 10 + 16 = 26
  22.     //uint8_t byte{ 0b1111 }; // (1 * 2 ^ 0) + (1 * 2 ^ 1) + (1 * 2 ^ 2) + (1 * 2 ^ 3) = 1 * 1 + 1 * 2 + 1 * 4 + 1 * 8 = 15
  23.  
  24.     // ~ - bitwise NOT - unary op
  25.     // << - bitwise shift left - binary op
  26.     // >> - bitwise shift right - binary op
  27.     // | - bitwise OR - binary op
  28.     // & - bitwise AND - binary op
  29.     // ^ - bitwise XOR - binary op
  30.  
  31.     // <<= a = a << 1 -> a <<= 1
  32.     // >>= a = a >> 4 -> a >>= 4
  33.     // |= a = a | b -> a |= b
  34.     // &= a = a & 0b0101 -> a &= 0b0101
  35.     // ^= a = a ^ 42 -> a ^= 42
  36.  
  37.     uint8_t byte{ 0b1010 }; // 0b00000000
  38.     uint8_t mask{ 0b0110 };
  39.     // 0b1010
  40.     // 0b0110
  41.  
  42.     // 0b1100
  43.     // 0b0110
  44.  
  45.     // 0b1010
  46.     // 0b1010
  47.  
  48.     uint8_t res;
  49.     res = byte ^ mask;
  50.  
  51.     uint16_t word{ 0 };
  52.     uint16_t wRes;
  53.     wRes = ~word;
  54.     // ~
  55.    
  56.  
  57.     //for (int i{ 0 }; i < 16; ++i)
  58.     //{
  59.     //  std::cout << +(uint16_t)(0b1000000000000000 >> i) << '\n';
  60.     //}
  61.     char message[] = "Hello crypted world :)";
  62.     char symb{ 'Z' };
  63.  
  64.     std::cout << message << '\n';
  65.     std::cout << encodecode(message, 'q') << '\n';
  66.     std::cout << encodecode(message, 'r') << '\n';
  67.     std::cout << encodecode(message, 'r') << '\n';
  68.     std::cout << encodecode(message, 'q') << '\n';
  69.  
  70.     //char* cPtr{nullptr};
  71.     //cPtr = &symb;
  72.     //cPtr = &message[0];
  73.     //cPtr = message
  74.  
  75.     //std::cout <<  (+res) << '\n';
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement