Hello, I have a question about bitwise operators, especially shifting.
Suppose we have:

#include <iostream>

using namespace std;

int main() {
    unsigned i = 2;
    i<<=2;
    cout << i << endl;
}

unsigned i = 2 in binary is 0.......00010, if we shift 2 bits to the left we will get 8, which is 0.......01000.

How can we print out or change bits using shift opertars(<<, >>)?

Thanks in advance

ok, so that make sense.
Thanks.

I have another question now. I am trying to set a bit in unsigned, that is what I am doing:

#include <iostream>

using namespace std;

int main(){
    unsigned i = 0;
    unsigned index = 1;
    i >> (31 - index) |= 1;
}

But I am getting this error:
invalid lvalue in assignment at line 8.

Can't figure that out.

Thanks for helping.

ok, so that make sense.
Thanks.

I have another question now. I am trying to set a bit in unsigned, that is what I am doing:

#include <iostream>

using namespace std;

int main(){
    unsigned i = 0;
    unsigned index = 1;
    i >> (31 - index) |= 1;
}

But I am getting this error:
invalid lvalue in assignment at line 8.

Can't figure that out.

Thanks for helping.

The left hand side of your expression does not compile to a memory address, which is necessary for the assignment to occur.
Set a bit like this:
i |= (1 << index);

This question has already been answered. Start a new discussion instead.