C++中的BITY与函数

比特和 是C++中的Buffin函数,用于在应用BITWISEGO及其参数(如运算符返回)后返回值。

null
template  struct bit_and 
{
  T operator() (const T& a, const T& b) const {return a&b;}
  typedef T type of first_argument;
  typedef T type of second_argument;
  typedef T result_type;
};

T 是所有参数的类型。

注:

  1. 此类对象可用于标准算法,例如 使改变 或者积累。
  2. 成员函数(’operator()’)返回 按位_和 它的论点是什么。

我们必须包含“函数”和“算法”库,以分别使用位_和和变换,否则它们将不起作用。

下面是展示该系统工作原理的程序 比特和 功能: 方案1:

// C++ program to show the
// functionality of bit_and
#include <algorithm> // transform
#include <functional> // bit_and
#include <iostream> // cout
#include <iterator> // end
using namespace std;
int main()
{
// declaring the values
int xyz[] = { 500, 600, 300, 800, 200 };
int abc[] = { 0xf, 0xf, 0xf, 255, 255 };
int n = 5;
// defining results
int results[n];
// transform is used to apply
// bitwise_and on the arguments
transform(xyz, end(xyz), abc,
results, bit_and< int >());
// printing the resulting array
cout << "Results:" ;
for ( const int & x : results)
cout << ' ' << x;
return 0;
}


输出:

Results: 4 8 12 32 200

方案2:

// C++ program to show the
// functionality of bit_and
#include <algorithm> // transform
#include <functional> // bit_and
#include <iostream> // cout
#include <iterator> // end
using namespace std;
int main()
{
// declaring the values
int xyz[] = { 0, 1100 };
int abc[] = { 0xf, 0xf };
// defining results
int results[2];
// transform is used to apply
// bitwise_and on the arguments
transform(xyz, end(xyz), abc,
results, bit_and< int >());
// printing the resulting array
cout << "Results:" ;
for ( const int & x : results)
cout << ' ' << x;
return 0;
}


输出:

Results: 0 12

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享