What are: & | ~ ^ operators?

Jan 31, 2010 at 10:40am
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.
Jan 31, 2010 at 11:04am
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?
Jan 31, 2010 at 11:21am
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?

Jan 31, 2010 at 11:26am
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.

Jan 31, 2010 at 11:43am
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 Jan 31, 2010 at 11:48am
Jan 31, 2010 at 11:56am
[] 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).
Jan 31, 2010 at 12:11pm
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 Jan 31, 2010 at 12:12pm
Jan 31, 2010 at 12:57pm
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.*/
};

Jan 31, 2010 at 1:21pm
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 Jan 31, 2010 at 1:27pm
Jan 31, 2010 at 1:40pm
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.
Jan 31, 2010 at 1:50pm
So revision is always the key.
what?
Jan 31, 2010 at 2:15pm
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 Jan 31, 2010 at 2:16pm
Topic archived. No new replies allowed.