By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,190 Members | 2,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,190 IT Pros & Developers. It's quick & easy.

Bit manipulation question

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <ge******@hotmail.com> napisal w wiadomosci
news:e0**************************@posting.google.c om...
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

Jul 22 '05 #2

P: n/a

"Elijah Bailey" <ge******@hotmail.com> wrote in message
news:e0**************************@posting.google.c om...
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
Jul 22 '05 #3

P: n/a

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.

Jul 22 '05 #4

P: n/a
On 3 Dec 2003 00:00:38 -0800, ge******@hotmail.com (Elijah Bailey)
wrote:
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!
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.