fork download
  1. /******************************************************************************
  2. Author: Deepank Korandla
  3. Date: May 25, 2016
  4. Effort: 15 minutes
  5. Purpose: This assignment allows the student to get further practice with
  6. bitwise operators and exposes them to the idea of creating a cohesive set of
  7. functions for interacting with an object type.
  8. ******************************************************************************/
  9.  
  10. #include <stdio.h>
  11.  
  12. void set_flag(unsigned int flag_holder[], int flag_position);
  13. void unset_flag(unsigned int flag_holder[], int flag_position);
  14. int check_flag(unsigned int flag_holder[], int flag_position);
  15. void display_32_flags_as_array(unsigned int flag_holder);
  16. void display_flags(unsigned int flag_holder[], int size);
  17.  
  18. int main(int argc, char* argv[]){
  19. //Set the first integer to zero and all others to zero by default.
  20. unsigned int flag_holder[5] = { 0 };
  21.  
  22. set_flag(flag_holder, 3);
  23. set_flag(flag_holder, 16);
  24. set_flag(flag_holder, 31);
  25. set_flag(flag_holder, 87);
  26.  
  27. display_flags(flag_holder, 5);
  28. printf("\n\n");
  29.  
  30. unset_flag(flag_holder, 31);
  31. unset_flag(flag_holder, 3);
  32. set_flag(flag_holder, 99);
  33. set_flag(flag_holder, 100);
  34.  
  35. display_flags(flag_holder, 5);
  36. return 0;
  37. }
  38.  
  39. void set_flag(unsigned int flag_holder[], int flag_position){
  40. int arrayIndex, bitPosition, checker;
  41. int maxBits = 32;
  42.  
  43. if (flag_position < maxBits) {
  44. arrayIndex = 0;
  45. bitPosition = flag_position;
  46. } else {
  47. arrayIndex = flag_position / maxBits;
  48. bitPosition = flag_position % maxBits;
  49. }
  50.  
  51. checker = 1 << (bitPosition - 1);
  52.  
  53. if ((flag_holder[arrayIndex] & checker) != checker) {
  54. flag_holder[arrayIndex] = flag_holder[arrayIndex] + checker;
  55. }
  56. }
  57.  
  58. void unset_flag(unsigned int flag_holder[], int flag_position){
  59. int arrayIndex, bitPosition, checker;
  60. int maxBits = 32;
  61.  
  62. if (flag_position < maxBits) {
  63. arrayIndex = 0;
  64. bitPosition = flag_position;
  65. } else {
  66. arrayIndex = flag_position / maxBits;
  67. bitPosition = flag_position % maxBits;
  68. }
  69.  
  70. checker = 1 << (bitPosition - 1);
  71.  
  72. if ((flag_holder[arrayIndex] & checker) == checker) {
  73. flag_holder[arrayIndex] = flag_holder[arrayIndex] - checker;
  74. }
  75. }
  76.  
  77. int check_flag(unsigned int flag_holder[], int flag_position){
  78. int arrayIndex, bitPosition, checker;
  79. int maxBits = 32;
  80.  
  81. if (flag_position < maxBits) {
  82. arrayIndex = 0;
  83. bitPosition = flag_position;
  84. } else {
  85. arrayIndex = flag_position / maxBits;
  86. bitPosition = flag_position % maxBits;
  87. }
  88.  
  89. checker = flag_holder[arrayIndex] >> (bitPosition - 1);
  90.  
  91. return (checker & 1);
  92. }
  93.  
  94. void display_32_flags_as_array(unsigned int flag_holder){
  95. int tempArray[1] = { flag_holder };
  96. int maxBits = 32;
  97. int i;
  98.  
  99. for (i = 0; i < maxBits; i = i + 1) {
  100. printf("%d", check_flag(tempArray, i));
  101.  
  102. if (i % 4 == 0) {
  103. printf(" ");
  104. }
  105. }
  106. }
  107.  
  108. void display_flags(unsigned int flag_holder[], int size){
  109. int i;
  110.  
  111. for (i = 0; i < size; i = i + 1) {
  112. display_32_flags_as_array(flag_holder[i]);
  113. printf("\n");
  114. }
  115. }
Success #stdin #stdout 0s 5520KB
stdin
Standard input is empty
stdout
0 0010 0000 0000 0001 0000 0000 0000 001
0 0000 0000 0000 0000 0000 0000 0000 000
0 0000 0000 0000 0000 0000 0010 0000 000
0 0000 0000 0000 0000 0000 0000 0000 000
0 0000 0000 0000 0000 0000 0000 0000 000


0 0000 0000 0000 0001 0000 0000 0000 000
0 0000 0000 0000 0000 0000 0000 0000 000
0 0000 0000 0000 0000 0000 0010 0000 000
0 0011 0000 0000 0000 0000 0000 0000 000
0 0000 0000 0000 0000 0000 0000 0000 000