简体   繁体   English

任何人都可以请检查我是否正确执行此散列

[英]Can anyone please check if I am doing this hashing correctly

I am trying to do an implementation of Fowler–Noll–Vo hash function我正在尝试实现Fowler–Noll–Vo 哈希函数

The Pseudocode looks like this伪代码看起来像这样

  hash = FNV_offset_basis
   for each byte_of_data to be hashed
        hash = hash × FNV_prime
        hash = hash XOR byte_of_data
   return hash

This is my code for that这是我的代码

uint8_t            byte_of_data;
uint16_t          hash;
uint16_t          FNV_offset_basis;
uint16_t          FNV_prime;
void computeHash(std::string p)
{
    FNV_offset_basis =  0xcbf29ce484222325;
    FNV_prime        =  0x100000001b3;

    hash = FNV_offset_basis;
    
    //Iterate through the string
    for(int i=0 ; i<p.size();i++)
    {
        hash = hash * FNV_prime;
        hash = hash ^ p.at(i);
    }
   
    std::cout << hash;  //output 2983
     std::cout << std::hex << hash ; //ba7
}

Now I am using it as this现在我用它作为这个

int main()
{
   computeHash("Hello");
}

I am testing my result here and I get the result as 0d47307150c412cf我在这里测试我的结果,我得到的结果为0d47307150c412cf

Update:更新:

I fixed my types to我修复了我的类型

uint8_t            byte_of_data;
uint64_t          hash;
uint64_t          FNV_offset_basis;
uint64_t          FNV_prime;

and I get the result fa365282a44c0ba7 which still does not match the result 0d47307150c412cf我得到的结果 fa365282a44c0ba7 仍然与结果 0d47307150c412cf 不匹配

Any suggestions on how I can fix this关于如何解决此问题的任何建议

This is the problem:这就是问题:

uint16_t          FNV_offset_basis;
uint16_t          FNV_prime;
void computeHash(std::string p)
{
    FNV_offset_basis =  0xcbf29ce484222325;
    FNV_prime        =  0x100000001b3;

FNV_prime and FNV_offset_basis are both 16-bit integers in your code, yet inexplicably you're assigning long 64-bit integers to them, your C++ compiler should be warning you about an improper literal assignment. FNV_primeFNV_offset_basis在您的代码中都是 16 位整数,但您莫名其妙地为它们分配了 64 位长整数,您的 C++ 编译器应该警告您不正确的文字分配。

What happens if your change the types to uint64_t ?如果您将类型更改为uint64_t会发生什么?

Your current result fa365282a44c0ba7 is correct according to the official reference根据官方参考,您当前的结果fa365282a44c0ba7是正确的
source code (in C) and manual calculation too... this makes the test site wrong.源代码(在 C 中)和手动计算也是......这使得测试站点错误。

The reference source files are linkedhere : C file and H file参考源文件链接在这里C文件H文件
I removed the includes of longlong.h and added following two code parts instead:我删除了longlong.h的包含并添加了以下两个代码部分:

/*before the reference code*/

#include <stdint.h>
#define HAVE_64BIT_LONG_LONG
typedef uint64_t u_int64_t;
typedef uint32_t u_int32_t;

/*after it*/

#include<stdio.h>
int main()
{
    printf("%llx\n", fnv_64_str("Hello", FNV1_64_INIT));
}

to make it compile with gcc -std=c11 source.c让它用gcc -std=c11 source.c编译
( gcc (i686-posix-sjlj-rev0, Built by MinGW-W64 project) 4.9.1 ) ( gcc (i686-posix-sjlj-rev0, Built by MinGW-W64 project) 4.9.1 )

Output: fa365282a44c0ba7 .输出: fa365282a44c0ba7
And Ideone says so too Ideone 也这么说

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能解释这个C ++代码在做什么? - Can anyone please explain what this C++ code is doing? 我在 cpp 文件中收到重新定义错误,谁能说一下,如何修复它? - I am getting an redifinition error in the cpp file, can anyone please say, how to fix it? 谁能给我解释一下这个 function,我无法理解 - Could anyone please explain me this function, I am not able to understand this 我正在尝试编写此程序,但它一直显示错误,下面提到的所有内容都可以检查 - I am trying to write this program but it keeps on displaying an a error everything is mentioned below can anyone check 在curl和c ++中测量下载速度。 我做得对吗? - Measuring Download Speed in curl and c++. Am I doing it correctly? 学习C ++向量...我这样做正确吗? 我对vector :: push_back()的反复调用可以简化吗? - Learning C++ vectors… Am I doing this correctly? Can my repeated calls to vector::push_back() be simplified? 谁能想到我可以对此进行错误检查的方法? - Can anyone think of a way I can error check this? 当然我正在做一些非常错误的提升:replace_all。 请进行理智检查? - Sure I'm doing something very wrong with boost:replace_all. Sanity check please? 任何人都可以看看这个吗? [等候接听] - Can anyone please take at a look at this? [on hold] 使用类的链表中的分段错误。 我的方法正确吗? - Segmentation fault in a linked list using classes. Am I doing the method correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM