Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion bit count or bit set && Python3

Received: by 10.180.107.167 with SMTP id hd7mr1678389wib.0.1351477537648;
        Sun, 28 Oct 2012 19:25:37 -0700 (PDT)
Path: q13ni92081wii.0!nntp.google.com!feeder2.cambriumusenet.nl!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!newsreader4.netcologne.de!news.netcologne.de!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path: <ros...@gmail.com>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@mail.python.org
X-Spam-Status: OK 0.019
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'python': 0.09; 'integers':
	0.09; 'subject:Python3': 0.09; 'subject:set': 0.09;
	'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'oct':
	0.16; 'subject:bit': 0.16; 'wrote:': 0.17; 'instance,': 0.17;
	'test.': 0.17; 'sort': 0.21; 'bit': 0.21;
	'received:209.85.214.174': 0.21; 'this:': 0.23; 'header:In-Reply-
	To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27;
	'fast.': 0.29; 'unlikely': 0.29; 'fri,': 0.30; 'could': 0.32;
	'to:addr:python-list': 0.33; 'skip:b 20': 0.34;
	'received:google.com': 0.34; 'christian': 0.34; 'list': 0.35;
	'faster': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but':
	0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38;
	'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'where':
	0.40; 'header:Received:5': 0.40; 'your': 0.60; 'profile': 0.61;
	'26,': 0.65; 'counts': 0.81; 'approach.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
	h=mime-version:in-reply-to:references:date:message-id:subject:from:to
	:content-type; bh=y4g7QRTlxwTxQbTv651QnR9r/VMdON8362GNh0eiTAA=;
	b=effAsDkX1WIz95SI7fL9HXb/l1lvX5PzThVxHD9FXgcy0mhChGBl24V49HcUF4D9Zi
	eNHtJ1PHI22pu28V4rMMwPfkXCn0bj86g0RZ1E58ePIJrjtSfIHEJI4nUx4PZSYwU2Od
	52TxGer/onlaq3kKp/TSgayzw7MDwHmpeMZjgS4EGPtexEzCtHyPWdkG7Btbq/YVk91h
	JHSofTzrjVcKGkqT55VwvU5BNVVkiCJQpGsakM8ruBFfnXMzumvAC57CQYtzG10xZ4mW
	kzqxggLNl5tB8JyMkRW2aE4cbHzEImAEU+zsX8Y245vSLDEKf0dOqgnVPAyQUElPd/ym
	NcTA==
MIME-Version: 1.0
In-Reply-To: <k6bllq$3g...@ger.gmane.org>
References: <5089511E.4090...@earthlink.net>
	<k6bllq$3g...@ger.gmane.org>
Date: Fri, 26 Oct 2012 02:31:53 +1100
Subject: Re: bit count or bit set && Python3
From: Chris Angelico <ros...@gmail.com>
To: python-l...@python.org
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: General discussion list for the Python programming language
	<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/python-list>,
	<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
	<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.2849.1351179123.27098.python-l...@python.org>
Lines: 19
NNTP-Posting-Host: 2001:888:2000:d::a6
X-Trace: 1351179123 news.xs4all.nl 6852 [2001:888:2000:d::a6]:43142
X-Complaints-To: ab...@xs4all.nl
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes <christ...@python.org> wrote:
> Simple, easy, faster than a Python loop but not very elegant:
>
>    bin(number).count("1")

Unlikely to be fast.

What you may want is some sort of hybrid loop/lookup approach. Do you
know what your highest bit number is going to be? For instance, are
all your integers 32-bit? You could use something like this:

c = bitcount[n&255] + bitcount[n>>8&255] + bitcount[n>>16&255] + bitcount[n>>24]

where bitcount is a list of 256 values, giving the counts for each
value from 0 to 255.

Profile and test. :)

ChrisA