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