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

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

  1. s.subbarayan

    s.subbarayan Guest

    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
     
    s.subbarayan, Apr 5, 2005
    #1
    1. Advertising

  2. s.subbarayan

    Grumble Guest

    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
    #2
    1. Advertising

  3. s.subbarayan

    Alex Fraser Guest

    "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
    #3
  4. 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
    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
     
    Peter Nilsson, Apr 7, 2005
    #4
  5. s.subbarayan

    s.subbarayan Guest

    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
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrinalroy

    Bit wise shift operators

    mrinalroy, Oct 20, 2006, in forum: Java
    Replies:
    3
    Views:
    468
    IchBin
    Oct 20, 2006
  2. purifier

    Re: How can i compare bit wise?

    purifier, Jan 19, 2005, in forum: C Programming
    Replies:
    10
    Views:
    537
    Default User
    Jan 20, 2005
  3. Enrique Cruiz

    Row-wise vs. column-wise image processing

    Enrique Cruiz, Jan 25, 2007, in forum: C Programming
    Replies:
    10
    Views:
    744
    christian.bau
    Jan 26, 2007
  4. Enrique Cruiz
    Replies:
    5
    Views:
    464
    Jim Langston
    Jan 25, 2007
  5. Michele Dondi
    Replies:
    46
    Views:
    381
    Mirco Wahab
    Sep 19, 2007
Loading...

Share This Page