C/c storage minimal space
Expert: Abhishek Kumar - 11/28/2011
QuestionHi,
How can i do the following in C:
in order to store the string "abcd", i may need 5 bytes( 40 bits). What i want to do is store each element 'a' or 'b' or ... in its minimal strorage form. eg. if character 'a' is represented as 97 it may take roughly( i didn't do the math) 7 bits. And considering it takes 7 bits for the other chars i need 35 bits to store the string. How can i do this? Any creativity is acceptable. THANK YOU VERY MUCH!
AnswerHi Heni,
If you see why we take 1 Byte (8 bits) space for storing a character is because ASCII characters range from 1 to 255 and hence we need 8 bits. as 255 is 11111111.
Now, question in hand is very specific, such that it can only have characters between "a-z" or "A-Z" in that case we can do alot of space reduction.
If this is the case in that case you can number them from 1 to 26 (a to z)
And (27-52) for A to Z.
And you can use 6 bits for storing them.
We can save more space if you have a condition say a character is not repeated.
In that case you only require max 52 bits that is only 6 characters.
Let me know the exact question after that we can see how much space we can save.
Regards,
Abhishek Kumar