位运算之bit_xor、bit_not、bit_and、bit_or
一、bit_xor
1、头文件 #include <functional>
2、模板
template <class T> struct bit_xor;
template <class T = void> struct bit_xor;
3、返回两个参数按位XOR的结果(公共成员函数)
4、使用
示例1:
1 #include <algorithm>
2 #include <functional> // to include bit_xor
3 #include <iostream>
4 #include <iterator>
5 using namespace std;
6
7 int main()
8 {
9 // declaring the values
10 int A[] = { 1, 2, 3, 4, 5, 6 };
11 int B[] = { 6, 7, 8, 4, 5, 0 };
12 int n = 6;
13 // defining result
14 int result[n];
15
16 // transform is used to apply bitwise_xor
17 // on the arguments A and B
18 transform(A, end(A), B,
19 result, bit_xor<int>());
20
21 // printing the resulting array
22 cout << "A xor B = ";
23 for (const int& answer:result)
24 cout << ' ' << answer;
25
26 return 0;
27 }
输出:
29 A xor B = 7 5 11 0 0 6
示例2:
1 // C++ program to illustrate bit_xor in C++
2
3 #include <algorithm>
4 #include <functional>
5 #include <iostream>
6 #include <iterator>
7 using namespace std;
8
9 int main()
10 {
11 // declaring the values
12 int A[] = { 0, 0xff, 15, 22 };
13 int B[] = { 1, 255, 0xfa, 0x16 };
14 int n = 4;
15 // defining result
16 int result[n];
17
18 // transform is used to apply bitwise_xor
19 // on the arguments A and B
20 transform(A, end(A), B,
21 result, bit_xor<int>());
22
23 // printing the resulting array
24 cout << "A xor B = ";
25 for (const int& answer:result)
26 cout << ' ' << answer;
27
28 return 0;
29 }
输出:
A xor B = 1 0 245 0
二、bit_not
1、头文件 #include <functional>
2、模板
template <class T = void> struct bit_not;
3、实现
1 constexpr T operator()(const T& arg) const
2 {
3 return ~arg;
4 }
4、返回其参数按位NOT的结果(公共成员函数)
三、bit_and
1、头文件 #include <functional>
2、模板
template <class T> struct bit_and;
template <class T = void> struct bit_and;
3、实现
1 template <class T> struct bit_and {
2 T operator() (const T& x, const T& y) const {return x&y;}
3 typedef T first_argument_type;
4 typedef T second_argument_type;
5 typedef T result_type;
6 };
4、二进制函数对象类,其调用返回在其两个参数之间应用按位“和”运算的结果(由运算符返回)和)。
5、使用
1 // bit_and example
2 #include <iostream> // std::cout
3 #include <functional> // std::bit_and
4 #include <algorithm> // std::transform
5 #include <iterator> // std::end
6
7
8 int main () {
9 int values[] = {100,200,300,400,500};
10 int masks[] = {0xf,0xf,0xf,255,255};
11 int results[5];
12
13 std::transform (values, std::end(values), masks, results, std::bit_and<int>());
14
15 std::cout << "results:";
16 for (const int& x: results)
17 std::cout << ' ' << x;
18 std::cout << '\n';
19
20 return 0;
21 }
输出:
results: 4 8 12 144 244
四、bit_or
1、头文件 #include <functional>
2、模板
template <class T> struct bit_or
3、实现
1 template <class T> struct bit_or {
2 T operator() (const T& x, const T& y) const {return x|y;}
3 typedef T first_argument_type;
4 typedef T second_argument_type;
5 typedef T result_type;
6 };
4、二进制函数对象类,其调用返回在其两个参数之间应用按位“或”运算的结果(由运算符返回)|)。
5、使用
1 // bit_or example
2 #include <iostream> // std::cout
3 #include <functional> // std::bit_or
4 #include <numeric> // std::accumulate
5 #include <iterator> // std::end
6
7 int main () {
8 int flags[] = {1,2,4,8,16,32,64,128};
9 int acc = std::accumulate (flags, std::end(flags), 0, std::bit_or<int>());
10 std::cout << "accumulated: " << acc << '\n';
11 return 0;
12 }
输出:
accumulated: 255
本文来自博客园,作者:Mr-xxx,转载请注明原文链接:https://www.cnblogs.com/MrLiuZF/p/14673670.html