Injecting a value to a byte/word....using bit wise operators...

S

s.subbarayan

Dear all,
I would like to know the easiest efficient way to set or inject a
particular value in the given word or byte?The problem is:

I have to implement a function which will set a value from position
"n" to "n+x" where n and x are passed dynamically,where n is start
position of the bit from which i will be setting a value and x is the
position where I will be finishing the setting.In short it looks like
this:

Suppose bits look like this:
0000000000000000-->In this word I will be asked to set a value of 0x3
from position 5 to 8 and the start and end position will be passed
dynamically(5,8 in this case) to me from a place where this function
will be called and value to be set(0x3 in this case) will also be
passed by user to me.

Whats the easiest and efficient way to do this?
I am sorry if this looks quite a school level problem,but I have found
lot of methods which are very inefficient and time consuming in terms
of execution.My approach would typically be to shift the given word
whose bits need to be altered with start position of the bit and then
end it with its destination bit and then OR it with the value
given.But I feel there should be better methods then I may be aware.

Can some one suggest me how this can be done in C also any useful
algorithms will be helpful to me even if not real program?

Looking farward to your replys and advanced thanks for the same,
Regards,
s.subbarayan
 
G

Grumble

s.subbarayan said:
I would like to know the easiest efficient way to set or inject a
particular value in the given word or byte?The problem is:

I have to implement a function which will set a value from position
"n" to "n+x" where n and x are passed dynamically,where n is start
position of the bit from which i will be setting a value and x is the
position where I will be finishing the setting.In short it looks like
this:

Suppose bits look like this:
0000000000000000-->In this word I will be asked to set a value of 0x3
from position 5 to 8 and the start and end position will be passed
dynamically(5,8 in this case) to me from a place where this function
will be called and value to be set(0x3 in this case) will also be
passed by user to me.

Set a value of 3 to bits 5-8 means

set bit 5 to 1
set bit 6 to 1
set bit 7 to 0
set bit 8 to 0

Is that correct?

If val=22, from=3, to=9 then does it mean

set bit 3 to 0
set bit 4 to 1
set bit 5 to 1
set bit 6 to 0
set bit 7 to 1
set bit 8 to 0
set bit 9 to 0

?
 
A

Alex Fraser

s.subbarayan said:
Dear all,
I would like to know the easiest efficient way to set or inject a
particular value in the given word or byte?The problem is:

I have to implement a function which will set a value from position
"n" to "n+x" where n and x are passed dynamically,where n is start
position of the bit from which i will be setting a value and x is the
position where I will be finishing the setting. [snip]
Whats the easiest and efficient way to do this?

There's really only one sensible way I know of; the basic idea is (in C):

word &= ~(((1u << length) - 1) << start_pos); /* clear affected bits */
word |= value << start_pos; /* set relevant bits */

(In C, the behaviour of shifting a value by an amount greater than or equal
to the width of the (promoted) type of the value is undefined, so watch out
for that. If you have start and end rather than start and length, the length
is probably inclusive. If bits are not numbered from zero, you'll need to
account for that.)

The first step is not necessary if all the potentially affected bits are
known to be clear. The second step assumes 'value' is within range.
I am sorry if this looks quite a school level problem,but I have found
lot of methods which are very inefficient and time consuming in terms
of execution.

Bitwise AND, bitwise complement, subtract constant, and bitwise OR translate
directly to machine instructions which execute quickly on every processor I
have come across. Shifting by a variable amount can be slow, but I don't
think there's any way to avoid it.

In other words, I don't think there's much room for improvement. Even if
there was, it would probably be rare for it to make a significant
performance difference to an application anyway.

Alex
 
P

Peter Nilsson

Alex said:
s.subbarayan said:
I would like to know the easiest efficient way to set or inject a
particular value in the given word or byte?The problem is:

I have to implement a function which will set a value from position
"n" to "n+x" where n and x are passed dynamically,where n is start
position of the bit from which i will be setting a value and x is the
position where I will be finishing the setting. [snip]
Whats the easiest and efficient way to do this?

There's really only one sensible way I know of; the basic idea is (in C):

word &= ~(((1u << length) - 1) << start_pos); /* clear affected bits */
word |= value << start_pos; /* set relevant bits */

This may not work properly if word has type unsigned long because your
mask may be too narrow.

[Also, it does assume that word is either not signed, or explicitly
twos complement. Generally, bitwise operations should be restricted
to unsigned types.]
(In C, the behaviour of shifting a value by an amount greater than or
equal to the width of the (promoted) type of the value is undefined,
so watch out for that.

You could assume that length is non-zero, since it's not possible to
put a value in 0 bits, then you can safely cater for the case where
length is the full width...

mask = 1; /* mask has type of target word */
mask = (mask << (length - 1) << 1) - 1;

word &= ~(mask << start_pos);
word |= (value & mask) << start_pos;

The (value & mask) is in case the value is outside the range of the
width specified.

There is an kludge that can force a constant like 1u to be the
suitable width for a target word with higher rank than unsigned
int, but it's pretty ugly...

#define MASK(word, length) \
( ((word) - (word) + 1u) << ((length) - 1) << 1 )

word &= ~(MASK(word, length) << start_pos);
word |= (value & MASK(word, length)) << start_pos);

Most compilers will optimise the 'word-word' sub-expression, but since
the compiler still needs to implement the semantics, i.e. implicit type
promotion, the mask will have a suitable width.
... Shifting by a variable amount can be slow, but I don't
think there's any way to avoid it.

Even if it isn't, the compiler is in the best position to optimise
it.
 
S

s.subbarayan

Grumble said:
Set a value of 3 to bits 5-8 means

set bit 5 to 1
set bit 6 to 1
set bit 7 to 0
set bit 8 to 0

Is that correct?

If val=22, from=3, to=9 then does it mean

set bit 3 to 0
set bit 4 to 1
set bit 5 to 1
set bit 6 to 0
set bit 7 to 1
set bit 8 to 0
set bit 9 to 0

?

Yes you are correct.This is what I was also looking for to understand
how it can be done programmatically given that all the 3 things
required .,viz.,start position,stop position and value to be set are
user defined and passed in to a function which implements the above
problem?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top