473,379 Members | 1,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Bit manipulation question

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
4 4035
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

"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robert Kern | last post by:
I'm in the middle of writing a scientific program with a number of very, very long formulae. You can see a typical one below. What I would like to do is to parse this code to yield an AST, identify...
9
by: I. Kobrinsky | last post by:
I'm new here. I started a personal password-program, a trial that includes username, logincounter and password. So my intention is to hide pwd while tipping. So I'm thinking about two popular...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
0
by: Wilfried Mestdagh | last post by:
Hi, I have a couple of dataGridView manipulation question: 1. The datetime is not displayed in the way I have set it in my system, how to set it the way my system is configured ? 2. I have...
3
by: Bartholomew Simpson | last post by:
I need to pack two small values into a double data type. I have come up with the following struct, which only takes up 16 bits and can therefore be stored safely in a double. struct mytype_ {...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
1
by: Rhadamanthys | last post by:
Hello All I am a relative beginner to SQL databases & new to this forum, so please bear with me if my query is too basic and advise if this question belongs somewhere else I began working at a...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.