Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Re: ( n & -n ).bit_length() - 1 ## Really? Date: 19 Sep 2026 18:23:32 GMT Organization: Stefan Ram Lines: 13 Expires: 1 Sep 2027 11:59:58 GMT Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de 8UcgJlHBE/yspVtz7XPNvAGQ5cblEo7a963UcsO9TLV5ua Cancel-Lock: sha1:r1r06TvsBO/gltW4y10VKvmehpw= sha256:4wfJoZKlGKosZg1w6wsCCEYjajTaiDJFjAyUtiSBe9c= X-Copyright: (C) Copyright 2026 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en Xref: csiph.com comp.lang.python:198022 "Johann \"Myrkraverk\" Oskarsson" wrote or quoted: >The above formula seems to work, given a few spot checks, but I thought >this should be a utility function in the underlying multiprecision lib- >rary in Python. Is the calculation n & -n really necessary? While "n & -n" might take some time for large Python integers, I see no way to do it faster in Python. In C, one might be able to access the segments of large numbers (the "limbs") starting with the least significant one to shortcut the operation as soon as a "1" is found.