Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include<stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. char msk[] = { 0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1 };
  9.  
  10. void dump(char* b, int size) {
  11. for (int i = size - 1; i >= 0; i--) {
  12. for (int j = 0; j < 8; j++) {
  13. std::cout << ((*(b + i) & msk[j]) ? 1 : 0);
  14. }
  15. printf(" ");
  16. }
  17. }
  18.  
  19. template<typename X>
  20. void pryamoi(X a) {
  21. int size = sizeof(X) * 8;
  22. std::cout << (((a) & (int)pow(2, size - 1)) ? 1 : 0);
  23. a = abs(a);
  24. int* m = new int[size - 1];
  25. for (int i = 0; i < size - 1; i++) {
  26. m[i] = a % 2;
  27. a = a / 2;
  28. }
  29. for (int i = 2; i < size + 1; i++) {
  30. cout << m[size - i];
  31. if (i % 8 == 0) {
  32. printf(" ");
  33. }
  34. }
  35. }
  36.  
  37. template<typename X>
  38. void obratni(X a) {
  39. int size = sizeof(X) * 8;
  40. std::cout << (((a) & (int)pow(2, size - 1)) ? 1 : 0);
  41.  
  42. int* m = new int[size - 1];
  43. if (a < 0) {
  44. a = abs(a);
  45. for (int i = 0; i < size - 1; i++) {
  46. m[i] = abs((a % 2) - 1);
  47. a = a / 2;
  48. }
  49. }
  50. else {
  51. for (int i = 0; i < size - 1; i++) {
  52. m[i] = a % 2;
  53. a = a / 2;
  54. }
  55. }
  56. for (int i = 2; i < size + 1; i++) {
  57. cout << m[size - i];
  58. if (i % 8 == 0) {
  59. printf(" ");
  60. }
  61. }
  62. }
  63.  
  64. int main() {
  65. int a = -4;
  66.  
  67. dump((char*)&a, sizeof(int));
  68. cout << endl;
  69. pryamoi(a);
  70. cout << endl;
  71. obratni(a);
  72.  
  73. float s = -1.44;
  74. cout << endl;
  75. dump((char*)&s, sizeof(float));
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement