Я учу побитовый оператор из курса, загруженного из udemy, но это очень запутанно. На этом примере они учат, что очень сложно читать и понимать, есть ли курс или пример, который научит побитовый оператор простым способом? а также какая польза от побитового оператора? важно ли снимать головную боль или нет?
Пример:
#include <iostream>
using namespace std;
main()
{
/*
Operator Symbol Form Operation
left shift << x << y all bits in x shifted left y bits
right shift >> x >> y all bits in x shifted right y bits
bitwise NOT ~ ~x all bits in x flipped
bitwise AND & x & y each bit in x AND each bit in y
bitwise OR | x | y each bit in x OR each bit in y
bitwise XOR ^ x ^ y each bit in x XOR each bit in y
0
1
0101 0110
for example 126
1 * 10 ^ 2 + 2 * 10 ^ 1 + 6 * 10 ^ 0 = ?
1 * 10 ^ 2 = 100
2 * 10 ^ 1 = 20
6 * 10 ^ 0 = 6 why? because write 10 about 0 times = 0
we have the answer 126
1 2 6
3 2 1 // this is the position of 126. it's in -1 formula we can use
3-1=2
2-1=1
1-1=0
1 2 6 = 1 * 10 ^ 2 + 2 * 10 ^ 1 + 6 * 10 ^ 0
1 0 1 0 = 1 * 2 + 0 * 2 + 1 * 2 + 0 * 2
1 0 1 0 = 1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 0 * 2 ^ 0 = 10
1 * 2 ^ 3 = 8
0 * 2 ^ 2 = 0
1 * 2 ^ 1 = 2
0 * 2 ^ 0 = 0
8 + 0 + 2 + 0 = 10
// Decimal notation for 1 0 1 0 is 10
1 0 1 1 0 0 = 0*0^0 + 0*0^1 + 1*2^2 + 1*2^3 + 0*0^4 + 1*2^5 = 44
0*0^0 = 0
0*0^1 = 0
1*2^2 = 4
1*2^3 = 8
0*0^4 = 0
1*2^5 = 32
0+0+4+8+0+32 = 44
*/
cout << (10 >> 1) << endl;
}