Set bits

Discussion in 'C Programming' started by s, Oct 9, 2003.

  1. s

    s Guest

    If I have:

    unsigned char value = 0xBD;
    unsigned char 2bits = 0x02;

    How do I set the two MSB in value to the two LSB in 2bits without
    changing any other bits in value?

    <mytry>
    value &= 0x3F; //set those two bits to zero
    value |= (2bits<<6);
    </mytry>

    Do I have to clear(or set) those two bits first?
     
    s, Oct 9, 2003
    #1
    1. Advertising

  2. s

    Frank Roland Guest

    "s" <youshouldbe@home> schrieb im Newsbeitrag
    news:<3F85BE28.1020901@home>...

    > If I have:


    >


    > unsigned char value = 0xBD;


    > unsigned char 2bits = 0x02;


    >


    Your identifiert may not start with a number. You may use two_bits instead.

    > How do I set the two MSB in value to the two LSB in 2bits without


    > changing any other bits in value?


    >


    > <mytry>


    > value &= 0x3F; //set those two bits to zero


    > value |= (2bits<<6);


    > </mytry>


    Use the now identifier in your try and it should work.

    Kind regards,
     
    Frank Roland, Oct 9, 2003
    #2
    1. Advertising

  3. s

    s Guest

    Thanks for the reply and sorry for the bad identifier!

    Here's a better explanation of what I'm really trying to do:

    unsigned char target = 0xBD;
    unsigned char small_value = 2;
    unsigned position = 0xC0;

    I need to put the two bits in small_value into target at the place
    indicated by the set bits in position.

    thanks

    Frank Roland wrote:
    > Your identifiert may not start with a number. You may use two_bits instead.
    >>value |= (2bits<<6);

    > Use the now identifier in your try and it should work.
    >
    > Kind regards,
    >
    >
     
    s, Oct 9, 2003
    #3
  4. s

    Tim Hagan Guest

    s wrote:
    >
    > If I have:
    >
    > unsigned char value = 0xBD;
    > unsigned char 2bits = 0x02;


    2bits is an invalid identifier. The first character must be a letter or
    an underscore, followed by any sequence of digits and/or upper or lower
    case letters.

    > How do I set the two MSB in value to the two LSB in 2bits without
    > changing any other bits in value?
    >
    > <mytry>
    > value &= 0x3F; //set those two bits to zero
    > value |= (2bits<<6);
    > </mytry>


    Looks good to me. However, it isn't very interesting, since the two
    bits that you are trying to set in 'value' are already set you way
    want them. Observe:

    0xBD = 10111101
    ^^
    0x02 = 00000010

    > Do I have to clear(or set) those two bits first?


    You must clear them first, as you did in your example.

    --
    Tim Hagan
     
    Tim Hagan, Oct 9, 2003
    #4
  5. s

    Tim Hagan Guest

    s wrote:
    >
    > Here's a better explanation of what I'm really trying to do:
    >
    > unsigned char target = 0xBD;
    > unsigned char small_value = 2;
    > unsigned position = 0xC0;
    >
    > I need to put the two bits in small_value into target at the place
    > indicated by the set bits in position.


    Here's one way to do it. It is not a very elegant solution, nor is the
    output particularly interesting.

    #include <stdio.h>

    int main(void)
    {
    unsigned char target = 0xBD;
    unsigned char small_value = 2;
    unsigned char position = 0xC0; /* must be non-zero */
    unsigned char temp = position;
    int i = 0;

    while (!(temp & 1))
    {
    temp >>= 1;
    i++;
    }
    target &= ~position;
    target |= (small_value << i);
    printf("%#2X\n", target);
    return 0;
    }

    --
    Tim Hagan
     
    Tim Hagan, Oct 10, 2003
    #5
  6. s

    Tim Hagan Guest

    Tim Hagan wrote:
    >
    > s wrote:
    > >
    > > If I have:
    > >
    > > unsigned char value = 0xBD;
    > > unsigned char 2bits = 0x02;

    >
    > 2bits is an invalid identifier. The first character must be a letter or
    > an underscore, followed by any sequence of digits and/or upper or lower
    > case letters.


    I tried to paraphrase K&R2 (a *big* mistake) and flubbed it. I should
    have mentioned that underscores are considered to be letters and can
    appear anywhere in identifier names. However, it is not recommended that
    identifiers *begin* with an underscore since C library routines usually
    use such names and conflicts may result. Also, some identifiers are
    reserved as keywords (auto, break, case, char, ... ).

    > > How do I set the two MSB in value to the two LSB in 2bits without
    > > changing any other bits in value?
    > >
    > > <mytry>
    > > value &= 0x3F; //set those two bits to zero
    > > value |= (2bits<<6);
    > > </mytry>

    >
    > Looks good to me. However, it isn't very interesting, since the two
    > bits that you are trying to set in 'value' are already set you way
    > want them.


    Gak! I need a better proofreader.

    > Observe:
    >
    > 0xBD = 10111101
    > ^^
    > 0x02 = 00000010
    >
    > > Do I have to clear(or set) those two bits first?

    >
    > You must clear them first, as you did in your example.


    --
    Tim Hagan
     
    Tim Hagan, Oct 10, 2003
    #6
    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. GGG
    Replies:
    10
    Views:
    12,567
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    828
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    446
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. sergey

    "casting" bits to bits?

    sergey, Nov 8, 2006, in forum: VHDL
    Replies:
    1
    Views:
    709
    sergey
    Nov 8, 2006
  5. Tomás

    Value Bits Vs Object Bits

    Tomás, Jun 2, 2006, in forum: C Programming
    Replies:
    13
    Views:
    549
    Hallvard B Furuseth
    Jul 1, 2006
Loading...

Share This Page