Advertisement
constk

Task_10_Masks

Oct 15th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <iostream>
  4.  
  5. int main() {
  6. setlocale(0, "Russian");
  7.  
  8. unsigned char x, current_bit;
  9. unsigned short result = 0, i = 0;
  10. int xi;
  11.  
  12. puts("Задание 10. Посчитать количество нулей в однобайтовом числе. \n");
  13. printf("Введите целое шестнадцатеричное число: ");
  14. scanf("%x", &xi);
  15.  
  16. x = xi;
  17.  
  18. current_bit = x & 0x01;
  19. result = result + current_bit;
  20. x = x >> 1;//first iteration
  21.  
  22. current_bit = x & 0x01;
  23. result = result + current_bit;
  24. x = x >> 1;//second
  25.  
  26. current_bit = x & 0x01;
  27. result = result + current_bit;
  28. x = x >> 1;//third
  29.  
  30. current_bit = x & 0x01;
  31. result = result + current_bit;
  32. x = x >> 1;//fourth
  33.  
  34. current_bit = x & 0x01;
  35. result = result + current_bit;
  36. x = x >> 1;//fivth
  37.  
  38. current_bit = x & 0x01;
  39. result = result + current_bit;
  40. x = x >> 1;//sixth
  41.  
  42. current_bit = x & 0x01;
  43. result = result + current_bit;
  44. x = x >> 1;//seventh
  45.  
  46. current_bit = x & 0x01;
  47. result = result + current_bit;
  48. x = x >> 1;//eighth
  49.  
  50. result = 8 - result;
  51.  
  52. printf("В числе %d нулей\n", result);
  53.  
  54. system("pause");
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement