By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

zero the last 13 bits of an unsigned long number

P: n/a
Hi guys,

I have some value stored in an unsigned long integer. My
requirement is to
zero the 13 LSB bits of this number. Or more specifically, I want to
make the number
8k (8192) aligned. For instance, if the value is 26 kilobytes I should
get 24kilobytes.
Can somebody tell me a portable way of doing it. I thought of
using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
some other
machine. I also thought of right shifting the number by 13 bits and
again left shifting it
by 13. But, I don't know if this is a good way of soing it.
Can anyone sugggest me a cleaner way of doing it ?

thanks a lot for any help ...

Jun 27 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ju**********@yahoo.co.in wrote:
Hi guys,

I have some value stored in an unsigned long integer. My
requirement is to
zero the 13 LSB bits of this number. Or more specifically, I want to
make the number
8k (8192) aligned. For instance, if the value is 26 kilobytes I should
get 24kilobytes.
Can somebody tell me a portable way of doing it. I thought of
using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
some other
machine. I also thought of right shifting the number by 13 bits and
again left shifting it
by 13. But, I don't know if this is a good way of soing it.
Can anyone sugggest me a cleaner way of doing it ?


#include <limits.h>
unsigned long mask_lsb13(unsigned long x) {
return x & (ULONG_MAX << 13);
}

S.
Jun 27 '06 #2

P: n/a

ju**********@yahoo.co.in wrote:
Hi guys,

I have some value stored in an unsigned long integer. My
requirement is to
zero the 13 LSB bits of this number. Or more specifically, I want to
make the number
8k (8192) aligned. For instance, if the value is 26 kilobytes I should
get 24kilobytes.
Can somebody tell me a portable way of doing it. I thought of
using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
some other
machine. I also thought of right shifting the number by 13 bits and
again left shifting it
by 13. But, I don't know if this is a good way of soing it.
Can anyone sugggest me a cleaner way of doing it ?


Um what about

x &= ~((1UL<<13) - 1UL)

btw the mask should be 0xFFFFE000 for 32-bit platforms :-)

Tom

Jun 27 '06 #3

P: n/a
<ju**********@yahoo.co.in> wrote:
I have some value stored in an unsigned long integer. My
requirement is to
zero the 13 LSB bits of this number. Or more specifically, I want to
make the number
8k (8192) aligned. For instance, if the value is 26 kilobytes I should
get 24kilobytes.
Can somebody tell me a portable way of doing it. I thought of
using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
some other
machine. I also thought of right shifting the number by 13 bits and
again left shifting it
by 13. But, I don't know if this is a good way of soing it.
Can anyone sugggest me a cleaner way of doing it ?


unsigned long mask = ~0x1fffUL;
Jun 27 '06 #4

P: n/a
On Tue, 27 Jun 2006 07:35:46 -0700, ju**********@yahoo.co.in wrote:
Hi guys,

I have some value stored in an unsigned long integer. My
requirement is to
zero the 13 LSB bits of this number. Or more specifically, I want to
make the number
8k (8192) aligned. For instance, if the value is 26 kilobytes I should
get 24kilobytes.
Can somebody tell me a portable way of doing it. I thought of
using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
some other
machine. I also thought of right shifting the number by 13 bits and
again left shifting it
by 13. But, I don't know if this is a good way of soing it.
Can anyone sugggest me a cleaner way of doing it ?

thanks a lot for any help ...

It's conceivable, I suppose, that you might save a femtogrunt by twiddling
bits, but I think arithmetic is clearer:
size -= size % 8192;
(and a good optimiser may well implement that as bit twiddling)
Duncan
Jun 27 '06 #5

P: n/a

Duncan Muirhead wrote:
On Tue, 27 Jun 2006 07:35:46 -0700, ju**********@yahoo.co.in wrote:
Hi guys,

I have some value stored in an unsigned long integer. My
requirement is to
zero the 13 LSB bits of this number. Or more specifically, I want to
make the number
8k (8192) aligned. For instance, if the value is 26 kilobytes I should
get 24kilobytes.
Can somebody tell me a portable way of doing it. I thought of
using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
some other
machine. I also thought of right shifting the number by 13 bits and
again left shifting it
by 13. But, I don't know if this is a good way of soing it.
Can anyone sugggest me a cleaner way of doing it ?

thanks a lot for any help ...

It's conceivable, I suppose, that you might save a femtogrunt by twiddling
bits, but I think arithmetic is clearer:
size -= size % 8192;
(and a good optimiser may well implement that as bit twiddling)


thanks Dunkan. This really looks great ...

Jun 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.