Solved

Problem with Bit Shifting to isolate bits

Posted on 2004-10-04
1
380 Views
Last Modified: 2010-05-18
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;
      cout<<hex<<temp<<endl;
      temp=Get_Bits(temp,5,5);
      cout<<temp<<endl;
      return 0;
}
unsigned int Get_Bits(unsigned int instruction, int lower, int upper)
{
      int left, right;
      for (left=0;left<(15-upper);left++)
      {
            instruction=instruction<<1;
            cout<<"<<"<<instruction<<endl;
      }
      for (right=0;right<(lower+left);right++)
      {
            instruction=instruction>>1;
            cout<<">>"<<instruction<<endl;
      }
      return instruction;
}

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.

1ff3
<<3fe6
<<7fcc
<<ff98
<<1ff30
<<3fe60
<<7fcc0
<<ff980
<<1ff300
<<3fe600
<<7fcc00
>>3fe600
>>1ff300
>>ff980
>>7fcc0
>>3fe60
>>1ff30
>>ff98
>>7fcc
>>3fe6
>>1ff3
>>ff9
>>7fc
>>3fe
>>1ff
>>ff
ff

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.
0
Comment
Question by:drumrboy44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12223247
You don't need to shift to 'clear' some undesired bits, just use and operator.
I will describe you pseudo code:

1) Construct a mask. By example if you want bits 4 to 9, your mask will be 000000000000001111110000
2) Apply the mask to the original number by example to:  00001111000011110000111100001111
    Result will be (I hope): 00000000000000000000001100000000
3) Now shift to the right using the lower value (4 in this example)
    Result will be (I hope again): 00000000000000000000000000110000
4) Your desired bits are now in first 6 bits.

To build your mask (1):
int mask = (1<<(high-low+1))-1)<<low;

To apply the mask and shift (2) and (3):
int desiredBits = (myNumber & mask)>>low;

So, if you join all:
int desiredBits = (myNumber & ((1<<(high-low+1))-1)<<low))>>low;

I guess it can be reduce to:
int desiredBits = (myNumber>>low) & (1<<(high-low+1))-1);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linker errors gone with a clean build 2 63
Visual Studio 2010 There is no editor available for ... 8 99
Convert money to nchar 4 85
Need some help with design 17 48
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question