473,750 Members | 5,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extracting front bits from an unsigned long long?

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 between
0 and 7 (=2^3-1). Could someone please help out?

I can assume the largest returned value fits in an int. Also,
I'm on a big-endian PPC (AIX), in case that matters.

Ideally, I'd like to implement a prototype like:
int extractFrontBit s(unsigned long long value, int num_bits);

Nov 15 '05 #1
36 5311
Digital Puer wrote:

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 between
0 and 7 (=2^3-1). Could someone please help out?


int three_bits = long_long_value & 7;

--
pete
Nov 15 '05 #2
Digital Puer wrote:
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 between
0 and 7 (=2^3-1). Could someone please help out?

I can assume the largest returned value fits in an int. Also,
I'm on a big-endian PPC (AIX), in case that matters.

Ideally, I'd like to implement a prototype like:
int extractFrontBit s(unsigned long long value, int num_bits);


I'm guessing long long is 64 bits? Regardless, the following should
work:

frontbits = (original_value >> (sizeof(long long) - 3)) & 0x03;

The masking at the end is to zero out the msb of the result since on
some systems it doesn't get zeroed.

If number of bits is not fixed, then it would look something like the
following:

frontbits = original_value >> (sizeof(long long) - num_bits);

but the masking will get complicated.

Nov 15 '05 #3
sl*******@yahoo .com wrote:

Digital Puer wrote:
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 between
0 and 7 (=2^3-1). Could someone please help out?
I'm guessing long long is 64 bits? Regardless, the following should
work: frontbits = (original_value >> (sizeof(long long) - 3)) & 0x03;


You're getting 2 bits from the middle of the value.
Anything & 3, ain't going to get you more than 2 bits.
You shift the original value to the right
and the low order bits disappear.

--
pete
Nov 15 '05 #4
On 10 Nov 2005 16:59:21 -0800, "Digital Puer"
<di**********@h otmail.com> wrote:
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 between
0 and 7 (=2^3-1). Could someone please help out?

I can assume the largest returned value fits in an int. Also,
I'm on a big-endian PPC (AIX), in case that matters.

Ideally, I'd like to implement a prototype like:
int extractFrontBit s(unsigned long long value, int num_bits);


Shift the value in the variable to the right
(sizeof(unsigne d long long)*CHAR_BIT - n)
bits. The result is the integer you requested.
<<Remove the del for email>>
Nov 15 '05 #5
pete wrote:

Digital Puer wrote:

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 between
0 and 7 (=2^3-1). Could someone please help out?


int three_bits = long_long_value & 7;


n bits, ... hmm ...

long_long_value & ((1ULL << n) - 1)

--
pete
Nov 15 '05 #6
"Digital Puer" <di**********@h otmail.com> writes:
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 between
0 and 7 (=2^3-1). Could someone please help out?


What do you mean by "front" bits?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7

pete wrote:
sl*******@yahoo .com wrote:

Digital Puer wrote:
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 between
0 and 7 (=2^3-1). Could someone please help out?

I'm guessing long long is 64 bits? Regardless, the following should
work:

frontbits = (original_value >> (sizeof(long long) - 3)) & 0x03;


You're getting 2 bits from the middle of the value.
Anything & 3, ain't going to get you more than 2 bits.
You shift the original value to the right
and the low order bits disappear.

--
pete


Ah, sorry, it should be:

frontbits = (original_value >> (sizeof(long long) - 3)) & 0x07;

Nov 15 '05 #8
sl*******@yahoo .com wrote:

pete wrote:
sl*******@yahoo .com wrote:

Digital Puer wrote:
> 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 between
> 0 and 7 (=2^3-1). Could someone please help out?
I'm guessing long long is 64 bits?
Regardless, the following should
work:

frontbits =
(original_value >> (sizeof(long long) - 3)) & 0x03;


You're getting 2 bits from the middle of the value.
Anything & 3, ain't going to get you more than 2 bits.
You shift the original value to the right
and the low order bits disappear.

Ah, sorry, it should be:

frontbits = (original_value >> (sizeof(long long) - 3)) & 0x07;


Barry Schwarz also seems to think that shifting the original
value to the right, is the right thing to do.
I don't get it.

I'll assume you think that CHAR_BIT is 8.
That makes your 64 bit bit long long, 8 bytes in size.
8 - 3 is two, so you shift your unsigned long long
two bits to the right, which is equivalent to dividing by 4,
and then you take the 3 lower order bits.
What is that?

--
pete
Nov 15 '05 #9
Digital Puer wrote:

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 between
0 and 7 (=2^3-1). Could someone please help out?

I can assume the largest returned value fits in an int. Also,
I'm on a big-endian PPC (AIX), in case that matters.

Ideally, I'd like to implement a prototype like:
int extractFrontBit s(unsigned long long value, int num_bits);


int extractFrontBit s(unsigned long long value, int num_bits)
{
return (int)(value & ((1ULL << num_bits) - 1))
}

--
pete
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5516
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 I right in doing the following // read the unsigned long from the file
40
5712
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 welcome any improvements: i = 0; if (g && 1) i++; if (g && 2) i++;
7
4806
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,
17
1854
by: James S. Singleton | last post by:
Let S be a pointer to a bytestring of length L. I would like to extract 4 bytes from S at the location p = S + d, with 0 < d < L - 4, and store them into an unsigned int. I am looking for suggestions on how to do this 1) In portable ANSI C. 2) As efficiently as possible. 3) Taking full account of the potential data alignment and endianness issues that this action must tackle.
0
8841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9587
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9260
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8267
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6086
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4718
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4896
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.