fork download
  1. #include <stdio.h>
  2.  
  3. // Function to get the position of the highest set bit (0-based)
  4. int getHighestBitPosition(unsigned int num) {
  5. int pos = -1;
  6. while (num) {
  7. pos++;
  8. num >>= 1;
  9. }
  10. return pos;
  11. }
  12.  
  13. // Function to print bits from 0 to highest set bit
  14. void printRelevantBits(unsigned int num) {
  15. int highestBit = getHighestBitPosition(num);
  16.  
  17. for (int i = 0; i <= highestBit; i++) {
  18. int bit = (num >> i) & 1;
  19. printf("Bit %d: %d\n", i, bit);
  20. }
  21. }
  22.  
  23. int main() {
  24. unsigned int value = 32; // Change this to any value you want
  25. printRelevantBits(value);
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5332KB
stdin
45
stdout
Bit 0: 0
Bit 1: 0
Bit 2: 0
Bit 3: 0
Bit 4: 0
Bit 5: 1