1. naive print bits function

I'm getting an infinite loop. I want to print from MSB to LSB. I'm not sure why its stuck...

Code:
```#include <climits>
#include <iostream>

using namespace std;

template<typename t>
//Q: how do I print it from MSB to LSB
void print_bits(const t& type)
{
size_t num_bits = sizeof(t) * CHAR_BIT;
size_t width = 0;

for ( size_t i = (num_bits - 1); i >= 0; --i )
{
mask = type & ( 1 << i);//turn off all bits except cur bit

if ( !mask )//=> if 0(base-10) it means cur bit is 0 (when cur bit is AND 1)where we turn off all bits, then output '0'
else
cout << 1;//b/c if it's non-zero in base-2, then output binary 1

if ( width % 4 == 3 )
cout << " ";//to separate into 4bit chunks for readability
++width;
}
cout << endl;
}

int main()
{
unsigned int garage = 90;
cout << "Status of each garage..." << endl;
print_bits(garage);

return 0;
}```
It outputs it correctly as: 0000 0000 0000 0000 0000 0000 0101 1010 (for base-10 of 90), but then repeats, why?

2. size_t is an unsigned type -> your condition i >= 0; is alwais true
try
Code:
`for ( int i = (num_bits - 1); i >= 0; --i )`
Kurt

3. Why would changing it from size_t (unsigned int) to int stop the infinite loop? It works, but why though?

4. You have to use a signed type or put
Code:
``` if ( i == 0 )
break;```
at the and of the loop
Kurt

5. an unsigned type never has a value lower then 0.
Kurt

6. I thought unsigned type was from: 0 to (2^n) - 1 where n is the data type's number of bits needed to store it. so int is 32bits so the range is from: 0 to (2^32) - 1

7. the condition i >= 0; is false only when the value is < 0.
Kurt

8. ok I get it now, 0 is smallest with unsigned, I wasn't thinking...

9. Here's all the ways I can think of fixing it.
Code:
```for (int i = numbits - 1; i >= 0; i--)
{
ifunc(i);
}

for (size_t i = numbits - 1; i != size_t(-1); i--) // is this always safe?
{
ifunc(i);
}

for (size_t i = numbits - 1;  1;  i--)
{
ifunc(i);
if (i == 0)
break;
}

size_t i = num_bits;
do {
--i;
ifunc(i);
} while (i != 0);```

10. The most common (and safe) way of solving it that I've seen is:
Code:
`for (size_t i = numbits; i-- > 0;)`
oogabooga, your third one fails when numbits is zero.
And for your second one, I had a feeling that unsigned overflow was okay, but that unsigned underflow was still UB.

11. By the way, if you used a higher warning level setting in your compiler (and you should), it should have warned you about this. I get this warning all the time. Saved me a lot of time over the years.

12. A bit cryptic perhaps, but I get the warning
Warning 11 warning C4702: unreachable code
on the original code using Visual Studio.
It doesn't complain about the infinite loop, but it definitely detects the infinite loop.

13. Hmm I can't seem to generate the warning now on GCC, but I've definitely seen it before.

14. I still prefer to iterate over a mask as done here on lines 11 and 27. The loop control is straight forward: bit > 0 is equivalent to what is written for the condition.

15. Hello cyberfish, how may i be in touch with you? I like your desktop streamer project