Operators Assignment 7 — Bitwise OR on Two Numbers Using the '|' Operator
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Perform Bitwise OR on Two Numbers Using the '|' Operator

📑 On this page:
  • The bitwise OR operator
  • Truth table
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How the | operator computes the bitwise OR of two integers
  • How binary representation matters for bitwise operations
  • The bit-by-bit truth table: 0 | 0 = 0, everything else is 1
  • How to use OR to set specific bits (the opposite of masking)
  • How to combine multiple flags into one value

The bitwise OR operator

The bitwise OR operator | compares two integers bit by bit. For each bit position, if either corresponding bit is 1, the result bit is 1. The result bit is 0 only when both input bits are 0. This is the complement of AND's behavior.

Don't confuse | (bitwise OR) with || (logical OR). | operates on each bit of two integers and produces an integer. || operates on two boolean values (treating nonzero as true) and produces 0 or 1. They look similar but are completely different operators.

Common uses of |:

  • Setting bits — turn specific bits on: n | (1 << k) sets bit k.
  • Combining flags — merge several flags into one integer: READ | WRITE | EXEC.
  • Merging bit masks — combine overlapping bit patterns.
  • Building packed data — encode multiple small values into one integer (e.g., RGB colors).
💡 In one line: a | b sets each bit of the result to 1 wherever either a or b has a 1. It's the "keep every bit that's on in either operand" operator.
Bitwise OR in C — comparing bits of two integers with the | operator, showing the binary result.

Truth table

For each pair of bits, the OR result is:

Bit a Bit b a | b
000
011
101
111

In words: the result is 0 only when both input bits are 0. Otherwise it's 1. Applying this rule at every bit position gives the bitwise OR.

For example, 12 | 10:

12 = 1100 (binary) 10 = 1010 (binary) ───────────── 12|10 = 1110 (binary) = 14 (decimal)

Reading bit by bit from left to right:

  • Bit 3 (8's place): 1 | 1 = 1
  • Bit 2 (4's place): 1 | 0 = 1
  • Bit 1 (2's place): 0 | 1 = 1
  • Bit 0 (1's place): 0 | 0 = 0

Result: 1110 in binary = 14 in decimal. So 12 | 10 = 14.

Complete C Program

Here is the program — bitwise OR on two integers:

Program: Bitwise OR of two integers

#include <stdio.h>

int main(void) {
    int a, b, result;

    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    result = a | b;

    printf("a      = %d\n", a);
    printf("b      = %d\n", b);
    printf("a | b  = %d\n", result);

    return 0;
}
                                

A variant that also shows the binary representation:

Variant: Show binary representation

#include <stdio.h>

void print_binary(int n) {
    for (int i = 7; i >= 0; i--) {
        printf("%d", (n >> i) & 1);
        if (i % 4 == 0 && i != 0) printf(" ");
    }
    printf("\n");
}

int main(void) {
    int a, b, result;

    printf("Enter two integers (0-255): ");
    scanf("%d %d", &a, &b);

    result = a | b;

    printf("\na       = ");
    print_binary(a);
    printf("b       = ");
    print_binary(b);
    printf("a | b   = ");
    print_binary(result);
    printf("\nDecimal result: %d\n", result);

    return 0;
}
                                

The print_binary helper iterates from bit 7 down to bit 0, using (n >> i) & 1 to isolate each bit — the AND operator appears here twice (once to compute the OR result, once to extract bits for display). This is the standard way to display binary in C, since printf has no %b specifier.

Program Output

Sample run of the basic program:

Enter two integers: 12 10
a      = 12
b      = 10
a | b  = 14
                            

As computed above: 12 (1100) | 10 (1010) = 1110 (14). Correct.

More examples:

Enter two integers: 1 2
a      = 1
b      = 2
a | b  = 3
                            

1 = 01, 2 = 10, OR is 11 = 3. This is the classic "combine flags" pattern — two separate flags merge into one value that has both bits set.

And:

Enter two integers: 100 7
a      = 100
b      = 7
a | b  = 103
                            

100 = 1100100, 7 = 0000111, OR is 1100111 = 103. The low 3 bits of 100 are 100 (binary), and OR with 111 gives 111, which equals 7 in decimal. The upper bits of 100 are preserved.

The variant with binary display:

Enter two integers (0-255): 12 10

a       = 0000 1100
b       = 0000 1010
a | b   = 0000 1110

Decimal result: 14
                            

The binary view makes the operation obvious: wherever either 12 or 10 has a 1, the result has a 1. Only bit 0 (the 1's place) is 0 in both, so it's 0 in the result.

Step-by-Step Explanation

1. Declarations: int a, b, result; — bitwise OR works on integers (or any integer type: char, short, long). The result has the same type as the operands after the usual arithmetic promotions.

2. Input: scanf("%d %d", &a, &b) reads two integers. The & here is the address-of operator, not the bitwise AND operator — same symbol, different context.

3. The OR operation: result = a | b computes the bitwise OR. Each bit of result is set to 1 if the corresponding bit is 1 in either a or b. For 12 | 10: 1100 | 1010 = 1110 = 14.

4. Why is it useful? The OR operator is the primary tool for setting bits. For example:

  • n | 1 — sets the lowest bit to 1, making n odd (if it was even).
  • n | (1 << k) — sets bit k to 1, leaving other bits unchanged.
  • flags | WRITE — turns on the WRITE flag, regardless of its previous state.
  • RED | GREEN — combines two color components into one integer.

5. Output format: %d prints the integer result in decimal. To see the binary, you'd need a custom function (as in the variant).

6. Precedence: The bitwise OR has lower precedence than most other operators, including equality, relational, and even bitwise AND and XOR. So a | b == c means a | (b == c), not (a | b) == c — a classic source of bugs. Always parenthesize: (a | b) == c.

🎯 Key idea: a | b is bit-by-bit: 1 if either bit is 1. It's the "set bit" operator — use it to turn specific bits on, combine flags, or merge bit patterns. Watch out for | vs || and for precedence with ==.

Frequently Asked Questions

1. What's the difference between | and ||?

| is bitwise OR — it operates on each bit of two integers and produces an integer. || is logical OR — it treats its operands as booleans (0 = false, nonzero = true) and produces 0 or 1. For example, 5 | 3 = 7 (bitwise), but 5 || 3 = 1 (both nonzero, so result is true = 1). Use | for bit manipulation, || for boolean logic.

2. How do I set a specific bit using OR?

Use n | (1 << k). This sets bit k to 1, leaving all other bits unchanged. For example, to set bit 3 (the 8's place): n = n | (1 << 3) or equivalently n |= (1 << 3). To set multiple bits at once, OR the masks together: n | 0b1010 sets bits 1 and 3. This is the fundamental way to turn on flags in C.

3. How do I combine multiple flags into one value?

Use OR: flags = READ | WRITE | EXEC;. Each flag is a power of 2 (1, 2, 4, 8, …), so they occupy distinct bit positions. OR combines them without conflict — READ | WRITE gives a value with both bits set. To test whether a flag is set: if (flags & READ). To remove a flag: flags &= ~READ. This pattern is ubiquitous in systems programming (permissions, file modes, feature bits).

4. Why is a | b == c often a bug?

Because == has higher precedence than |. So a | b == c parses as a | (b == c), not (a | b) == c. Since b == c evaluates to 0 or 1, the result is usually a | 0 = a or a | 1. If you meant "the OR equals c", write (a | b) == c. Better yet, always parenthesize bitwise expressions.

5. Can I use | on negative numbers?

Yes — the operator works on the binary representation of negative numbers (which is two's complement on all modern systems). For example, −1 | 5 = −1, because −1 is all 1s in binary, and OR with anything gives all 1s. −2 | 3 = −1 (...11111110 | 00000011 = ...11111111 = −1). The results depend on the bit patterns, which for negatives are implementation-defined (but in practice two's complement, uniform across platforms).

💡 Tip: once this works, try these classic OR applications: (a) set flags with flags |= FLAG_X, (b) combine color components (red | (green << 8) | (blue << 16) for RGB), (c) turn on the lowest bit to force a number odd (n | 1), (d) merge two 4-bit values into one byte ((hi << 4) | lo). Each is a small extension of the same operator.

📖 Related Tutorials

  • Previous: Bitwise AND on Two Numbers Using '&'
  • Next: Bitwise XOR on Two Numbers Using '^'
  • Bitwise Left Shift Using '<<'
  • Bitwise Operators in C — Full Tutorial
  • All 15 Operator Assignments in C
  • Operators and Expressions
  • Operators FAQ

Previous Topic: -->> 6. Bitwise AND on Two Numbers Using the '&' Operator   ||   Next Topic: -->> 8. Bitwise XOR on Two Numbers Using the '^' Operator


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.