Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) operator&

 

operator& (pronounced as 'bitwise and operator') is an operator to perform a bitwise and.

 

#include <cassert>

int main()
{
  int x = 3;     //0011
  int y = 5;     //0101
  int z = x & y; //0001
  assert(z==1);
}

 

 

 

 

 

Example: bitflags

 

#include <iostream>

void ShowFlags(const int i)
{
  if (i & 1) std::cout << "1\n";
  if (i & 2) std::cout << "2\n";
  if (i & 4) std::cout << "4\n";
  if (i & 8) std::cout << "8\n";
}

int main()
{
  ShowFlags(15);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict