create a constructo with two bool arrays...

Hello everyone..

I'm trying to code a constructor from a class with two byte(arrays), and It's not working at all...I leave my code right here...

[code]

class arithmetic{
public:
int a;
int b;
bool Z;
bool Y;
bool word1[8];
bool word2[8];

arithmetic(bool x[8],bool y[8]): word1(x),word2(y){};


.......
.......
int main(int argc, char **argv)
{
bool array1[] = {1,0,1,1,1,0,1,1};
bool array2[] = {1,1,1,0,0,1,1,0};
bool array3[] = {0,1,1,1,0,0,1,0};
bool array4[] = {1,1,1,1,0,0,0,1};


arithmetic byte1(array1,array2);
arithmetic byte2(array3,array4);


[code\]

there surely will be other ways to do it easily but I would like to achieve it like that becasue later I want to overload the operator & and | and do some logic operation,It would be like this :

[code]
arithmetic operator&(const arithmetic& lhs,const arithmetic& rhs){
arithmetic temp;
temp.word1[0] = lhs.word1[0] & rhs.word1[0];
temp.word1[1] = lhs.word1[1] & rhs.word1[1];
temp.word1[2] = lhs.word1[2] & rhs.word1[2];
temp.word1[3] = lhs.word1[3] & rhs.word1[3];
temp.word1[4] = lhs.word1[4] & rhs.word1[4];
temp.word1[5] = lhs.word1[5] & rhs.word1[5];
temp.word1[6] = lhs.word1[6] & rhs.word1[6];
temp.word1[7] = lhs.word1[7] & rhs.word1[7];

temp.word2[0] = lhs.word2[0] & rhs.word2[0];
temp.word2[1] = lhs.word2[1] & rhs.word2[1];
temp.word2[2] = lhs.word2[2] & rhs.word2[2];
temp.word2[3] = lhs.word2[3] & rhs.word2[3];
temp.word2[4] = lhs.word2[4] & rhs.word2[4];
temp.word2[5] = lhs.word2[5] & rhs.word2[5];
temp.word2[6] = lhs.word2[6] & rhs.word2[6];
temp.word2[7] = lhs.word2[7] & rhs.word2[7];
return temp;
}

arithmetic operator|(const arithmetic& lhs,const arithmetic& rhs){
arithmetic temp;
temp.word1[0] = lhs.word1[0] | rhs.word1[0];
temp.word1[1] = lhs.word1[1] | rhs.word1[1];
temp.word1[2] = lhs.word1[2] | rhs.word1[2];
temp.word1[3] = lhs.word1[3] | rhs.word1[3];
temp.word1[4] = lhs.word1[4] | rhs.word1[4];
temp.word1[5] = lhs.word1[5] | rhs.word1[5];
temp.word1[6] = lhs.word1[6] | rhs.word1[6];
temp.word1[7] = lhs.word1[7] | rhs.word1[7];

temp.word2[0] = lhs.word2[0] | rhs.word2[0];
temp.word2[1] = lhs.word2[1] | rhs.word2[1];
temp.word2[2] = lhs.word2[2] | rhs.word2[2];
temp.word2[3] = lhs.word2[3] | rhs.word2[3];
temp.word2[4] = lhs.word2[4] | rhs.word2[4];
temp.word2[5] = lhs.word2[5] | rhs.word2[5];
temp.word2[6] = lhs.word2[6] | rhs.word2[6];
temp.word2[7] = lhs.word2[7] | rhs.word2[7];
return temp;
}

[code\]

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <algorithm>
#include <initializer_list>

template < std::size_t N > struct arithmetic_t
{
    arithmetic_t() = default ;

    arithmetic_t( const bool (&w1)[N], const bool (&w2)[N] )
    {
        // http://en.cppreference.com/w/cpp/algorithm/copy
        std::copy( w1, w1+N, word1 ) ;
        std::copy( w2, w2+N, word2 ) ;
    }

    // http://en.cppreference.com/w/cpp/utility/initializer_list
    template < typename T > arithmetic_t( std::initializer_list<T> il_one,
                                          std::initializer_list<T> il_two )
    {
        // http://en.cppreference.com/w/cpp/algorithm/min
        auto sz = std::min( il_one.size(), N ) ;
        std::copy( il_one.begin(), il_one.begin()+sz, word1 ) ;

        sz = std::min( il_two.size(), N ) ;
        std::copy( il_two.begin(), il_two.begin()+sz, word2 ) ;
    }

    arithmetic_t& operator &= ( const arithmetic_t& that )
    {
        for( std::size_t i = 0 ; i < N ; ++i )
        {
            word1[i] &= that.word1[i] ;
            word2[i] &= that.word2[i] ;
        }
        return *this ;
    }

    bool word1[N] { false };
    bool word2[N] { false };
};

// See: Canonical implementations: Binary arithmetic operators
// in: http://en.cppreference.com/w/cpp/language/operators
template < std::size_t N >
arithmetic_t<N> operator& ( arithmetic_t<N> a, const arithmetic_t<N>& b )
{ return a &= b ; }

template < std::size_t N >
std::ostream& operator << ( std::ostream& stm, const arithmetic_t<N>& a )
{
    stm << '{' ;
    for( bool v : a.word1 ) stm << v ;
    stm << ',' ;
    for( bool v : a.word2 ) stm << v ;
    return stm << '}' ;
}

int main()
{
    using arithmetic = arithmetic_t<8> ;

    arithmetic a ;
    std::cout << "    a: " << a << '\n' ;

    bool array1[] = {1,0,1,1,1,0,1,1};
    bool array2[] = {1,1,1,0,0,1,1,0};
    arithmetic b( array1, array2 ) ;
    std::cout << "    b: " << b << '\n' ;

    arithmetic c ( {1,1,1,1,0,0,0,0}, {0,0,0,0,1,1,1,1} ) ;
    std::cout << "    c: " << c << '\n' ;

    std::cout << "b & c: " << (b&c) << '\n' ;
}

http://coliru.stacked-crooked.com/a/83ba9461dea8332f
Thank you, but It is going to take long until I get everythig...I have work to do forward...
Topic archived. No new replies allowed.