Bit manipulation question

E

Elijah Bailey

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
 
W

wilk

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 :)
 
C

Chris Theis

Elijah Bailey said:
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?

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

HTH
Chris
 
D

Dan W.

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

//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!
 
N

Naren

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top