Storing bits in a container

Started by
12 comments, last by Komal Shashank 9 years ago


As per what you explained above, am I to understand that the code variable is a binary integer? In that case, can I declare it as an int type and perform the left shift operation given? Just to clarify, shift operations can be done on int types, right?

Yes this should work well.

And ofcourse you need to remember or calculate how many bits are actually used for the code, as the int will always be the same number of bits in total (usually 32). And every time you shift left, one more bit is used.

Advertisement


And ofcourse you need to remember or calculate how many bits are actually used for the code, as the int will always be the same number of bits in total (usually 32). And every time you shift left, one more bit is used.

Hi, Erik. Thanks for replying. If you don't mind me asking, can you elaborate on what you mean here? I didn't understand what you said here. Specifically, the 32 bit part. Thank you.

This is about how integers work with bits in the computer. If you read up on bitwise operations and integers it should become clear.

When using an "int" for example, a certain number of bits is allocated by the compiler, it's not dynamic, so assigning 0 to the number uses the same number of bits as assigning 9999999.

It's usually irrelevant, but as you need to store your codes with a certain number of bits you need to be able to at some point pick out the specific bits actually required.

OK... Now I understood what you meant. But I'm not sure how I would do this. Since I have the bit lengths for each code and the length will be same for each new corresponding canonical code, I was thinking I could convert resulting integer to binary and truncate it to that length. Correct me if I'm wrong, is this a good way of doing it?

Also, just to be clear, the code variable in the pseudo code is the new canonical code, right? And this is declared as integer? Because if these assumptions of mine are right, then I was thinking about doing the aforementioned method. Please clarify. Thank you.

This topic is closed to new replies.

Advertisement