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


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 (, but how do i extract the last 20 bits (or some number of bits) ... thanks
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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
zzynxSr. Software 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

zzynxSr. Software engineerCommented:
The 0xFF is the lowest 16 bits
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

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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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).
zzynxSr. Software engineerCommented:
Thanx 4 axxepting.
100 points for just repeating me. Good deal. ;-)
AndyAinscowFreelance programmer / ConsultantCommented:

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.
zzynxSr. Software engineerCommented:
... if you are really unhappy ...
Not "really"
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).
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

From novice to tech pro — start learning today.