Operators Assignment 6 — Bitwise AND on Two Numbers Using the '&' Operator
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Perform Bitwise AND on Two Numbers Using the '&' Operator

📑 On this page:
  • The bitwise AND 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 AND of two integers
  • How binary representation matters for bitwise operations
  • The bit-by-bit truth table: 1 & 1 = 1, everything else is 0
  • How to use AND as a mask to extract specific bits
  • How n & 1 checks even/odd in a single operation

The bitwise AND operator

The bitwise AND operator & compares two integers bit by bit. For each bit position, if both corresponding bits are 1, the result bit is 1. Otherwise, the result bit is 0. That's the whole rule — but its applications are numerous.

Don't confuse & (bitwise AND) with && (logical AND). & operates on each bit of two integers and produces an integer result. && 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 &:

  • Masking — extract specific bits: n & 0x0F gives the low 4 bits of n.
  • Even/odd check — n & 1 is 1 for odd, 0 for even.
  • Testing a bit — n & (1 << k) is nonzero if bit k is set.
  • Clearing bits — n & ~(1 << k) clears bit k.
  • Parity — n & 1 tells you if the least significant bit is set.
💡 In one line: a & b sets each bit of the result to 1 only where both a and b have a 1. It's the "keep only the bits that are on in both" operator.
Bitwise AND in C — comparing bits of two integers with the & operator, showing the binary result.

Truth table

For each pair of bits, the AND result is:

Bit a Bit b a & b
000
010
100
111

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

For example, 12 & 10:

12 = 1100 (binary) 10 = 1010 (binary) ───────────── 12&10 = 1000 (binary) = 8 (decimal)

Reading bit by bit from left to right:

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

Result: 1000 in binary = 8 in decimal. So 12 & 10 = 8.

Complete C Program

Here is the program — bitwise AND on two integers:

Program: Bitwise AND 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 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  = 8
                            

As computed above: 12 (1100) & 10 (1010) = 1000 (8). Correct.

More examples:

Enter two integers: 255 15
a      = 255
b      = 15
a & b  = 15
                            

255 = 11111111, 15 = 00001111, AND is 00001111 = 15 — the mask kept only the low 4 bits of 255, which happen to be all 1s. And:

Enter two integers: 100 7
a      = 100
b      = 7
a & b  = 4
                            

100 = 1100100, 7 = 0000111, AND is 0000100 = 4. The low 3 bits of 100 are 100 (binary), which equals 4 in decimal.

The variant with binary display:

Enter two integers (0-255): 12 10

a       = 0000 1100
b       = 0000 1010
a & b   = 0000 1000

Decimal result: 8
                            

The binary view makes the operation obvious: the only bit where both 12 and 10 have a 1 is bit 3 (the 8's place), so that's the only 1 in the result.

Step-by-Step Explanation

1. Declarations: int a, b, result; — bitwise AND 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 not the bitwise operator — it's the address-of operator. Same symbol, completely different meaning, distinguished by context.

3. The AND operation: result = a & b computes the bitwise AND. Each bit of result is set to 1 only if the corresponding bit is 1 in both a and b. For 12 & 10: 1100 & 1010 = 1000 = 8.

4. Why is it useful? The AND operator is the primary tool for masking — extracting or testing specific bits. For example:

  • n & 1 — extracts the lowest bit (0 if even, 1 if odd).
  • n & 0xFF — extracts the low 8 bits (useful for extracting a byte from a word).
  • n & (1 << k) — tests whether bit k is set (result is 0 or 2k).
  • n & ~0xF — clears the low 4 bits of n.

5. Output format: %d prints the integer result in decimal. To see the binary, you'd need a custom function (as in the variant) or the compiler's binary literal syntax (C23's 0b1100, but that's input format only, not output).

6. Precedence: The bitwise AND has lower precedence than equality, relational, and arithmetic operators. 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 only when both bits are 1. It's the mask operator — use it to extract bits, test flags, or clear specific bits. Watch out for & vs && and for precedence with ==.

Frequently Asked Questions

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

& is bitwise AND — it operates on each bit of two integers and produces an integer. && is logical AND — it treats its operands as booleans (0 = false, nonzero = true) and produces 0 or 1. For example, 5 & 3 = 1 (bitwise), but 5 && 3 = 1 (both nonzero, so result is true = 1). And 4 & 2 = 0 (bitwise, no common bits), but 4 && 2 = 1 (both nonzero). Use & for masking, && for boolean logic.

2. How do I check if a bit is set?

Use n & (1 << k). If the result is nonzero, bit k is set. For example, to test bit 3 (the 8's place): if (n & (1 << 3)). To check whether a number is even: if ((n & 1) == 0). To check odd: if (n & 1). These are common idioms that avoid the (usually slower) modulo operator.

3. How does n & 1 check even/odd?

Because in binary, the last bit of a number is 1 for odd numbers and 0 for even numbers. n & 1 isolates that last bit: it returns 1 if n is odd, 0 if n is even. This works for positive and negative numbers (two's complement), and it's faster than n % 2 on most CPUs. Note: n & 1 returns 1 for negative odd numbers too, while n % 2 returns −1 for negative odd numbers — another reason to prefer &.

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 0 (unless a has bit 0 set). If you meant "the AND 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 = 5, because −1 is all 1s in binary, and x & all-1s = x. −2 & 3 = 2 (...11111110 & 00000011 = 00000010). 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 AND applications: (a) extract specific bits with a mask (n & 0x0F for low 4 bits), (b) clear bits with AND-not (n & ~(1 << k)), (c) check multiple flags at once (if (flags & (FLAG_A | FLAG_B))), (d) implement a fast modulo for powers of 2 (n & (m - 1) instead of n % m when m is a power of 2). Each is a small extension of the same operator.

📖 Related Tutorials

  • Previous: Find the Remainder Using the '%' Operator
  • Next: Bitwise OR on Two Numbers Using '|'
  • Bitwise XOR on Two Numbers Using '^'
  • Bitwise Operators in C — Full Tutorial
  • All 15 Operator Assignments in C
  • Operators and Expressions
  • Operators FAQ

Previous Topic: -->> 5. Find the Remainder Using the '%' Operator   ||   Next Topic: -->> 7. Bitwise OR 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.