What are: & | ~ ^ operators?

I have seen these used before, however I don't understand what they are used for. As they are rarely spoken about in beginning tutorials, books etc and I'm having trouble finding reference on them.

1
2
3
 & | ~ ^
and
^= &= |= <<= >>= []


I looking at the table on http://www.cplusplus.com/doc/tutorial/classes2/
About overloading operators.
I assume, that the [] is the array operators?... There's also -> this, and () parenthesis operators is this right?
However I'm uncertain about the others.
http://www.cplusplus.com/doc/tutorial/operators/

About overloading operators.

So, you don't understand what these operators do, or you don't understand how to overload them?
I don't understand what they do, I guess a better question would have been: What is a bitwise operator ? Where and in what context do you use them?

What is a bitwise operator ?

http://en.wikipedia.org/wiki/Bitwise_operation

Where and in what context do you use them?

The most common use, is to use individual bits as flags.
It's used lot of other places as well, I think you will know where you will need it.

hmm ok thanks, so all the others are as I thought? I don't see how you could overload [], If it's a constant pointer so to speak, what exactly could you overload it to? the multiplication operators are easy, and make sense as you can specify different members you want to math, or in the case of = specify only certain members to assign. But things like [] ,, Is well beyond my logical train of thought ...
Last edited on
[] is overloaded usually if the class represents some sort of array, or needs indexing (for example std::vector).
I usually overload () as a do_it() method. Since with () you can arbitrary parameters, it can have a lot of uses.
-> for example is overloaded for iterators (this is the pair of the dereference (*) operator).
I usually overload () as a do_it() method. Since with () you can arbitrary parameters, it can have a lot of uses.


I assume by arbitrary you mean mathematics: undetermined; not assigned a specific value.

Aren't parameters arbitrary by nature? I don't really get that. I mean it's in the case of objects so say we have a circle object we would probably have a blank constructor, and one that takes radius. create a constructor that takes another circle? What then could you overload () to do ?
Last edited on
Under arbitrary I mean you can have all these overloads at once :
1
2
3
4
5
6
7
class foo {
    void operator()();
    int operator()(int i);
    double operator()(float i);
    string operator()(int i, string str, double d, vector<int>);
    /*etc.*/
};

overload [] demo
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
#include <iostream>
using namespace std;

template<class dtype>class arrayx {
  private:
    dtype* ptr;
  public:
    unsigned int length;
    arrayx(unsigned int len) {
        if(len>=length || len<0) throw -1;
        length = len;
        ptr =  new dtype[len];
    }
    ~arrayx() {
        delete [] ptr;
    }
    dtype& operator [](unsigned int i) {
        if(i>=length || i<0) throw -1;
        return ptr[i];
    }
};

int main() {
    arrayx<char> arr(15);
    for(int x=0; x<arr.length; x++)
        arr[x] = x+65;

    for(int x=0; x<arr.length; x++)
        cout << arr[x] << endl;

    arr[-1] = 10;
    return 0;
}
Last edited on
Thanks guys, I get it. Still a little unsure of some things but I don't think I'll be using them anytime soon anyway. So revision is always the key.
So revision is always the key.
what?
bit shift demo
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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
/*
 Shifting number right 3 positions with the expression (number >> 3) divides the
  value by 8 and discards the remainder.
 Shifting the result of the previous operation left by 3 positions with the
 expression ((number >> 3) << 3) multiplies
 it by 8 so you get the value of number less the remainder R after dividing by
 8 i.e. (number - R)
 Subtracting (number - R) from number gives just R, which is what you are
 looking for.

 ex. given: 12 = 00001100
     12>>3 = 1 = 00000001
     1<<3  = 8 = 00001000
     12-8  = 4 = 00000100
*/

int main()
{
   int number;

   cout << "Enter a number: ";
   cin >> number;

   cout << "Thank you. The remainder after dividing your number by 8 is "
        << number - ((number >> 3) << 3);
   cout << endl;

   return 0;
}
Last edited on
Topic archived. No new replies allowed.