Abstract

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

Usage

Syntax

Syntax Diagrams

Bitwise OR Expression Syntax Diagram

BNF

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

EBNF

bitwise-or-expression
::= <bitwise-xor-expression> ( <bitwise-or-operator> <bitwise-xor-expression> ) *

Form

bitwise-or-operator
→ |

Contextual Constraints

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

Semantics

bitwise-or-expression
For each bit position, the result is 0 iff both operands are 0 in that bit position.
For each bit position, the result is 1 iff either operand is 1 in that bit position.
The value of each bit is the boolean OR of that bit of the operands:
Boolean OR
Operands Value
000
011
101
111
The bitwise OR 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
   ------  ----------------
        7  0000000000000111
   ======  ================

       15  0000000000001111
|      22  0000000000010110
   ------  ----------------
       31  0000000000011111
   ======  ================

     -311  1111111011001001
|    7519  0001110101011111
   ------  ----------------
      -33  1111111111011111
   ======  ================

    -5703  1110100110111001
|   -6300  1110011101100100
   ------  ----------------
    -4099  1110111111111101
   ======  ================

Reference Links

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

Other Languages

C Now
C Bitwise Or Expression
C++ Now C++ Bitwise Or Expression
Java Now Java Bitwise Or Expression
PHP Now PHP Bitwise Or Expression