How to extract the last 20 bits or x bits from an unsigned int (32 bits)

Hi,

say, i have i an unsigned int (32 bits) and i want to extract the last x bits (e.g. the last 20 bits). i found a site that shows how to do the last 8 bits (http://stackoverflow.com/questions/3270307/how-do-i-get-the-lower-8-bits-of-int), but how do i extract the last 20 bits (or some number of bits) ... thanks
zizi21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
In exactly the same way.  The 0xFF is the lowest 16 bits which is the same as 0x00FF, so apply a little binary knowledge and the 20 bits should be with 0x07FF
0
zzynxSoftware engineerCommented:
Out of my head (not tested):

unsigned n = ...;
unsigned bitMaskForXBits = 0;
int numberOfBits = 20; // <<<<< Adapt according to your needs
for (int i=0; i<numberOfBits; i++) {
    bitMaskForXBits += (1<<i);
}
unsigned lowXbits = n & bitMaskForXBits;

Open in new window

0
zzynxSoftware engineerCommented:
The 0xFF is the lowest 16 bits
Wrong.
0xFF is the lowest 8 bits (as the linked article says)

00 00 00 0F = 1 1 1 1 (4 bits)
00 00 00 FF = 1 1 1 1 1 1 1 1 (8 bits)
00 00 FF FF = 16 bits
00 0F FF FF = 20 bits
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

AndyAinscowFreelance programmer / ConsultantCommented:
duh, I was distracted.  It is the lowest 8 but the principle is the same.  0x000FFFFF (not 0x07FF).  You need the binary number 11111111111111111111 converted to hex (and I hope I counted correctly this time - there should be 20 ones there).
0
zzynxSoftware engineerCommented:
Thanx 4 axxepting.
100 points for just repeating me. Good deal. ;-)
0
AndyAinscowFreelance programmer / ConsultantCommented:
@zzynx

We don't know just what the 100pts was for.  Was it for the 0xFFFFF or was it for the principle being the same (a repeat of my first comment - incidentally the very first comment to the question) ?

ps.  Hit the request attention if you are really unhappy, I'll abide by what a moderator decides.
0
zzynxSoftware engineerCommented:
... if you are really unhappy ...
Not "really"
0
AndyAinscowFreelance programmer / ConsultantCommented:
The link actually provides general techniques for any number of bits and how to generate the mask required.  One *could* argue my very first comment saying just use the techniques there is a complete answer (ignoring my cock up with the mask as irrelevant).  I'm not going to argue that, there isn't any point to doing that and I did make a mistake in part of the comment which does spoil it somewhat.

Look.  Both of us provided comments the asker viewed to be useful and did award you 4 times the points as they did me - so it wasn't just give each 250 points to close the question without thinking what is appropriate (or just accepting the final comment even if totally irrelevant and by an expert that didn't contribute anything useful).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.