//5 – 6 – 1 bitwise AND operator
#include <iostream>
using namespace std;
/*
& if there has one 0, there must be 0
&& if there has one false, there must be false
*/
int main() {
//1.bitwise AND operator’s definition
int a = 0b1010; //10
//0b is mean that binary
int b = 0b0110; //6
// 0010
cout << (a & b) << endl;//2
cout << “————-” << endl;
//2.Parity odd number and even number
cout << (5 % 2) << endl; // we can know this 5 is also an odd number by moding 2;
//this kind of manipulator’s priority is high and the efficiency is high too
cout << (5 & 1) << endl;//we can know this 5 is also an odd number by bitwising AND 1
//this kind of manipulator’s priority is lower than the mod manipulate,but the efficiency is the same as bitwise AND manipulator
//0b101
//0b001
//On the binary, if the last bit have the odd number,the whole value must be the odd number
cout << “————-” << endl;
//3.retrive the last 5 bits of a number
int c = 0b1010010101001;
cout << (c & 0b11111) << endl; // the last 5 bits set binary’s 11111
cout << “————-” << endl;
//4.let the last 5 bits into the zero
int d = 0b11111111111111111111111111100000;
cout << (c & d) << endl; // the output must be 0b1010010100000, which is the last 5 bits of c turned into 0,the dec is 5280
cout << “————-” << endl;
//5.clean the last continuous 1 on the latter bits
int e = 0b101010111111;
//e+1 = 0b101011000000
// & = 0b101010000000
//0b101010000000
cout << (e & (e + 1)) << endl;
cout << “————-” << endl;
//6.to judge the power(幂) of 2
int f = 0b100000000;
//f-1= 0b011111111
// & = 0b000000000 = 0
//((f>0)&&( f & (f-1))==0;if equal to 0,the f value must be the power of 2
cout << (f-1) << endl;
cout << (f & (f – 1)) << endl;
return 0;
}
//5 – 6 – 2 bitwise OR operator
#include <iostream>
using namespace std;
/*
| if there has one 1, there must be 1
|| if there has one true, there must true
*/
int main() {
//1.the definition of bitwise OR
int a = 0b1010; //10
int b = 0b0110; //6
// | = 0b1110; // 14
cout << (a | b) << endl;
cout << “———” << endl;
//2.set the flag bit(标记位)
int c = 0b100111; // 39
// 0b101111 // 47
cout << (c | (0b1000)) << endl;
cout << “———” << endl;
//3.unset the flag bit
//0b100111 ->0b100110
int d = 0b000001;
// use or operator,and let the last bit become 1 ,then minus the last 1, the operator is adapt to all the number included the last bit has 1 or not has 1
cout << ((c | d) – d) << endl;
cout << “———” << endl;
//4.let last continuous bits’ 0 turned into 1
int e = 0b1010010000;
//e-1 = 0b1010001111;
// & -> 0b1010011111;
int f = 0b1010011111;
// 0b1010010000 -> 0b1010011111;
cout << f << endl;
cout << (e | (e – 1)) << endl;
return 0;
}