Connecting Tech Pros Worldwide Help | Site Map

Bit manipulation question

Elijah Bailey
Guest
 
Posts: n/a
#1: Jul 22 '05
I have a long x;
I want to write a function

long f(long x, int k)

such that it extracts every k-th bit of x, concatenates
them and returns it. Anyone can help me in writing this
function?

examples
x = 10101010 k = 1 f(x) = 10101010
x = 10101010 k = 2 f(x) = 1111
x = 10101010 k = 3 f(x) = 010
x = 10101010 k = 4 f(x) = 11

Any bit gurus here who can help me?

Thanks,
--Elijah
wilk
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Bit manipulation question


I think you made a mistake. It should be:
x=10101010 k=3 f(x)=101
not f(x)=010
Am I wrong ??

You can change the x to string useing div and mod, then choose the k-th bits
and that change it back to long. It's of course slow , but .... it's
something :/
For now I can't see any solution with &,| or xor because of cancatenating.
And all this operations leave unnecessary zeroes in results.

PS. First I write to show you mistake and that I wrote this useless help,
sorry :)
--
Sorry for mistakes.

Uzytkownik "Elijah Bailey" <geomrock@hotmail.com> napisal w wiadomosci
news:e008fef8.0312030000.504c368d@posting.google.c om...[color=blue]
> I have a long x;
> I want to write a function
>
> long f(long x, int k)
>
> such that it extracts every k-th bit of x, concatenates
> them and returns it. Anyone can help me in writing this
> function?
>
> examples
> x = 10101010 k = 1 f(x) = 10101010
> x = 10101010 k = 2 f(x) = 1111
> x = 10101010 k = 3 f(x) = 010
> x = 10101010 k = 4 f(x) = 11
>
> Any bit gurus here who can help me?
>
> Thanks,
> --Elijah[/color]


Chris Theis
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Bit manipulation question



"Elijah Bailey" <geomrock@hotmail.com> wrote in message
news:e008fef8.0312030000.504c368d@posting.google.c om...[color=blue]
> I have a long x;
> I want to write a function
>
> long f(long x, int k)
>
> such that it extracts every k-th bit of x, concatenates
> them and returns it. Anyone can help me in writing this
> function?
>
> examples
> x = 10101010 k = 1 f(x) = 10101010
> x = 10101010 k = 2 f(x) = 1111
> x = 10101010 k = 3 f(x) = 010
> x = 10101010 k = 4 f(x) = 11
>
> Any bit gurus here who can help me?[/color]

Take a look at the standard library's bitset<> class. There you'll find
everything you need.

HTH
Chris


Naren
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Bit manipulation question



I don't know about how good it is but it works.
#include<iostream>
int main()
{
int X = 170;
int k = 2;
int c = 0;

int n = 1 << (k-1);
int cnt = 1;
while(X)
{
c += cnt*((X&n)?1:0);
X >>= k;
cnt <<= 1;
}

cout << c;
return 0;
}

I think you can tailor this to your requirements.

Hope this helps.

Best Regards,
Naren.



Dan W.
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Bit manipulation question


On 3 Dec 2003 00:00:38 -0800, geomrock@hotmail.com (Elijah Bailey)
wrote:
[color=blue]
>I have a long x;
>I want to write a function
>
>long f(long x, int k)
>
>such that it extracts every k-th bit of x, concatenates
>them and returns it. Anyone can help me in writing this
>function?
>
>examples
> x = 10101010 k = 1 f(x) = 10101010
> x = 10101010 k = 2 f(x) = 1111
> x = 10101010 k = 3 f(x) = 010
> x = 10101010 k = 4 f(x) = 11
>
>Any bit gurus here who can help me?
>
>Thanks,
>--Elijah[/color]

//you didn't say 'unsigned long' so I'll assume the
//msb is zero

long f( long x, int k)
{
assert( x >= 0 );
assert( k >= 0 );
assert( k <= 30 );
int inbitndx = 0;
postshifts = 31-31%k;
assert( postshifts < 31 );
assert( postshifts >= 0 );
//result must be unsigned to prevent
//right shift from leaving msb set...
unsigned long result = 0;
while( inbitndx < 31 )
{
mask = 1 << ( inbitndx );
assert( mask != 0 );
assert( mask != 0x80000000;
if( x & mask ) result |= 0x80000000;
result >> 1;
inbitndx += k;
}
result >>= postshifts;
assert( result == x || k != 1 );
return result;
}

Magic numbers should be replaced by
portable expressions; I wrote them in a
simple way to make the code clear.
Probably needs debugging ;-)
There are non-portable bit manipulation
instructions one can use (non-portably)
in assembler, but here one speaks C++.
Ask your question in an assembler
newsgroup, if you need speed.

Cheers!


Closed Thread


Similar C / C++ bytes