Q:

C++ - Using Bitmask to get values from an int

I'm given a 32 bit number rendered as an int in the original file. I need the rightmost 8 bits (0-7) in one value and I need the next rightmost 8 bits (8-15) in another. I was told to use bit-masking and bit-shifting to sort that all out but I don't know how it works with integers.

A:

To get only the 8 rightmost bits, you can do
int right8 = mynum & 0xFF;

Because you are anding the top 24 bits with 0, these will be set to 0, while the bottom 8 will retain their values as they will be anded with ones.
In order to get the next 8 rightmost, you will need to set bits 8-15 to one and all the other to zero in your bitmask. This will maintain the same bits in positions 8-15, but since you need them to be in the bottom 8 bits, you can simply bit shift them 8 positions to the right.  You can do that with the following.
int next8 = myNum & 0xFF00;
int next8 = next8 >> 8;

