Guest User

Untitled

a guest
Nov 12th, 2019
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int INT_BIT = sizeof(int);
  4.  
  5. void print_bits(int b[], int size){
  6. int i = 0;
  7. while(i < size){
  8. printf("%i", b[i]);
  9. i++;
  10. }
  11. printf("\n");
  12. }
  13.  
  14. void init_bits(int b[], int size){
  15. int i = 0;
  16. while(i < size){
  17. b[i] = 0;
  18. i++;
  19. }
  20. }
  21.  
  22. void get_bits(int b[], int n){
  23. int i;
  24. int bit = 0;
  25. for(i = 0; i < n; i++){
  26. b[i] = (n >> i) & 1;
  27. }
  28. }
  29.  
  30. int get_int(int b[]){
  31. int i = 0;
  32. int a = 0;
  33. while(i < INT_BIT){
  34. a += b[i];
  35. a = a << 1;
  36. i++;
  37. }
  38. return a;
  39. }
  40.  
  41. int main(void){
  42. int b[INT_BIT];
  43. init_bits(b, INT_BIT);
  44. print_bits(b, INT_BIT);
  45. int b2[4];
  46. get_bits(b, 7);
  47. print_bits(b, 3);
  48. printf("%i\n",get_int(b2));
  49. printf("%i\n",get_int(b));
  50. return 0;
  51. }
RAW Paste Data