bkdrhash demo
bkdrhash demo
/* Start of bkdrhash.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <getopt.h> #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif /* End of defined(__cplusplus) || defined(c_plusplus) */ unsigned long BKDRHash(unsigned long seed, const char *str, unsigned int len) { if (str == NULL || seed == 0 || len == 0) { return 0; } static const int c1 = 0xcc9e2d51; static const int c2 = 0x1b873593; static const int r1 = 15; static const int r2 = 13; static const int m = 5; static const int n = 0xe6546b64; unsigned long hash = seed; const int nblocks = len / 4; const int *blocks = (const int *)str; int i; for (i = 0; i < nblocks; i++) { int k = blocks[i]; k *= c1; k = (k << r1) | (k >> (32 - r1)); k *= c2; hash ^= k; hash = ((hash << r2) | (hash >> (32 - r2))) * m + n; } const int *tail = (const int *)(str + nblocks * 4); int k1 = 0; switch (len & 3) { case 3: k1 ^= tail[2] << 16; case 2: k1 ^= tail[1] << 8; case 1: k1 ^= tail[0]; k1 *= c1; k1 = (k1 << r1) | (k1 >> (32 - r1)); k1 *= c2; hash ^= k1; } hash ^= len; hash ^= (hash >> 16); hash *= 0x85ebca6b; hash ^= (hash >> 13); hash *= 0xc2b2ae35; hash ^= (hash >> 16); return hash; } #if defined(__cplusplus) || defined(c_plusplus) } #endif /* End of defined(__cplusplus) || defined(c_plusplus) */ int main(int argc, char *argv[]) { if ( argc != 3 ) { fprintf(stderr, "Using: %s 1001 user111\n", argv[0]); return -1; } long seed = atol(argv[1]); const char *str = argv[2]; if (seed <= 0) { fprintf(stderr, "Error: %s\n", "seed must be greater than zero"); return -1; } if (str == NULL) { fprintf(stderr, "Error: %s\n", "string cannot be empty"); return -1; } fprintf(stdout, "Info: seed(%ld), str(%s)\n", seed, str); unsigned long result = BKDRHash((unsigned long)seed, str, strlen(str)); if ( result == 0 ) { fprintf(stderr, "Error: %s\n", "BKDRHash calculation failed"); return -1; } fprintf(stdout, "Info: BKDRHash result(%lu)\n", result); return 0; } /* End of bkdrhash.c */
====== End
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2020-01-08 Q_DECLARE_PRIVATE与Q_DECLARE_PUBLIC
2019-01-08 DNS 透明代理
2018-01-08 redis cluster管理工具redis-trib.rb详解
2018-01-08 Redis主从集群的Sentinel配置
2018-01-08 redis cluster 设置密码做集群时gem下client.rb文件修改
2018-01-08 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)