Packing 2 bit numbers into an int.

I have an array of ints where each int represents 16 blocks, I am attempting to set and read groups of 2 bits within the int as their own numbers, but am getting numbers I did not put in. (ex. i write a 2 in but it gives me a 3 when reading) This is how I'm setting the values: (key is the new value)
int intPos = pos / 16;
int whereInInt = pos - (intPos * 16);
blockData[intPos] |= (key & 0x3) << (whereInInt*2);
int intPos = pos / 16;
int whereInInt = pos - (intPos * 16);
blockData[intPos] |= (key & 0x3) << (whereInInt*2);
This is how I'm getting the values:
int intPos = pos / 16;
int values = blockData[intPos];
int whereInInt = pos - (intPos * 16);
return (values >> (whereInInt*2)) & 0x3;
int intPos = pos / 16;
int values = blockData[intPos];
int whereInInt = pos - (intPos * 16);
return (values >> (whereInInt*2)) & 0x3;
1 Reply
JavaBot
JavaBotā€¢5d ago
āŒ› This post has been reserved for your question.
Hey @š“pothicon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?