3

I've got a series of bits, represented by pointers.

Bit* bits[32];

Now I need to implement the binary right rotate operator. I've got that the first part is a right shift of the same amount, which deals with all the bits which did not wrap around, but I'm not sure what to do about the ones which do wrap around.

How can I determine which bits end up in which positions in the end results?

So far, I've got

ExpressionHolder rotate(unsigned int other) const {
    ExpressionHolder out;
    out.e = new Expression;
    for(int i = 0; i < (32 - other); i++) {
        out.e->bits[i] = e->bits[i + other];
    }  
    for(int i = (32 - other); i < 32; i++) {
        out.e->bits[i] = e->bits[31 - i];
    }
    return out;
}

Is this correct?

2
  • 2
    You continue to surprise me with the occasional question that I'm sure you could have figured out yourself. Just feeling lazy today? Commented May 29, 2012 at 19:08
  • @BenVoigt: I distrust any intuition where the type system won't catch a mistake and I won't even find out if there is a mistake until I depend upon the results. I behave the same way with algorithms on float - no matter what shit you write, you always get a float back out and if the answer isn't what it should be, good luck working out why. Commented May 29, 2012 at 19:28

2 Answers 2

4

std::rotate_copy should do the trick

ExpressionHolder rotate( ExpressionHolder const& in, std::size_t count )
{
  ExpressionHolder out;
  out.e = new Expression();

  std::rotate_copy( in.e->bit, in.e->bit + 32 - count, in.e->bit + 32, out.e->bit );
  return out;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Quantum mechanics dictates that bits are always in a superposition of one and zero. So we can never be sure of the actual bit value of your pointer until we look at *e.bits[i].

But then after we look a straightforward

for(int i = 0; i < 32; i++)
     out.e->bits[i] = e->bits[(i + other) %32];

should do the trick.

1 Comment

Nice QM reference, but bits[i] only represents a bit. It is not an actual bit and does not have a defined value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.