有许多位计数的实现,但在我的情况下,我需要测试一个任意大的数字是否包含至多两个设置位.
我编写了以下函数来完成这项工作并且似乎非常快,但我想知道它是否可以针对C#进一步优化.这个函数在一个循环中被调用几百万次.
public static byte [] BitCountLookupArray = new byte []
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, …Run Code Online (Sandbox Code Playgroud) 什么是位数的大 O?我不确定该方法是如何工作的,但我认为它是在 O(logn) 中完成的。
特别是使用此代码(其中 x = 4,y = 1):
return Integer.bitCount(x^y);
Run Code Online (Sandbox Code Playgroud) 我发现__builtin_ctzll通过后Intrinsic 来计算 64 位整数中的尾随零位非常快地计算 64 位 int 的尾随零位?。
我是 C++ 的初学者,不知道如何包含这个函数。
我尝试使用#include,但这没有任何意义。我发现这个“内置”来自 GNU,但我不知道如何处理这些信息。如何为我的项目准备合适的库/扩展?
Integer.bitCount()的Java API告诉我们:
"public static int bitCount(int i)
返回指定int值的二进制补码表示形式中的一位数.此功能有时称为人口计数.
返回:指定int值的二进制补码表示形式中的一位数.自:1.5"
因此,如果我们取255并将其转换为二进制,我们得到11111111.如果我们将其转换为二进制补码版本,我们得到00000001,使得一位数为1.但是,如果我运行此代码:
import java.lang.*;
public class IntegerDemo {
public static void main(String[] args) {
int i = 255;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned integer value
represented by the argument in binary (base 2) */
System.out.println("Binary = " + Integer.toBinaryString(i));
/* The next few lines convert the binary number to its two's
complement representation */
char[] tc= Integer.toBinaryString(i).toCharArray();
boolean firstFlipped = true;
for (int j …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我需要AND两个大小为40字节(320位)的二进制数组,然后在C++中计算设置位数.我找到了一些算法来做这个,但我想知道在c ++中实现它的最快方法是什么.我的意思是什么c ++数据类型是正确的?(unsinged char*,unsigned int 32,u_int64,...).我知道许多算法与32位整数兼容,尽管我的数组大小是40字节.
那个链接中描述的算法怎么样: 快速位计数技术哪个更快?
const类型更好还是没有区别?
任何帮助将非常感激.
是否已经BitCount为big.Int 编写了一个方法?数学/大似乎没有.
显然,如果没有,我会自己写一个 - 有没有人已经写过了?
我想要数字中的设置位数.像Java BigInteger.bitCount().
这是我的TA帮助我获得的代码,但后来我完全忘记了它是如何工作的,因为我似乎无法得到正确的答案,而面试评分是明天.如果有人可以提供帮助请.谢谢
* bitCount - returns count of number of 1's in word
* Examples: bitCount(5) = 2, bitCount(7) = 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 40
* Rating: 4
*/
int bitCount(int x) {
int m4 = 0x1 | (0x1<<8) | (0x1<<16) | (0x1<<24);
int m1 = 0xFF;
int s4 = (x&m4) + ((x>>1)&m4) + ((x>>2)&m4) + ((x>>3)&m4) + ((x>>4)&m4) + ((x>>5)&m4) + ((x>>6)&m4) + ((x>>7)&m4);
int s1 = (s4&m1) + …Run Code Online (Sandbox Code Playgroud) 我试图对Java中的两个64位Long变量执行XOR操作。问题是,当我在变量中添加超过16位时,它将失败。
例如,这有效并返回7:
Long h1 = Long.parseLong("1100001101001101");
Long h2 = Long.parseLong("1100001101000001");
System.out.println(Long.bitCount(h1 ^ h2));
Run Code Online (Sandbox Code Playgroud)
如果我将h1和h2的值增加到:
Long h1 = Long.parseLong("11000110000110100110101101001101");
Long h2 = Long.parseLong("11000011100001101001101101000001");
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
例外在线程“主” java.lang.NumberFormatException:对于输入字符串: “11000110000110100110101101001101”
在
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
在java.lang.Long.parseLong(Long.java:592)
在java.lang.Long.parseLong(Long.java:631)
如果我加倍(64位要计算的),则相同:
Long h1 = Long.parseLong("1100011100011000011010011010110100110110000110100110101101001101");
Long h2 = Long.parseLong("1100001110001100001101001101011010011011100001101001101101000001");
Run Code Online (Sandbox Code Playgroud)
关于为什么为什么失败超过16位有帮助吗?
我正在研究关于比特计数的不同方法,或给定整数的人口计数方法fopr,在这几天,我试图弄清楚以下算法是如何工作的
pop(x)=-sum(x<<i) where i=0:31
Run Code Online (Sandbox Code Playgroud)
我认为在计算x的每个值之后,我们会得到
x+2*x+4*x+8*x+16*x+..............+2^31*x =4294967294*x
Run Code Online (Sandbox Code Playgroud)
如果我们将它乘以-1,我们得到-4294967294*x,但它如何计算位数?请帮助我理解这个方法.谢谢