Skip to study material
← Question index

QUESTION 60 · Operators

Explain bitwise operators with a worked binary example.

5 marks

Answer in brief

Bitwise operators work on individual bits of integer values. Unsigned integers make introductory bit examples easier to understand.

Operators

  1. AND (&): Sets a result bit when both input bits are 1.
  2. OR (: )|Sets a result bit when either input bit is 1.
  3. XOR (^): Sets a result bit when the input bits differ.
  4. Complement (~): Reverses every bit of the promoted operand.
  5. Left shift (<<): Moves bits left and fills the new low positions with zeros for unsigned values.
  6. Right shift (>>): For unsigned values, moves bits right and fills the new high positions with zeros.

Worked example

Use a = 12U and b = 10U. Their low four bits are 1100 and 1010. a & b gives 1000, or 8. a | b gives 1110, or 14. a ^ b gives 0110, or 6. Also, 12U << 1 gives 24 and 12U >> 1 gives 6.

Uses

  1. Flags: Store several yes/no settings in different bits of one integer.
  2. Masks: Select, set or clear required bit positions.
  3. Low-level programming: Work with data formats and hardware interfaces where bit positions matter.

Important points

~12U reverses all bits of unsigned int, not only the four displayed bits. A shift count must be non-negative and smaller than the width of the promoted left operand. Signed shifts have extra restrictions. & and | operate on bits; && and || operate on truth values.

Reference tables

↔ Scroll horizontally on a small screen
Unsigned a = 12 and b = 10
ExpressionLow four bitsDecimal result
a & b10008
a | b111014
a ^ b01106
a << 11100024
a >> 101106
↔ Scroll horizontally on a small screen
Bitwise truth table
a bitb bita & ba | ba ^ b
00000
01011
10011
11110

Diagram or flowchart

For the low four bits of unsigned values 12 (1100) and 10 (1010), AND produces 1000, OR produces 1110 and XOR produces 0110.
Bitwise AND, OR and XOR · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    unsigned int a = 12U, b = 10U;
    printf("AND = %u\n", a & b);
    printf("OR = %u\n", a | b);
    printf("XOR = %u\n", a ^ b);
    printf("left shift = %u\n", a << 1);
    printf("right shift = %u\n", a >> 1);
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

AND = 8
OR = 14
XOR = 6
left shift = 24
right shift = 6

Key points

  • A displayed four-bit example does not imply a four-bit C integer type.
  • Use unsigned values for these bit-pattern examples.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • March/April 2023 · Q3(b) · 6 marks

    List bitwise operators and explain each with its precedence and associativity.

    Paper P05 · page 1
  • March/April 2023 · Q2(a) · 5 marks

    Explain bitwise operators in detail.

    Paper P07 · page 1
  • January/February 2024 · Q1(a) · 1 marks

    List bitwise operators in C.

    Paper P08 · page 1
View all matching exam references →