extract "set" bits including position from unsigned long

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 example, consider this
four-bit number:
1100
from this number I want to extract two numbers:
1000 and 100
had the four-bit number been 0101 I would want to extract 100 and 1.

How should I do this? I wish I had some code to post but I don't right now
=/

Advertisements

"William Payne" <> wrote in message
news:cga4eq$8bp$...
> 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 example, consider
> this four-bit number:
> 1100
> from this number I want to extract two numbers:
> 1000 and 100
> had the four-bit number been 0101 I would want to extract 100 and 1.
>
> How should I do this? I wish I had some code to post but I don't right now
> =/
>
> These new numbers I will use to look-up strings in a map.
>
> / WP
>

Advertisements

"William Payne" <> wrote in message
news:cga5de$8g1$...
>
> "William Payne" <> wrote in message
> news:cga4eq$8bp$...
>> 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 example, consider
>> this four-bit number:
>> 1100
>> from this number I want to extract two numbers:
>> 1000 and 100
>> had the four-bit number been 0101 I would want to extract 100 and 1.
>>
>> How should I do this? I wish I had some code to post but I don't right
>> now =/
>>
>> These new numbers I will use to look-up strings in a map.
>>
>> / WP
>>
>
> Will this work?
> for(unsigned long i = 1; i <= sizeof(unsigned long) / 8; i+=i)
> {
> if(i & styles)
> {
> map<unsigned long, string>::const_iterator pos;
>
> pos = styles_map.find(i);
>
> if(pos != styles_map.end())
> {
> cout << pos->second << endl;
> }
> }
> }
>
> styles is of type unsigned long and styles_map is map<unsigned long,
> string>
>
> / WP
>

"William Payne" <> wrote in message
news:cga4eq$8bp$...
> 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 example, consider this
> four-bit number:
> 1100
> from this number I want to extract two numbers:
> 1000 and 100
> had the four-bit number been 0101 I would want to extract 100 and 1.
>
> How should I do this? I wish I had some code to post but I don't right now
> =/
>
> These new numbers I will use to look-up strings in a map.
>
> / WP

Bit 31 is not set
Bit 30 is not set
Bit 29 is not set
Bit 28 is set
Bit 27 is not set
Bit 26 is not set
Bit 25 is set
Bit 24 is set
Bit 23 is set
Bit 22 is not set
Bit 21 is set
Bit 20 is set
Bit 19 is not set
Bit 18 is not set
Bit 17 is not set
Bit 16 is set
Bit 15 is not set
Bit 14 is not set
Bit 13 is set
Bit 12 is set
Bit 11 is set
Bit 10 is not set
Bit 9 is set
Bit 8 is set
Bit 7 is not set
Bit 6 is not set
Bit 5 is not set
Bit 4 is not set
Bit 3 is set
Bit 2 is set
Bit 1 is set
Bit 0 is not set

Bit 31 is not set
Bit 30 is not set
Bit 29 is not set
Bit 28 is set
Bit 27 is not set
Bit 26 is not set
Bit 25 is set
Bit 24 is set
Bit 23 is set
Bit 22 is not set
Bit 21 is set
Bit 20 is set
Bit 19 is not set
Bit 18 is not set
Bit 17 is not set
Bit 16 is set
Bit 15 is not set
Bit 14 is not set
Bit 13 is set
Bit 12 is set
Bit 11 is set
Bit 10 is not set
Bit 9 is set
Bit 8 is set
Bit 7 is not set
Bit 6 is not set
Bit 5 is not set
Bit 4 is not set
Bit 3 is set
Bit 2 is set
Bit 1 is set
Bit 0 is set

Bit 31 is not set
Bit 30 is not set
Bit 29 is not set
Bit 28 is set
Bit 27 is not set
Bit 26 is not set
Bit 25 is set
Bit 24 is set
Bit 23 is set
Bit 22 is not set
Bit 21 is set
Bit 20 is set
Bit 19 is not set
Bit 18 is not set
Bit 17 is not set
Bit 16 is set
Bit 15 is not set
Bit 14 is not set
Bit 13 is set
Bit 12 is set
Bit 11 is set
Bit 10 is not set
Bit 9 is set
Bit 8 is set
Bit 7 is not set
Bit 6 is not set
Bit 5 is not set
Bit 4 is set
Bit 3 is not set
Bit 2 is not set
Bit 1 is not set
Bit 0 is not set

"William Payne" <> wrote:
> 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 example, consider this
> four-bit number:
> 1100
> from this number I want to extract two numbers:
> 1000 and 100
> had the four-bit number been 0101 I would want to extract 100 and 1.

William Payne wrote:
> 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 example, consider this
> four-bit number:
> 1100
> from this number I want to extract two numbers:
> 1000 and 100
> had the four-bit number been 0101 I would want to extract 100 and 1.
>
> How should I do this? I wish I had some code to post but I don't right now
> =/
>
> These new numbers I will use to look-up strings in a map.
>
> / WP
>
>

The solutions that I've seen posted so far are all O(N) in the number of
bits in an unsigned long. That stinks.

Here's a pair of skeleton solutions that's linear in the number of SET
bits instead of the total number of bits. Use whichever you prefer.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!