Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void printNum(int num) {
  7. int cur = 128;
  8. for (int i = 0; i < 8; i++) {
  9. cout << ((num&cur) != 0 ? 1 : 0);
  10. cur >>= 1;
  11. }
  12. cout << endl;
  13. }
  14.  
  15. int revertBit(int a) {
  16. int pos = 1;
  17. int res = 0;
  18. for (int i = 0; i < 8; i++) {
  19. int tmp = a & pos;
  20. if ((a&pos) != 0) {
  21. res |= (1 << (7 - i));
  22. }
  23. pos <<= 1;
  24. }
  25. return res;
  26. }
  27. void revert(char* text, int size) {
  28. ofstream out("out.txt", ios::binary);
  29. for (int i = size - 1; i > -1; i--) {
  30. out << (char) revertBit((int) text[i]);
  31. }
  32. out.close();
  33. }
  34.  
  35. int main(void) {
  36. printNum(64 + 32 + 8 + 2);
  37. printNum(revertBit(64 + 32 + 8 + 2));
  38. ifstream in("in.txt", ios::ate | ios::binary); //поставили курсор в конец файла
  39. int size = in.tellg();
  40. char* text = new char[size];
  41. in.seekg(0, ios::beg); //поставили курсор в начало
  42.  
  43. for (int i = 0; i < size; i++) {
  44. text[i] = in.get();
  45. }
  46. in.close();
  47.  
  48. revert(text, size);
  49.  
  50. system("pause");
  51. return 1;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement