Problem with Bit Shifting to isolate bits
Posted on 2004-10-04
I'm using Visual Studio.NET specifically the C++ compiler, and I'm writing a mini-compiler of my own. I read in 16 bit numbers, and I need to break them down (isolate certain groups of bits).
I wrote a Get_Bits function (just now) that I will paste below. My problem is that when I do a left shift, instead of deleting the bits shifted out of the 16 bit scope, the integer simply gets extended. I need those bits to be deleted so I can extract certain sections, such as bit 6-8 or something like that.
int _tmain(int argc, _TCHAR* argv)
unsigned int temp;
temp = 0x1FF3;
unsigned int Get_Bits(unsigned int instruction, int lower, int upper)
int left, right;
With the value I have assigned to temp in my main, and the parameters entered into the call to Get_Bits, I would like to clear everything but the fifth bit from the right (start counting with zero). Instead this is my resulting output.
I need some way to cap the unsigned int at 16 bits, so that the shifting will indeed just clear bits. Or if you have suggestions as to how I can do this more easily, I would appreciate those as well.