bit copying

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • curium

    bit copying

    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.



  • Rob Williscroft

    #2
    Re: bit copying

    curium wrote in news:3f45ae9d$0 $11384$cc9e4d1f @news.dial.pipe x.com:
    [color=blue]
    > 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.
    >[/color]

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

    Comment

    • CBFalconer

      #3
      Re: bit copying

      Rob Williscroft wrote:[color=blue]
      > curium wrote:
      >[color=green]
      > > 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.
      > >[/color]
      >
      > unsigned merge( unsigned char lo, unsigned char hi )
      > {
      > unsigned result = static_cast< unsigned >(lo) & 0x3F;
      >
      > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
      >
      > return rsult;
      > }[/color]

      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 (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
      Available for consulting/temporary embedded and systems.
      <http://cbfalconer.home .att.net> USE worldnet address!

      Comment

      • Nudge

        #4
        Re: bit copying

        > I don't understand what you mean by[color=blue]
        > "There is no such thing as an AND or an OR operator."[/color]

        AND and OR and not defined as keywords in C.

        You'd use one of:
        &
        &&
        |
        ||

        Comment

        • pete

          #5
          Re: bit copying

          Nudge wrote:[color=blue]
          >[color=green]
          > > I don't understand what you mean by
          > > "There is no such thing as an AND or an OR operator."[/color]
          >
          > AND and OR and not defined as keywords in C.[/color]

          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.
          [color=blue]
          > You'd use one of:
          > &[/color]
          N868
          6.5.10 Bitwise AND operator
          [color=blue]
          > &&[/color]
          N869
          6.5.13 Logical AND operator
          [color=blue]
          > |[/color]
          N868
          6.5.12 Bitwise OR operator

          [color=blue]
          > ||[/color]
          N869
          6.5.14 Logical OR operator

          --
          pete

          Comment

          • Samuel Barber

            #6
            Re: bit copying

            Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message news:<Xns93DF48 389C834ukcoREMO VEfreenetrtw@19 5.129.110.201>. ..[color=blue]
            > curium wrote in news:3f45ae9d$0 $11384$cc9e4d1f @news.dial.pipe x.com:
            >[color=green]
            > > 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.[/color]
            >
            > unsigned merge( unsigned char lo, unsigned char hi )
            > {
            > unsigned result = static_cast< unsigned >(lo) & 0x3F;
            >
            > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
            >
            > return result;
            > }[/color]

            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

            Comment

            • Samuel Barber

              #7
              Re: bit copying

              Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message news:<Xns93DF48 389C834ukcoREMO VEfreenetrtw@19 5.129.110.201>. ..[color=blue]
              > curium wrote in news:3f45ae9d$0 $11384$cc9e4d1f @news.dial.pipe x.com:
              >[color=green]
              > > 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.[/color]
              >
              > unsigned merge( unsigned char lo, unsigned char hi )
              > {
              > unsigned result = static_cast< unsigned >(lo) & 0x3F;
              >
              > result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
              >
              > return rsult;
              > }[/color]

              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.

              Comment

              • CBFalconer

                #8
                Re: bit copying

                pete wrote:[color=blue]
                > CBFalconer wrote:[color=green]
                > > Rob Williscroft wrote:[color=darkred]
                > > > 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;
                > > > }[/color]
                > >
                > > 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.[/color]
                >
                > I don't understand what you mean by
                > "There is no such thing as an AND or an OR operator."[/color]

                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 (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.net> USE worldnet address!

                Comment

                • Alan Balmer

                  #9
                  Re: bit copying

                  On Fri, 22 Aug 2003 11:44:48 GMT, pete <pfiland@mindsp ring.com> wrote:
                  [color=blue][color=green]
                  >>[color=darkred]
                  >> > I don't understand what you mean by
                  >> > "There is no such thing as an AND or an OR operator."[/color]
                  >>
                  >> AND and OR and not defined as keywords in C.[/color]
                  >
                  >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.[/color]

                  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
                  removebalmercon sultingthis@att .net

                  Comment

                  • pete

                    #10
                    Re: bit copying

                    CBFalconer wrote:[color=blue]
                    >
                    > pete wrote:[color=green]
                    > > CBFalconer wrote:[color=darkred]
                    > > > 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.[/color]
                    > >
                    > > I don't understand what you mean by
                    > > "There is no such thing as an AND or an OR operator."[/color]
                    >
                    > The operators are || and &&,
                    > unless you #include <iso646.h>, when
                    > the macros 'or' and 'and' expand to those anyway.[/color]

                    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

                    Comment

                    • Jan Engelhardt

                      #11
                      Re: bit copying

                      >>> > I don't understand what you mean by[color=blue][color=green][color=darkred]
                      >>> > "There is no such thing as an AND or an OR operator."
                      >>>
                      >>> AND and OR and not defined as keywords in C.[/color]
                      >>
                      >>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.[/color]
                      >
                      >I think you're confusing operators and operations.
                      >
                      >The set of C operators is quite small, and does not include either AND
                      >or OR.[/color]

                      But & and |

                      --
                      - Jan Engelhardt

                      Comment

                      • pete

                        #12
                        Re: bit copying

                        Alan Balmer wrote:[color=blue]
                        >
                        > On Fri, 22 Aug 2003 11:44:48 GMT, pete <pfiland@mindsp ring.com> wrote:
                        >[color=green][color=darkred]
                        > >>
                        > >> > 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.[/color]
                        > >
                        > >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.[/color]
                        >
                        > I think you're confusing operators and operations.[/color]

                        What do you mean ?
                        [color=blue]
                        > The set of C operators is quite small,[/color]

                        There are over 40 operators in C.
                        [color=blue]
                        > and does not include either AND or OR.[/color]

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

                        --
                        pete

                        Comment

                        • pete

                          #13
                          Re: bit copying

                          Rob Williscroft wrote:[color=blue]
                          >
                          > Samuel Barber wrote in
                          > news:37991aef.0 308220428.63d4f d0a@posting.goo gle.com:
                          >[color=green]
                          > > 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.[/color]
                          >
                          > In a *nice* little function with unsigned args, I guess so.[/color]

                          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

                          Comment

                          • Alan Balmer

                            #14
                            Re: bit copying

                            On Fri, 22 Aug 2003 16:21:17 GMT, pete <pfiland@mindsp ring.com> wrote:
                            [color=blue]
                            >Alan Balmer wrote:[color=green]
                            >>
                            >> On Fri, 22 Aug 2003 11:44:48 GMT, pete <pfiland@mindsp ring.com> wrote:
                            >>[color=darkred]
                            >> >>
                            >> >> > 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.[/color]
                            >>
                            >> I think you're confusing operators and operations.[/color]
                            >
                            >What do you mean ?[/color]

                            Exactly what I said.[color=blue]
                            >[color=green]
                            >> The set of C operators is quite small,[/color]
                            >
                            >There are over 40 operators in C.[/color]

                            Yes. A small set.[color=blue]
                            >[color=green]
                            >> and does not include either AND or OR.[/color]
                            >
                            >I'll admit that "AND" is too brief,
                            >to completely specify which operator it refers to.[/color]

                            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
                            removebalmercon sultingthis@att .net

                            Comment

                            • pete

                              #15
                              Re: bit copying

                              Alan Balmer wrote:[color=blue]
                              >
                              > On Fri, 22 Aug 2003 16:21:17 GMT, pete <pfiland@mindsp ring.com> wrote:
                              >[color=green]
                              > >Alan Balmer wrote:[color=darkred]
                              > >>
                              > >> On Fri, 22 Aug 2003 11:44:48 GMT,
                              > >> pete <pfiland@mindsp ring.com> wrote:[/color][/color][/color]
                              [color=blue][color=green]
                              > >I'll admit that "AND" is too brief,
                              > >to completely specify which operator it refers to.[/color]
                              >
                              > 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 '&&'.[/color]

                              What you are calling the "descriptio n 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.
                              [color=blue]
                              > If you like, you can write a paragraph describing what '&&' does, but
                              > that paragraph will not be an operator.[/color]

                              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

                              Comment

                              Working...