# 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
###### Who is Participating?

x
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.

Freelance 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
Sr. Software engineerCommented:
Out of my head (not tested):

``````unsigned n = ...;
for (int i=0; i<numberOfBits; i++) {
}
unsigned lowXbits = n & bitMaskForXBits;
``````
Sr. Software 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

Experts Exchange Solution brought to you by

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

Freelance 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).
Sr. Software engineerCommented:
Thanx 4 axxepting.
100 points for just repeating me. Good deal. ;-)
Freelance 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.
Sr. Software engineerCommented:
... if you are really unhappy ...
Not "really"
Freelance 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
C#

From novice to tech pro — start learning today.