Bitwise operators understanding.

After messing with the bit-wise operators I sort of have an understanding of what is happening for of them, however I'm still a little confused about them. I was wondering if my observations were correct about them. I was wondering if you guys could explain what exactly I'm doing? I believe I understand the << operator however the other examples I have no idea what they're doing. I think the ~ operator makes the number negative and then subtracts by 1. But the rest of these operators are a little confusing. Not only that but what is the true purpose of these operators anyways?
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int test;
    for (int a = 0; a < 5; a++) {
        cout << "a << i: ";
        //Equal to (a = a * 2)
        for (int i = 1; i < 11; i++) {
              test = a << i;
              cout << test << ' ';
        }
        cout << endl;
        cout << "~a ";
        //Equal to ((a = a * -1) -1)
        for (int i = 1; i < 11; i++) {
              test = ~a;
              cout << test << ' ';
        }
        cout << endl;
        cout << "a ^ i: ";
        for (int i = 1; i < 11; i++) {
              test = a ^ i;
              cout << test << ' ';
        }
        cout << endl;
        cout << "~a ^ i: ";
        for (int i = 1; i < 11; i++) {
              test = ~a ^ i;
              cout << test << ' ';
        }
        cout << endl;
        cout << '~' << a << " & i: ";
        for (int i = 1; i < 11; i++) {
              test = ~a & i;
              cout << test << ' ';
        }
        cout << endl << endl;
    }
}
Topic archived. No new replies allowed.