Re: Is this acceptable (i.e., compliant) code?

From: anonymous (iunknown2k4_at_yahoo.co.in)
Date: 08/11/04


Date: 11 Aug 2004 09:55:35 -0700


> #include <stdio.h>
> #include <stddef.h>
> #include <limits.h>

> static void printbits(void *mem, size_t size)
> {
> size_t i;
> unsigned char mask;
> char *p = mem;
>
> for(i = 0; i < size; i++) {
> for(mask = 1 << (CHAR_BIT - 1); mask; mask >>= 1)
> putc((mask & p[i]) ? '1' : '0', stdout);
>
> putc(' ', stdout);
> }
> putc('\n', stdout);
> }

The function above prints from LSB..MSB, I wanted to print in the reverse order
I tried using

for(i = size; i >= 0 ; i--)

But goes into an infinite loop because of unsigned int comparison, so I used

for(i = size; i != UINT_MAX; i--)

This works but is there a better way to do that?