Mail Archives: djgpp/1998/07/10/15:05:12
Horst Kraemer wrote:
>
> On Wed, 08 Jul 1998 22:38:27 +0200, dns AT cyberdude DOT com wrote:
>
> >I use the following code:
> >unsigned short s=1;
> >unsigned int t=1;
> >while ((s<<t)!=0) { // this loop is infinite! but why?
> > t++; }
>
> You just discovered a case of undefined behaviour.
If you really need to know the number of bits in a `short', you could
do:
unsigned short s = 1;
unsigned int t = 1;
while ((s <<= 1) != 0) t++;
It's theoretically possible that some machines might barf when `s'
overflows, but I don't know of any that do.
--
Nate Eldredge
nate AT cartsys DOT com
- Raw text -