algorithm - Using c and bit shifting to solve a specific requirement -


i have 16 letter alphabet. given sentence, count frequency of each letter, , encapsulate frequencies in 1 number using clever bit shifting. lets assume sentences 100 letter each, , assuming no letter occurs more 31 times, this:

a: occurs 2 times -> 0010 b: occurs 10 times -> 1010 c: occurs 7 times -> 0111 

etc.

now, concatenation this: 001010100111...

i concentrated frequencies above. store number easily, wanted convert binary above 64 bit unsigned int.

my other requirement have long , re extract frequencies per letter. so, need able generate decimal parse individual frequency bits.

how in c? can bit shifting , additions of frequencies means i'm overlapping frequencies. other issue when extracting frequencies, how know how many bits shift since trailing 0s insignificant , not saved in decimal important in algorithm.

any clever ideas? thank you.

you have 2 problems: mathematical problem , coding problem.

let's ignore math problem moment. can build array 16 integers , count occurrences of each letter when scan text. if assume no letter occurs more 15 times, don't have worry overflow , can put counts 64-bit integer enough. you'd write:

int counts[16];  // has counts unsigned long long freqs;  // holds encoded value  // after compute counts freqs = 0; (int = 0; < 16; ++i) {     freqs <<= 4;     freqs |= (counts[i] & 0xf); } 

at point, count first letter in top 4 bits of freqs, , count last letter is bottom 4 bits. other counts in between. each 1 occupies 4 bits of 64-bit number.

now, if want ability larger text, or letter can occur more 15 times, have scale numbers after counting maximum no larger 15. that's math problem alluded to. think can figure out how handle one. have scale numbers.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -