C++ bit rotate and bitwise AND not working..

Discussion in 'Mac Programming' started by mmmdreg, May 25, 2008.

  1. mmmdreg macrumors 65816


    Apr 14, 2002
    Sydney, Australia
  2. MrStevieP macrumors newbie

    Feb 27, 2008
    Can you give any more information? Have you reassigned the item after using the AND?

    A code snippet would be handy o diagnose this problem
  3. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    He had code earlier I guess he has fixed his problem and edited out his original post.
  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    When I read the post earlier I was on my phone and didn't want to try to type everything out there.

    Essentially the issue appeared to be sign extension. The highest bit of the uint8 was a 1, so the sign got extended to the higher order bytes when cast to a 32-bit int. The result would have varied in other test cases, as the 0 sign bit extension wold have "looked different".

    Essentially, however, you should have gotten the same numerical result (though signed, not unsigned) no matter what, as in 2s complement sign extension will not modify the value. You had wanted your uint8 to be unsigned, but turning it into an int and not an unsigned int caused the issues you saw.

    It seems like you figured it out already, but wanted to toss this out there for others that might read the thread.

    In the future when you resolve something it might be nicer to leave the post and add the solution in the edit or a new post for others to learn from. I don't think it was a silly question and I'm sure others will run into something similar at some point.


Share This Page