我試圖基本哈希表program..which是計算在整數1點的數量:什麼是在C中提取整數(假設32位整數)8個半字節的方法?
我有一個哈希表,該表是在0,1,2...E,F
HashTable:
0 0
1 1
2 1
3 2
4 1
5 1
6 2
7 3
8 1
9 2
A 2
B 3
C 2
D 3
E 3
F 4
用的1的數目的陣列
現在,我想從整數中提取8個半字節,以便我可以使用arr[nibble-value]
在每個半字節中得到1的數字。
int arr[16] = {0,1,1,2,1,1,2,3,1,2,2,3,2,3,3,4};
int main (void)
{
int x = 127;
int temp, sum =0, i;
int nibbles = 2 * sizeof(x);
for (i = 1; i<= nibbles; i++)
{
temp = x << (4*i); // <<< I Know this is wrong!!!! <<Here is what I need!!>>
printf("Temp[%d]:%d\n", i, temp);
sum = sum + arr[temp];
}
printf("No.of ones: %d\n", sum);
return 0;
}
可以是簡單的邏輯...
順便說一句,是你的最終目標:獲得每半字節設置的總位數? –
我只是嘗試基本的哈希表技術,所以要回答你的問題..是的! – Ram
順便說一下,設置位的數量被稱爲「人口數」或「popcount」。如果您有興趣,您可以使用該搜索字詞搜索關於其他方式的大量信息。 –