473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

zero the last 13 bits of an unsigned long number

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
5 1421
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
4
by: Simon | last post by:
Hi, I am reading a file that contains a bunch of unsigned longs. The number itself is broken down in bits, for example bit 31-24 is a certain code and bit 7-6 represents a certain state. am...
14
by: Ben | last post by:
Hi, I need to write some data types into an array of unsigned chars. These are basically "signals" within a message, so each signal will have a start bit and a length. The signals will also...
40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
26
by: G Patel | last post by:
Hi, I'm wondering if anyone knows if the following function will function properly as a set-bit counter on non 2s complement machines (as K&R2 implies). | int bitcount(unsigned x) | { | ...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
36
by: Digital Puer | last post by:
Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int...
15
by: Tomás | last post by:
Is the following fully legal and fully portable for all the unsigned types? The aim of the function is to take an array by reference and set each element's value to zero. #include <... ...
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.