English 中文(简体)
在散射功能中达到16倍。
原标题:get16bits macro in hash function
  • 时间:2012-05-15 14:57:47
  •  标签:
  • c
  • hash

我正研究一下白天的功能,并进入一个有一例的网站上。 多数法典很容易掌握,但这一宏观功能,我实际上可以把我的头部总结下来。

难道有人会把这里发生的事情打下了吗?

#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) +(uint32_t)(((const uint8_t *)(d))[0]))
最佳回答

基本上,在32个轨道分类账中,它得到的不到16倍。

让我们打破僵局

#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) +(uint32_t)(((const uint8_t *)(d))[0]))
uint32_t a = 0x12345678;
uint16_t b = get16bits(&a); // b == 0x00005678

首先,我们必须通过一个至<条码>的>目标16bits()的地址,否则不会奏效。

(((uint32_t)(const uint8_t *)(d))[1])) << 8

this first converts the 32 bit integer into an array of 8 bit integers and retrieves the 2 one. It then shifts the value by 8 bit so it and adds the lower 8 bits to it

+ (uint32_t)(((const uint8_t *)(d))[0]))

举我们为例,它将

uint8_t tmp[4] = (uint8_t *)&a;
uint32_t result;
result = tmp[1] << 8; // 0x00005600
result += tmp[0]; //tmp[0] == 0x78
// result is now 0x00005678
问题回答

宏观相当于:

static uint32_t get16bits(SOMETYPE *d)
{
unsigned char temp[ sizeof *d];
uint32_t val;

memcpy(temp, d, sizeof *d);

val = (temp[0] << 8)
     + temp[1];
return val;
}

但是,宏观论点没有类型,职能论点确实如此。

另一种方式是:

static uint32_t get16bits(SOMETYPE *d)
{
unsigned char *cp = (unsigned char*) d;
uint32_t val;

val = (cp[0] << 8)
     + cp[1];
return val;
}

这一点也显示出了弱点:通过将1指数化,法典假定(d)的面积至少为2。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签