I am Learning the bitwise operator from a course downloaded from udemy but It is very confusing. They teach by this example which is very confusing to read and understand, is there a course or example which will teach bitwise operator in simple way? and also what is the use of bitwise operator? is it important to take headache from it or not?
The example:
#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;
}
Aucun commentaire:
Enregistrer un commentaire