Bits and Addresses

This is a discussion on Bits and Addresses within the C++ Programming forums, part of the General Programming Boards category; I'm writing a memory management program/simulation. Let's say I have a 32-bit pointer. I want it to represent a virtual ...

Bits and Addresses

I'm writing a memory management program/simulation. Let's say I have a 32-bit pointer. I want it to represent a virtual address. Part of that address, let's say the upper 22 is the virtual page number, and the lower 10 is the page offset. How do I do bit selection in C++? How do I say something like:
int upper = upper22bits(pointer);
int lower = lower 10bits(pointer);

Thanks

I seem to remember messing around with shifts and masking but I don't really remember how.
For example if I want the upper 22 its, I think I'd right shift 10, & with enough 1's to cover the bits (2^22-1 I think) and I'd have a number represented by the upper 22 bits.
If I want the lower 10 bits I think I don't have to shift, I just & with enough 1's to cover the bits (2^10-1 I think).
I'm not sure if this is correct

2^3-1 is 0. However, the & will happen first (it has higher precedence that ^, see your Order of Operations)
The bigger issue, however, I suspect that ^ is not what you're expecting it to be. ^ is binary XOR, not exponent. (There is no C operator for exponent.)