Bit manipulation

K

Kapil Khosla

Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

I am not able to figure this one out.
Can you help ?
Thanks,
kapil
 
J

Jack Klein

Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

I am not able to figure this one out.
Can you help ?
Thanks,
kapil

The general rule around here is that we don't write people's code for
them, unless they show us that they have made some effort at doing it
themselves first.

This is actually rather tricky to do in a completely standard,
portable way, but quite simple if you limit portability to
implementations that have no padding bits in their unsigned integer
types.

In any case, I have a short function that will work on any platform
that meets the limitation (not much of a limitation) above.

So you show us yours and I'll show you mine.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

Jeff

Kapil Khosla said:
Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

Something like that.

long myfunc(long var, int pos, int size)
{
long mask = 0;
long tmp;
int i;

for(i=pos; i>pos-size; i--)
{
tmp = 1 << (i - 1);
mask = mask | tmp;

}

return var ^ mask;

}
 
D

Dave Thompson

void myfunc () { puts ("Hey! Nice bits!"); }

You meant 'complement'. Or, as correct and easier to spell, 'invert'.
Something like that.

long myfunc(long var, int pos, int size)
{
long mask = 0;
long tmp;
int i;
It's generally best to use unsigned types for bitwise values and
operations.
for(i=pos; i>pos-size; i--)
{
tmp = 1 << (i - 1);
mask = mask | tmp;
Must be 1L /* or better 1UL as I said */ << (i - 1) if any of the
masks to be computed don't fit in int but do in long (which is only
possible of course if long is wider than int).
And, mask |= tmp /* or the expression */ is more idiomatic.
(Much) better just /* mask = */ ~(~0UL << size) << (pos-size-1) .
As long as size is strictly less than the width of unsigned long; if
it may not be, either add a special case or use <<1<<(size-1)
(assuming it must be > 0, as is presumably true here) which can be
optimized(?) to ~1UL<<(size-1).

Or possibly substitute ~(~0UL<<size) by a precomputed table.
return var ^ mask;

}

If this was homework hopefully my reply will be late enough not to
help, not to mention that the OP is apparently lame enough to have no
chance of passing any serious course anyway.

- David.Thompson1 at worldnet.att.net
 

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