HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free and registered users see less advertising! If you just want to browse through the existing questions, just select the forum that you want to visit from the selection below. Otherwise, click here to register!. We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Re:Doing stuff to bits in C++??

I thought there was an easy way to shift bits in C++. I may be thinking of Java or something. But anyway, here are the ways I would do the equivelant assembler functions. n is how many bits to shift or rotate.

shl:
Multiply by 2 ^ n.

shr:
Divide by 2 ^ n.

rol:
Multiply by 2. Capture overflow error. (Is there a way to do this in C++? It may be necessary to use typecasting to save the results in a larger data type and test the value of the new number to see if it's greater than what your old data type would allow instead.) Add 1 if overflow. Do this n times.

ror:
Divide by 2. Modulus (the % operator) the old number by 2. Set the first bit of new number if remainder. (You can do this by adding or oring. It may be more convenient to do the modulus operation first.)