Form

bitwise-and-operator
&

Domain

bitwise-and-operator
&: integer × integer → integer
&: long × long → long
&: unsigned integer × unsigned integer → unsigned integer
&: unsigned long × unsigned long → unsigned long

Semantics

bitwise-and-operator
For each bit position, the result is 0 iff either operand is 0.
For each bit position, the result is 1 iff both operands are 1.
For each bit position, the result is the boolean AND of the corresponding operand bits:
Boolean AND
OperandsResult
000
010
100
111

Side Effects

bitwise-and-operator
The bitwise and operators have no side effects.

Commutativity

bitwise-and-operator
The bitwise and operator is mathematically commutative.

Associativity

bitwise-and-operator
The bitwise and operator associates syntactically left to right.
The bitwise and operator is mathematically associative.

Precedence


Higher
Subscript [ ]
UnaryPostfix ++ --
Prefix ++ -- + - ! ~ ()
Arithmetic Multiplicative * / %
Additive + -
Shift >> <<
ComparisonRelational < <= > >=
Equality == !=
Bitwise Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Boolean Boolean AND &&
Boolean OR ||
Conditional ? :
Assignment = += -= *= /= %= &= |= ^= <<= >>=
Sequence ,

Example

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void Display (short A, short B);
void ToBinary (char * Display, short N);

short main () {
  time_t T;
  srand (time (&T));
  Display (2, 7);
  Display (15, 22);
  Display (rand() - 16768, rand() - 16768);
  Display (rand() - 16768, rand() - 16768);
  return 0;
}

void Display (short I, short J) {
  char Binary [17];
  short K;
  K = I & J;
  ToBinary (Binary, I);
  printf ("   %6i  %s\n", I, Binary);
  ToBinary (Binary, J);
  printf ("&  %6i  %s\n", J, Binary);
  printf ("   ------  ----------------\n");
  ToBinary (Binary, K);
  printf ("   %6i  %s\n", K, Binary);
  printf ("   ======  ================\n\n");
}

void ToBinary (char * Display, short N) {
  short I;
  I = 15;
  while (I >= 0) {
    Display [I] = (N & 1) + '0';
    N >>= 1;
    --I;
  }
  Display [16] = 0;
}

Output


    

Example

#include <stdio.h>
#include <stdlib.h>

void Pack (int * Code, unsigned month, unsigned day, int year);
void Unpack (int Code, unsigned * Month, unsigned * Day, int * Year);

int main () {
  int year;
  unsigned month;
  unsigned day;
  int Packed;
  
  year = 1900 + (rand () % 200);
  month = 1 + rand () % 12;
  day = 1 + rand () % 28;
  
  printf ("Initial:  %i/%u/%u\n", month, day, year);
  Pack (&Packed, month, day, year);
  printf ("Packed:   %il\n", Packed);
  Unpack (Packed, &month, &day, &year);
  printf ("Unpacked: %i/%u/%u\n", month, day, year);
  
  return 0;
}

void Pack (int * Code, unsigned Month, unsigned Day, int Year) {
  *Code = Year << 9
         | (Month & 15) << 5 
         | (Day & 31);
}

void Unpack (int Code, unsigned * Month, unsigned * Day, int * Year) {
  *Year = Code >> 9;
  *Month = (Code >> 5) & 15;
  *Day = Code & 31;
}

Output


    

Reference Links

Bitwise AND Expression <Bitwise-and-expression>
Bitwise OR Operator Bitwise-or-operator
Bitwise XOR Operator Bitwise-xor-operator

Other Languages

C Now
C Bitwise AND Operators
C++ Now C++ Selection Bitwise AND Operators
Java Now Java Bitwise AND Operators
PHP Now PHP Bitwise AND Operators