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

Discussion in 'C Programming' started by s.subbarayan, Apr 5, 2005.

1. ### s.subbarayanGuest

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?

Regards,
s.subbarayan

s.subbarayan, Apr 5, 2005

2. ### GrumbleGuest

s.subbarayan wrote:

> 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

?

Grumble, Apr 6, 2005

3. ### Alex FraserGuest

"s.subbarayan" <> wrote in message
news:...
> 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

Alex Fraser, Apr 6, 2005
4. ### Peter NilssonGuest

Alex Fraser wrote:
> "s.subbarayan" <> wrote...
> > 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

[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...

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...

( ((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.

--
Peter

Peter Nilsson, Apr 7, 2005
5. ### s.subbarayanGuest

Grumble <> wrote in message news:<d304a0\$i5v\$>...
> s.subbarayan wrote:
>
> > 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
>
> ?

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?

s.subbarayan, Apr 7, 2005