bit copying

Discussion in 'C++' started by curium, Aug 21, 2003.

  1. curium

    curium Guest

    Hi

    I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i need to
    copy bits 0-6 from another byte into bits 7-14 of this word.

    I am going gray from messing around with AND, OR, >> and << operators. I
    hope the collective audience of this group can point out an obvious solution
    to this problem so that i can kick myself for not seeing the obvious.

    The main problem i am experiencing is preserving the LO byte of the word
    when copying from the 2nd byte.

    Thanks.
     
    curium, Aug 21, 2003
    #1
    1. Advertising

  2. curium wrote in news:3f45ae9d$0$11384$:

    > Hi
    >
    > I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
    > need to copy bits 0-6 from another byte into bits 7-14 of this word.
    >
    > I am going gray from messing around with AND, OR, >> and << operators.
    > I hope the collective audience of this group can point out an obvious
    > solution to this problem so that i can kick myself for not seeing the
    > obvious.
    >
    > The main problem i am experiencing is preserving the LO byte of the
    > word when copying from the 2nd byte.
    >


    unsigned merge( unsigned char lo, unsigned char hi )
    {
    unsigned result = static_cast< unsigned >(lo) & 0x3F;

    result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;

    return rsult;
    }


    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Aug 22, 2003
    #2
    1. Advertising

  3. curium

    CBFalconer Guest

    Rob Williscroft wrote:
    > curium wrote:
    >
    > > I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
    > > need to copy bits 0-6 from another byte into bits 7-14 of this word.
    > >
    > > I am going gray from messing around with AND, OR, >> and <<
    > > operators. I hope the collective audience of this group can point
    > > out an obvious solution to this problem so that i can kick myself
    > > for not seeing the obvious.
    > >
    > > The main problem i am experiencing is preserving the LO byte of
    > > the word when copying from the 2nd byte.
    > >

    >
    > unsigned merge( unsigned char lo, unsigned char hi )
    > {
    > unsigned result = static_cast< unsigned >(lo) & 0x3F;
    >
    > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
    >
    > return rsult;
    > }


    Because some idiot cross posted to c.l.c and c.l.c++, you have
    posted something that is not applicable on c.l.c. So lets change
    the function body to be valid C, and hope it is still valid C++
    :)

    unsigned int result;

    result = ((hi & 0x3f) << 6) | (lo & 0x3f);
    return result;

    These values can never cause an integer overflow. There is no
    such thing as an AND or an OR operator.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 22, 2003
    #3
  4. curium

    Nudge Guest

    > I don't understand what you mean by
    > "There is no such thing as an AND or an OR operator."


    AND and OR and not defined as keywords in C.

    You'd use one of:
    &
    &&
    |
    ||
     
    Nudge, Aug 22, 2003
    #4
  5. curium

    pete Guest

    Nudge wrote:
    >
    > > I don't understand what you mean by
    > > "There is no such thing as an AND or an OR operator."

    >
    > AND and OR and not defined as keywords in C.


    I can understand that well enough to say that
    that makes no sense at all.
    There's a lot more to C, than just keywords.

    > You'd use one of:
    > &

    N868
    6.5.10 Bitwise AND operator

    > &&

    N869
    6.5.13 Logical AND operator

    > |

    N868
    6.5.12 Bitwise OR operator


    > ||

    N869
    6.5.14 Logical OR operator

    --
    pete
     
    pete, Aug 22, 2003
    #5
  6. Rob Williscroft <> wrote in message news:<Xns93DF48389C834ukcoREMOVEfreenetrtw@195.129.110.201>...
    > curium wrote in news:3f45ae9d$0$11384$:
    >
    > > I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
    > > need to copy bits 0-6 from another byte into bits 7-14 of this word.
    > >
    > > I am going gray from messing around with AND, OR, >> and << operators.
    > > I hope the collective audience of this group can point out an obvious
    > > solution to this problem so that i can kick myself for not seeing the
    > > obvious.

    >
    > unsigned merge( unsigned char lo, unsigned char hi )
    > {
    > unsigned result = static_cast< unsigned >(lo) & 0x3F;
    >
    > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
    >
    > return result;
    > }


    Or if I understood the problem description, this:

    unsigned merge( unsigned result, unsigned char lo, unsigned char hi )
    {
    result &= ~( (1<<14) - 1 );

    result |= lo & 0x3F;

    result |= ( hi & 0x3F ) << 6;

    return result;
    }

    The casts are not needed.

    Sam
     
    Samuel Barber, Aug 22, 2003
    #6
  7. Rob Williscroft <> wrote in message news:<Xns93DF48389C834ukcoREMOVEfreenetrtw@195.129.110.201>...
    > curium wrote in news:3f45ae9d$0$11384$:
    >
    > > I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
    > > need to copy bits 0-6 from another byte into bits 7-14 of this word.
    > >
    > > I am going gray from messing around with AND, OR, >> and << operators.
    > > I hope the collective audience of this group can point out an obvious
    > > solution to this problem so that i can kick myself for not seeing the
    > > obvious.

    >
    > unsigned merge( unsigned char lo, unsigned char hi )
    > {
    > unsigned result = static_cast< unsigned >(lo) & 0x3F;
    >
    > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
    >
    > return rsult;
    > }


    Or if I understood the problem description, this:

    unsigned merge( unsigned result, unsigned char lo, unsigned char hi )
    {
    result &= ~( (1<<14) - 1);

    result |= lo & 0x7F;

    result |= ( hi & 0x7F ) << 7;

    return result;
    }

    The casts are not needed.

    Sam

    P.S. Ignore my other reply.
     
    Samuel Barber, Aug 22, 2003
    #7
  8. curium

    CBFalconer Guest

    pete wrote:
    > CBFalconer wrote:
    > > Rob Williscroft wrote:
    > > > curium wrote:
    > > >
    > > > > I need to copy bits 0-6 from a byte into bits 0-6 of a word.
    > > > > then i need to copy bits 0-6 from another byte
    > > > > into bits 7-14 of this word.
    > > > >
    > > > > I am going gray from messing around with AND, OR, >> and <<
    > > > > operators. I hope the collective audience of this group can
    > > > > point out an obvious solution to this problem so that i can
    > > > > kick myself for not seeing the obvious.
    > > > >
    > > > > The main problem i am experiencing is preserving the LO byte
    > > > > of the word when copying from the 2nd byte.
    > > >
    > > > unsigned merge( unsigned char lo, unsigned char hi )
    > > > {
    > > > unsigned result = static_cast< unsigned >(lo) & 0x3F;
    > > >
    > > > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
    > > > return rsult;
    > > > }

    > >
    > > Because some idiot cross posted to c.l.c and c.l.c++, you have
    > > posted something that is not applicable on c.l.c. So lets change
    > > the function body to be valid C, and hope it is still valid C++
    > > :)
    > >
    > > unsigned int result;
    > >
    > > result = ((hi & 0x3f) << 6) | (lo & 0x3f);
    > > return result;
    > >
    > > These values can never cause an integer overflow. There is no
    > > such thing as an AND or an OR operator.

    >
    > I don't understand what you mean by
    > "There is no such thing as an AND or an OR operator."


    The operators are || and &&, unless you #include <iso646.h>, when
    the macros 'or' and 'and' expand to those anyway. That also give
    the equivalence of 'bit_and' and &, and of 'bit_or' and |.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 22, 2003
    #8
  9. curium

    Alan Balmer Guest

    On Fri, 22 Aug 2003 11:44:48 GMT, pete <> wrote:

    >>
    >> > I don't understand what you mean by
    >> > "There is no such thing as an AND or an OR operator."

    >>
    >> AND and OR and not defined as keywords in C.

    >
    >I can understand that well enough to say that
    >that makes no sense at all.
    >There's a lot more to C, than just keywords.


    I think you're confusing operators and operations.

    The set of C operators is quite small, and does not include either AND
    or OR.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Aug 22, 2003
    #9
  10. curium

    pete Guest

    CBFalconer wrote:
    >
    > pete wrote:
    > > CBFalconer wrote:
    > > > Rob Williscroft wrote:
    > > > > curium wrote:
    > > > >
    > > > > > I need to copy bits 0-6 from a byte into bits 0-6 of a word.
    > > > > > then i need to copy bits 0-6 from another byte
    > > > > > into bits 7-14 of this word.
    > > > > >
    > > > > > I am going gray from messing around with AND, OR, >> and <<
    > > > > > operators. I hope the collective audience of this group can
    > > > > > point out an obvious solution to this problem so that i can
    > > > > > kick myself for not seeing the obvious.
    > > > > >
    > > > > > The main problem i am experiencing is preserving the LO byte
    > > > > > of the word when copying from the 2nd byte.
    > > > >
    > > > > unsigned merge( unsigned char lo, unsigned char hi )
    > > > > {
    > > > > unsigned result = static_cast< unsigned >(lo) & 0x3F;
    > > > >
    > > > > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
    > > > > return rsult;
    > > > > }
    > > >
    > > > Because some idiot cross posted to c.l.c and c.l.c++, you have
    > > > posted something that is not applicable on c.l.c. So lets change
    > > > the function body to be valid C, and hope it is still valid C++
    > > > :)
    > > >
    > > > unsigned int result;
    > > >
    > > > result = ((hi & 0x3f) << 6) | (lo & 0x3f);
    > > > return result;
    > > >
    > > > These values can never cause an integer overflow. There is no
    > > > such thing as an AND or an OR operator.

    > >
    > > I don't understand what you mean by
    > > "There is no such thing as an AND or an OR operator."

    >
    > The operators are || and &&,
    > unless you #include <iso646.h>, when
    > the macros 'or' and 'and' expand to those anyway.


    I'm having a very hard time parsing that sentence.

    You're saying that when you #include <iso646.h>,
    then || and && aren't the operators, but that when you
    don't #include <iso646.h>, then they are the operators.

    I'm even more confused about what you're saying now.
    What do you mean by "The", in "The operators" ?
    There's no || or && in the posted code,
    so you must mean that those operators have been previously
    referred to in the text of this thread.
    *Which* operators are || and && ?
    The ones which there are no such things as ?

    Is your objection that he did not precisely specify
    "bitwise AND operator"
    and
    "bitwise inclusive OR assignment operator"
    ?

    --
    pete
     
    pete, Aug 22, 2003
    #10
  11. >>> > I don't understand what you mean by
    >>> > "There is no such thing as an AND or an OR operator."
    >>>
    >>> AND and OR and not defined as keywords in C.

    >>
    >>I can understand that well enough to say that
    >>that makes no sense at all.
    >>There's a lot more to C, than just keywords.

    >
    >I think you're confusing operators and operations.
    >
    >The set of C operators is quite small, and does not include either AND
    >or OR.


    But & and |

    --
    - Jan Engelhardt
     
    Jan Engelhardt, Aug 22, 2003
    #11
  12. curium

    pete Guest

    Alan Balmer wrote:
    >
    > On Fri, 22 Aug 2003 11:44:48 GMT, pete <> wrote:
    >
    > >>
    > >> > I don't understand what you mean by
    > >> > "There is no such thing as an AND or an OR operator."
    > >>
    > >> AND and OR and not defined as keywords in C.

    > >
    > >I can understand that well enough to say that
    > >that makes no sense at all.
    > >There's a lot more to C, than just keywords.

    >
    > I think you're confusing operators and operations.


    What do you mean ?

    > The set of C operators is quite small,


    There are over 40 operators in C.

    > and does not include either AND or OR.


    I'll admit that "AND" is too brief,
    to completely specify which operator it refers to.

    --
    pete
     
    pete, Aug 22, 2003
    #12
  13. curium

    pete Guest

    Rob Williscroft wrote:
    >
    > Samuel Barber wrote in
    > news::
    >
    > > Or if I understood the problem description, this:
    > >
    > > unsigned merge( unsigned result, unsigned char lo, unsigned char hi )
    > > {
    > > result &= ~( (1<<14) - 1);
    > >
    > > result |= lo & 0x7F;
    > >
    > > result |= ( hi & 0x7F ) << 7;
    > >
    > > return result;
    > > }
    > >
    > > The casts are not needed.

    >
    > In a *nice* little function with unsigned args, I guess so.


    That's the way that I would write it (with unsigned args).

    unsigned merge( unsigned result, unsigned lo, unsigned hi);

    .... regardles of the fact that the representations of values
    for lo and hi, fit into bytes.
    That way, the variables don't get converted in any of
    the operations in that function.
    I realise that the conversion of variables isn't quite tragic,
    but the function is simpler without it.

    --
    pete
     
    pete, Aug 22, 2003
    #13
  14. curium

    Alan Balmer Guest

    On Fri, 22 Aug 2003 16:21:17 GMT, pete <> wrote:

    >Alan Balmer wrote:
    >>
    >> On Fri, 22 Aug 2003 11:44:48 GMT, pete <> wrote:
    >>
    >> >>
    >> >> > I don't understand what you mean by
    >> >> > "There is no such thing as an AND or an OR operator."
    >> >>
    >> >> AND and OR and not defined as keywords in C.
    >> >
    >> >I can understand that well enough to say that
    >> >that makes no sense at all.
    >> >There's a lot more to C, than just keywords.

    >>
    >> I think you're confusing operators and operations.

    >
    >What do you mean ?


    Exactly what I said.
    >
    >> The set of C operators is quite small,

    >
    >There are over 40 operators in C.


    Yes. A small set.
    >
    >> and does not include either AND or OR.

    >
    >I'll admit that "AND" is too brief,
    >to completely specify which operator it refers to.


    Again, you're referring to a *description* of the operator, not the
    operator itself, which in this instance is even more brief - one of
    '&' or '&&'.

    If you like, you can write a paragraph describing what '&&' does, but
    that paragraph will not be an operator.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Aug 22, 2003
    #14
  15. curium

    pete Guest

    Alan Balmer wrote:
    >
    > On Fri, 22 Aug 2003 16:21:17 GMT, pete <> wrote:
    >
    > >Alan Balmer wrote:
    > >>
    > >> On Fri, 22 Aug 2003 11:44:48 GMT,
    > >> pete <> wrote:


    > >I'll admit that "AND" is too brief,
    > >to completely specify which operator it refers to.

    >
    > Again, you're referring to a *description* of the operator, not the
    > operator itself, which in this instance is even more brief - one of
    > '&' or '&&'.


    What you are calling the "description of the operator",
    (Bitwise AND operator)
    I am calling the "name of the operator",
    and my point was that the full name of the operator wasn't given.

    > If you like, you can write a paragraph describing what '&&' does, but
    > that paragraph will not be an operator.


    I think you're making a distinction between
    names and descriptions and subjects, which is inappropriate.
    What you wrote, is as obvious as saying that
    if I write a paragraph about you, then that paragraph isn't you.

    However if I said that "the guy I'm arguing with" is wrong,
    or if I said "Alan Balmer" is wrong,
    then that's the same thing as saying that you are wrong.

    --
    pete
     
    pete, Aug 22, 2003
    #15
  16. curium

    CBFalconer Guest

    pete wrote:
    > CBFalconer wrote:
    > >

    .... snip ...
    > >
    > > The operators are || and &&,
    > > unless you #include <iso646.h>, when
    > > the macros 'or' and 'and' expand to those anyway.

    >
    > I'm having a very hard time parsing that sentence.
    >
    > You're saying that when you #include <iso646.h>,
    > then || and && aren't the operators, but that when you
    > don't #include <iso646.h>, then they are the operators.


    When you include iso646.h, you have the option of using the word
    'and' to signify &&, and the word 'or' to signify ||. Note
    option. && and || still work as advertised.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 23, 2003
    #16
    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. kumar
    Replies:
    8
    Views:
    3,536
    Vikram
    Feb 17, 2004
  2. curium

    bit copying

    curium, Aug 21, 2003, in forum: C Programming
    Replies:
    15
    Views:
    557
    CBFalconer
    Aug 23, 2003
  3. Replies:
    3
    Views:
    1,760
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    977
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    177
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page