如何在数字的二进制表示中找到1的数量?(How to find the number of 1's in a binary representation of a number?)
从其他搜索中,我发现这个问题被称为“汉明重量”或“人口数量”。 有这么多的统计数据,有很多答案吗? 我需要以简单的方式找到解决方案吗? 复杂性不是什么大问题。 在Java的Integer.bitCount中是否有任何内置函数?
我现在这样做如下。
var binary = 3; var original = binary; var count = 0; while(binary>0) { binary = binary >> 1 << 1; if(original-binary==1) count++; original = binary >> 1; binary = original; }
这有更好,更简单,更优雅的方式吗?
From other searches, I found that this problem is called 'Hamming Weight' or 'Population Count'. There are lot of answers out there given with so many statistics? I need to find the solution in a simple way? Complexity is not a big deal. Is there any in-built function in JavaScript like Java's Integer.bitCount?
I'm currently doing this as follows.
var binary = 3; var original = binary; var count = 0; while(binary>0) { binary = binary >> 1 << 1; if(original-binary==1) count++; original = binary >> 1; binary = original; }
Is there a better, more simple as well as elegant way for this?
原文:https://stackoverflow.com/questions/24506555
最满意答案
波纹管代码片段应该产生所需的结果:
Y^ = grp2idx(Y)
The bellow code snippet should produce the desired results:
Y^ = grp2idx(Y)
相关问答
更多-
波纹管代码片段应该产生所需的结果: Y^ = grp2idx(Y) The bellow code snippet should produce the desired results: Y^ = grp2idx(Y)
-
int()是将标准转换为整数值的Python标准内置函数。 你用一个包含一个数字作为参数的字符串来调用它,并且返回转换成一个实际整数的数字: print int("1") + 1 以上打印2 。 如果你知道你的列表的结构(它只包含列表,只有一个级别),你可以在python 2中这样做: T2 = [map(int, x) for x in T1] 在python 3: T2 = [list(map(int, x)) for x in T1] int() is the Python standard b ...
-
我找不到任何DQL关键字(你可能需要自定义Doctrine函数),但它可以通过其他方式完成。 如果你写: ->orWhere("CONCAT(street.id, '') LIKE :query") ->orWhere("CONCAT(street.lastUpdated, '') LIKE :query") 转换为字符串将隐式完成,使用LIKE运算符应该没有问题。 I couldn't find any DQL keywords for this (you'd probably need ...
-
这是你在寻找的东西: z = dec2bin('abc',8)' z1 = z(:)' z2 = arrayfun(@str2double, z1) z1 = 011000010110001001100011 z2 = 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 1 ...
-
查看输出后,您可能会注意到str_cetag和cel_num已正确写入和读取。 问题是acq_date太大而无法存储为8位无符号整数,这是fwrite的默认值。 这工作正常: str_cetag = 'AIMIDB'; cel_num = 89; acq_date = 20150422110459; % Open and write to file fid = fopen('BFile.bin','w'); fwrite(fid,str_cetag); fwrite(fid,cel_num,'uint8' ...
-
Matlab onehot整数(Matlab onehot to integers)[2022-06-04]
你可以使用find并返回像这样的列索引 Y = [1 0 0; 0 1 0; 0 1 0]; [~, new_y] = find(Y); % output: [1; 2; 2] is the col indices of your 1s 同样,如果您的输入是转置,您可以返回行索引 [new_y, ~] = find(Y); % output: [1; 2; 3] is the row indices of your 1s You could use find and return only t ... -
如何在matlab中将逗号分隔的字符串转换为字符串的单元格数组(How to convert comma-separated string to cell array of strings in matlab)[2022-06-26]
这里有一个解决方案,可以在逗号,分号或空格处切断字符串,并且适用于任何长度的字符串 string = 'A, BB, C' tmp = regexp(string,'([^ ,:]*)','tokens'); out = cat(2,tmp{:}) out = 'A' 'BB' 'C' Here's a solution that will cut up the string at commas, semicolons, or white spaces, and that w ... -
您需要手动删除双引号符号。 您可以使用cellfun将匿名函数应用于每个单元格。 这有两种可能的方法: 匿名函数简单地剥离了第一个和最后一个字符 。 如果双引号符号始终位于这些位置,则此方法有效: a = {'"15"'; '"16"'; '"17"'; '"30"'; '"50"'}; result = cellfun(@(x) str2double(x(2:end-1)), a); 如果某些字符串可能不包含双引号符号,或者可能包含多个任意位置,请使用匿名函数删除该符号的任何出现 : a = {'15' ...
-
在Pandas列中将字符串转换为int列表的快速方法?(Fast way to convert strings into lists of ints in a Pandas column?)[2023-08-29]
由于汉明距离并不关心幅度差异,因此我可以用df.apply(lambda x: np.array([mapping[char] for char in x]))替换df.apply(lambda x: np.array([mapping[char] for char in x]))来获得大约40-60%的加速df.apply(lambda x: map(ord, x))在虚拟数据集上。 Since Hamming distance doesn't care about magnitude differenc ... -
只需将结果数组转换为int[] where id = ANY ( string_to_array('9755, 9759', ',')::int[] ) Just cast the resulting array to an int[] where id = ANY ( string_to_array('9755, 9759', ',')::int[] )