www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/10/15:05:12

Sender: nate AT cartsys DOT com
Message-ID: <35A66020.DDA10500@cartsys.com>
Date: Fri, 10 Jul 1998 11:40:32 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
MIME-Version: 1.0
To: Horst Kraemer <horst DOT kraemer AT snafu DOT de>
CC: djgpp AT delorie DOT com
Subject: Re: Infinite loop???
References: <35A3D8C3 DOT 208E6D23 AT cyberdude DOT com> <35a3fc6d DOT 35890095 AT news DOT snafu DOT de>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019