当前位置: 首页 > news >正文

汉明权重(Hamming Weight)(统计数据中1的个数)VP-SWAR算法

汉明权重(Hamming Weight)(统计数据中1的个数)VP-SWAR算法

定义

汉明重量是一串符号中非零符号的个数。它等于同样长度的全零符号串的汉明距离(在信息论中,两个等长字符串之间的汉明距离等于两个字符串对应位置的不同字符的个数)。
汉明重量在常见的数据位符号串中,它是1的个数。

算法思想

基于分治的算法,将n位二进制进行分组,通过一系列位移和位运算操作,可以在常数时间内计算多个字节的汉明重量,并且不需要使用额外的内存。

简化示例

假设一个8bit的2进制串 x=abcd,efgh其中a-b 属于{0,1}
求解的输出是 ans = a+b+c+d+e+f+g+h

step1. 2bits m1= 0101 0101

x&m1 = 0b0d 0f0h
(x>>1)&m1 = 0a0c 0e0g
求和得到[a+b]_2[c+d]_2 [e+f]_2[g+h]_2,这里[x]_2表示2位二进制中1的个数

step2. 4bits m2 = 0011 0011

x&m2 = 00[c+d]_2 00[g+h]_2
(x>>2)&m2 = 00[a+b]_2 00[e+f]_2
求和得到[a+b+c+d]_4 [e+f+g+h]_4

step3. 8bits m4 = 0000 1111

x&m4 = 0000 [e+f+g+h]_4
(x>>4)&m4 = 0000 [a+b+c+d]_4
求和得到 [a+b+c+d+e+f+g+h]_8
对应的十进制值就是最终的答案

算法实现 variable-precision SWAR算法

const uint64_t m1  = 0x5555555555555555; //binary: 0101...
const uint64_t m2  = 0x3333333333333333; //binary: 00110011..
const uint64_t m4  = 0x0f0f0f0f0f0f0f0f; //binary:  4 zeros,  4 ones ...
const uint64_t m8  = 0x00ff00ff00ff00ff; //binary:  8 zeros,  8 ones ...
const uint64_t m16 = 0x0000ffff0000ffff; //binary: 16 zeros, 16 ones ...
const uint64_t m32 = 0x00000000ffffffff; //binary: 32 zeros, 32 ones
const uint64_t h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3...//This is a naive implementation, shown for comparison,
//and to help in understanding the better functions.
//This algorithm uses 24 arithmetic operations (shift, add, and).
// 朴素算法
int popcount64a(uint64_t x)
{x = (x & m1 ) + ((x >>  1) & m1 ); //put count of each  2 bits into those  2 bits x = (x & m2 ) + ((x >>  2) & m2 ); //put count of each  4 bits into those  4 bits x = (x & m4 ) + ((x >>  4) & m4 ); //put count of each  8 bits into those  8 bits x = (x & m8 ) + ((x >>  8) & m8 ); //put count of each 16 bits into those 16 bits x = (x & m16) + ((x >> 16) & m16); //put count of each 32 bits into those 32 bits x = (x & m32) + ((x >> 32) & m32); //put count of each 64 bits into those 64 bits return x;
}

详细步骤

详细步骤
优化算法

//This is better when most bits in x are 0
//This algorithm works the same for all data sizes.
//This algorithm uses 3 arithmetic operations and 1 comparison/branch per "1" bit in x.
// 适用于0比较多的数
// 数字 n中最低位的 1 总是对应 n - 1 中的 0
// 将 n 和 n - 1 进行与运算总是能把 n 中最低位的 1 变成 0,并保持其他位不变
int popcount64d(uint64_t x)
{int count;for (count=0; x; count++)x &= x - 1;return count;
}// 常用写法
int hammingWeight(uint32_t n) {int count = 0;while( n ){count ++;n &= n-1;}return count;
}// 查表法 用空间换时间 从而得到O(1)的最优算法
// 以4bit的串为例,可以构造一个数组int counts[16]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}.
// 对于4bit的x, x的hamming weight为:counts[x].
static unsigned char wordbits[65536] = { bitcounts of ints between 0 and 65535 };
static int popcount(uint32 i)
{return (wordbits[i&0xFFFF] + wordbits[i>>16]);
}

参考

Hamming weight WIKI
汉明权重(hamming weight) ----- 计算数据位中1的个数

相关文章:

  • 网闸(Network Gatekeeper或Security Gateway)
  • Pytorch深度学习实践(5)逻辑回归
  • 请求重定向后,端口自动去掉的问题
  • -XX:MaxDirectMemorySize和-Dio.netty.maxDirectMemory区别
  • 使用Python实现深度学习模型:智能安防监控与异常检测
  • k8s中部署Jenkins、SonarQube、StorageClass部署流程
  • 微服务实战系列之玩转Docker(七)
  • golang设置远程调试
  • Mamba-yolo|结合Mamba注意力机制的视觉检测
  • Spring Boot整合Quartz使用的详解
  • 基于python的BP神经网络红酒品质分类预测模型
  • Github个人网站搭建详细教程【Github+Jekyll模板】
  • HTTP详解
  • MySQL之视图和索引实战
  • 使用git工具管理泰山派内核源码目录及抽打补丁简易流程
  • 【翻译】babel对TC39装饰器草案的实现
  • CSS盒模型深入
  • Odoo domain写法及运用
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • windows下mongoDB的环境配置
  • 反思总结然后整装待发
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 解析 Webpack中import、require、按需加载的执行过程
  • 近期前端发展计划
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 树莓派 - 使用须知
  • 小程序button引导用户授权
  • 赢得Docker挑战最佳实践
  • linux 淘宝开源监控工具tsar
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #QT(智能家居界面-界面切换)
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (1)svelte 教程:hello world
  • (6) 深入探索Python-Pandas库的核心数据结构:DataFrame全面解析
  • (C语言)共用体union的用法举例
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (九)One-Wire总线-DS18B20
  • (南京观海微电子)——I3C协议介绍
  • (学习日记)2024.01.09
  • (一)Neo4j下载安装以及初次使用
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .NET Core 成都线下面基会拉开序幕
  • .net core 控制台应用程序读取配置文件app.config
  • .net SqlSugarHelper
  • .net 发送邮件
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .NET开发不可不知、不可不用的辅助类(三)(报表导出---终结版)