473,405 Members | 2,272 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,405 software developers and data experts.

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

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
Nov 14 '05 #1
4 1829
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

?
Nov 14 '05 #2
"s.subbarayan" <s_**********@rediffmail.com> wrote in message
news:c3**************************@posting.google.c om...
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
Nov 14 '05 #3
Alex Fraser wrote:
"s.subbarayan" <s_**********@rediffmail.com> 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
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.

--
Peter

Nov 14 '05 #4
Grumble <de*****@kma.eu.org> wrote in message news:<d3**********@news-rocq.inria.fr>...
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?
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
17
by: George Sakkis | last post by:
Is there a general way of injecting code into a function, typically before and/or after the existing code ? I know that for most purposes, an OO solution, such as the template pattern, is a cleaner...
9
by: mprocopio | last post by:
Fellow JavaScripters, I am looking for code or implementation ideas for converting an integer variable to a four-byte array. I'm porting some of my code from C#, where I make use of their...
5
by: Nadav | last post by:
Hi, Introduction: ************************************************************ I am working on a project that should encrypt PE files ( Portable executable ), this require me to inject some...
17
by: Luke Matuszewski | last post by:
Hi ! Simple question (but thus it may appear no simple answer): If i putting a script onto the page i simply could inline it in <script> element or via its src attribute so (second way): <script...
9
by: John | last post by:
I'm sorry if this is sounding like somewhat of a noob question. I'm loading in a large binary array of 8x8 double precision floating point matrices, right now this is defined something like ...
7
by: gene kelley | last post by:
I have an application where I need to read some header information found in 3 different types of file types. Two of the file types were fairly straight forward as the items to read in the header...
14
by: ofiras | last post by:
Hii everyone, I'm a web programmer, but I never understood sql injecting. All I found was that you can write "a' or 'a'='a" in the password field to try to connect without knowing the password. I...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.