Can two block of memory do & operation meanwhile?

Discussion in 'C++' started by dolphin, Feb 4, 2009.

  1. dolphin

    dolphin Guest

    Hi all
    I have a question about & operation. For example, I have two
    memory blocks like below:
    char memory1[10];
    char memory2[10];
    Is there any way to do the & operation at one time for this two
    arrays? Or I need ues a loop to do it by byte? Thanks!
     
    dolphin, Feb 4, 2009
    #1
    1. Advertising

  2. dolphin

    Ian Collins Guest

    dolphin wrote:
    > Hi all
    > I have a question about & operation. For example, I have two
    > memory blocks like below:
    > char memory1[10];
    > char memory2[10];
    > Is there any way to do the & operation at one time for this two
    > arrays? Or I need ues a loop to do it by byte? Thanks!


    The latter, you need a loop.

    You may be able to alias the blocks with a pointer to a larger type (say
    unsigned long) and cut down the number of iterations.

    --
    Ian Collins
     
    Ian Collins, Feb 4, 2009
    #2
    1. Advertising

  3. On Feb 4, 6:12 am, dolphin <> wrote:
    > Hi all
    >        I have a question about & operation. For example, I have two
    > memory blocks like below:
    >        char memory1[10];
    >        char memory2[10];
    >        Is there any way to do the & operation at one time for this two
    > arrays? Or I need ues a loop to do it by byte? Thanks!


    Chexk out std::valarray.
     
    Vidar Hasfjord, Feb 4, 2009
    #3
  4. On Feb 4, 6:12 am, dolphin <> wrote:
    > Hi all
    >        I have a question about & operation. For example, I have two
    > memory blocks like below:
    >        char memory1[10];
    >        char memory2[10];
    >        Is there any way to do the & operation at one time for this two
    > arrays? Or I need ues a loop to do it by byte? Thanks!


    Chexk out std::valarray.
     
    Vidar Hasfjord, Feb 4, 2009
    #4
  5. dolphin <> writes:

    > Hi all
    > I have a question about & operation. For example, I have two
    > memory blocks like below:
    > char memory1[10];
    > char memory2[10];
    > Is there any way to do the & operation at one time for this two
    > arrays? Or I need ues a loop to do it by byte? Thanks!


    std::transform(memory1,memory1+10,memory2,memory3,bitAnd);

    ------------------------------------------------------------------------
    #include <algorithm>
    #include <iostream>
    #include <iterator>

    #include <boost/bind.hpp>

    unsigned char bitAnd(unsigned char a,unsigned char b){return(a&b);}
    int main(){
    unsigned char memory1[10]={1,2,3,4,5,6,7,8,10};
    unsigned char memory2[10]={15,14,13,12,11,10,9,8,7,6};
    unsigned char memory3[10];
    // memory3 = memory1 & memory2
    std::transform(memory1,memory1+10,memory2,memory3,bitAnd);
    std::copy(memory3,memory3+10,std::eek:stream_iterator<int>(std::cout,", "));
    std::cout<<std::endl;
    return(0);
    }

    /*
    -*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
    Compilation started at Wed Feb 4 10:49:20

    SRC="/home/pjb/src/tests-c++/array-and.c++" ; EXE="array-and" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
    1, 2, 1, 4, 1, 2, 1, 8, 2, 0,
    status = 0

    Compilation finished at Wed Feb 4 10:49:20

    */
    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Feb 4, 2009
    #5
  6. On Feb 4, 4:50 am, (Pascal J. Bourguignon)
    wrote:
    > dolphin <> writes:
    > > Hi all
    > >        I have a question about & operation. For example, I have two
    > > memory blocks like below:
    > >        char memory1[10];
    > >        char memory2[10];
    > >        Is there any way to do the & operation at one time for this two
    > > arrays? Or I need ues a loop to do it by byte? Thanks!

    >
    >     std::transform(memory1,memory1+10,memory2,memory3,bitAnd);
    >
    > ------------------------------------------------------------------------
    > #include <algorithm>
    > #include <iostream>
    > #include <iterator>
    >
    > #include <boost/bind.hpp>
    >
    > unsigned char bitAnd(unsigned char a,unsigned char b){return(a&b);}
    > int main(){
    >     unsigned char memory1[10]={1,2,3,4,5,6,7,8,10};
    >     unsigned char memory2[10]={15,14,13,12,11,10,9,8,7,6};
    >     unsigned char memory3[10];
    >     // memory3 = memory1 & memory2
    >     std::transform(memory1,memory1+10,memory2,memory3,bitAnd);
    >     std::copy(memory3,memory3+10,std::eek:stream_iterator<int>(std::cout,", "));
    >     std::cout<<std::endl;
    >     return(0);
    >
    > }
    >
    > /*
    > -*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
    > Compilation started at Wed Feb  4 10:49:20
    >
    > SRC="/home/pjb/src/tests-c++/array-and.c++" ; EXE="array-and" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} &&  ./${EXE} && echo status = $?
    > 1, 2, 1, 4, 1, 2, 1, 8, 2, 0,
    > status = 0
    >
    > Compilation finished at Wed Feb  4 10:49:20
    >
    >  */              
    > --
    > __Pascal Bourguignon__


    I don't think you need to include #include <boost/bind.hpp> in this
    example.
     
    puzzlecracker, Feb 4, 2009
    #6
  7. dolphin

    dolphin Guest

    On Feb 4, 10:50 am, (Pascal J. Bourguignon)
    wrote:
    > dolphin <> writes:
    > > Hi all
    > >        I have a question about & operation. For example, I have two
    > > memory blocks like below:
    > >        char memory1[10];
    > >        char memory2[10];
    > >        Is there any way to do the & operation at one time for this two
    > > arrays? Or I need ues a loop to do it by byte? Thanks!

    >
    >     std::transform(memory1,memory1+10,memory2,memory3,bitAnd);
    >
    > ------------------------------------------------------------------------
    > #include <algorithm>
    > #include <iostream>
    > #include <iterator>
    >
    > #include <boost/bind.hpp>
    >
    > unsigned char bitAnd(unsigned char a,unsigned char b){return(a&b);}
    > int main(){
    >     unsigned char memory1[10]={1,2,3,4,5,6,7,8,10};
    >     unsigned char memory2[10]={15,14,13,12,11,10,9,8,7,6};
    >     unsigned char memory3[10];
    >     // memory3 = memory1 & memory2
    >     std::transform(memory1,memory1+10,memory2,memory3,bitAnd);
    >     std::copy(memory3,memory3+10,std::eek:stream_iterator<int>(std::cout,", "));
    >     std::cout<<std::endl;
    >     return(0);
    >
    > }
    >
    > /*
    > -*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
    > Compilation started at Wed Feb  4 10:49:20
    >
    > SRC="/home/pjb/src/tests-c++/array-and.c++" ; EXE="array-and" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} &&  ./${EXE} && echo status = $?
    > 1, 2, 1, 4, 1, 2, 1, 8, 2, 0,
    > status = 0
    >
    > Compilation finished at Wed Feb  4 10:49:20
    >
    >  */              
    > --
    > __Pascal Bourguignon__


    I think it is helpful for me. Thnaks I will try it.
     
    dolphin, Feb 11, 2009
    #7
    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. GenxLogic
    Replies:
    3
    Views:
    1,352
    andrewmcdonagh
    Dec 6, 2006
  2. morrell
    Replies:
    1
    Views:
    992
    roy axenov
    Oct 10, 2006
  3. david ullua
    Replies:
    13
    Views:
    684
  4. raan
    Replies:
    2
    Views:
    464
  5. Buzz Lightyear
    Replies:
    10
    Views:
    1,146
    Alexander Bartolich
    Aug 12, 2009
Loading...

Share This Page