计数在比特数 [英] Count bits in the number

查看:174
本文介绍了计数在比特数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer\">Best算法来计算一个32位整数设置的位数?


假设你有一个数字。有没有什么方法来计算相等于1这个数字,不使用迭代的二进制重新presentation位?我的意思是,有没有办法使用一些位运算符和口罩,以做到在固定的时间。我需要的解决方案,将两种架构的32位和64位工作。啊差点忘了,我需要它的C语言或汇编语言也是不错的。

Suppose you have a number. Is there any way to count the bits which equals to 1 in binary representation of that number, not using iteration? I mean, is there any way to do it in constant time using some bitwise operators and masks. I need solution which will work well for both architectures 32 bit and 64 bit. Ah almost forgot, I need it for C language or assembler is also good.

推荐答案

有一个位处的 http://graphics.stanford.edu/~seander/bithacks.html 。位计数算法在很多的http:// gurmeetsingh.word press.com / 2008/08/05 /快位计数例程/

There is a bit counting algorithm without a loop at http://graphics.stanford.edu/~seander/bithacks.html. Lots of bit counting algorithms at http://gurmeetsingh.wordpress.com/2008/08/05/fast-bit-counting-routines/

这篇关于计数在比特数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆