Path: csiph.com!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: portable way to get highest bit set? Date: Sun, 15 Oct 2023 01:35:05 -0700 Organization: A noiseless patient Spider Lines: 25 Message-ID: <86r0lw8erq.fsf@linuxsc.com> References: <878r884vi5.fsf@bsb.me.uk> <87edi0qide.fsf@bsb.me.uk> <86cyxgabe6.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: dont-email.me; posting-host="eb478ed0db5b06d993c3ccd040701c7e"; logging-data="440333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/g68YYkaXNxaesu6XetLGwXcvL+XkD3IQ=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:NLiGcTO8TnlQNcyHwpzPnh70TRw= sha1:dySae5+7kunKga4lcIgtljZZpNo= Xref: csiph.com comp.lang.c:272711 jak writes: > jak ha scritto: > >> Tim Rentsch ha scritto: >> >>> jak writes: >>> >>>> unsigned long h_bit(unsigned long val) >>>> { >>>> int i; >>>> for(i = 0; val > 1; val /= 2, i++); >>>> return val << i; >>>> } >>> >>> A nice compact solution. I like the way it >>> naturally deals with the case when val is zero. >> >> This is not true because if Val is 0 also the bit shifted to left >> is, so also its result is 0. This one of the reason why the exit >> from the loop is greater than 1. > > Sorry about the answer. I have translated your comment badly. No worries. I see how it works, and it's cool.