Abstract

The bitwise or expression is used to compute the bit-by-bit Boolean AND of two integer values. 

Usage

Syntax

Syntax Diagrams

Bitwise AND Expression Syntax Diagram

BNF

bitwise-and-expression
::= <bitwise-and-expression> <bitwise-and-operator> <equality-expression>
::= <equality-expression>

EBNF

bitwise-and-expression
::= <equality-expression> ( <bitwise-and-operator> <equality-expression> ) *

Form

bitwise-and-operator
&

Contextual Constraints

bitwise-and-operator
The operands of the AND operator should be of int type. 
The operands of the AND operator will be coerced to int type if possible. 

Semantics

bitwise-and-expression
For each bit position, the result is 0 iff either operand is 0 in that bit position.
For each bit position, the result is 1 iff both operands are 1 in that bit position.
The value of each bit is the boolean AND of that bit of the operands:
Boolean AND
Operands Value
000
010
100
111
The bitwise AND operator uses full evaluation.  It does not short circuit. 

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

        2  0000000000000010
&       7  0000000000000111
   ------  ----------------
        2  0000000000000010
   ======  ================

       15  0000000000001111
&      22  0000000000010110
   ------  ----------------
        6  0000000000000110
   ======  ================

    13302  0011001111110110
&   15714  0011110101100010
   ------  ----------------
    12642  0011000101100010
   ======  ================

   -13987  1100100101011101
&    9139  0010001110110011
   ------  ----------------
      273  0000000100010001
   ======  ================

Reference Links

Bitwise AND Operator <bitwise-and-operator>
Equality Expression <equality-expression>

Other Languages

C Now
C Bitwise AND Expression
C++ Now C++ Bitwise AND Expression
Java Now Java Bitwise AND Expression
PHP Now PHP Bitwise AND Expression