Bit wise operators Assignment

27 Sep 2026 Anvesh G 0 C Programming

Bitwise Operators practical assignment: Take runtime input, bit-level manipulation, and different data units rather than simply writing isolated &, |, ^ examples.

Bitwise Operators – Practical Assignment

Objective

Develop a menu-driven C program that accepts input from the user at runtime and performs different bit-level operations on the selected data unit.

The program should demonstrate manipulation at:

  • BIT → 1 bit

  • NIBBLE → 4 bits

  • BYTE → 8 bits

  • HALFWORD → 16 bits

  • WORD → 32 bits

The operations to be supported are:

  • SET a bit

  • CLEAR a bit

  • TOGGLE a bit

  • READ a bit

  • WRITE a bit

  • SWAP two bits


Requirement Specification

1. Runtime Input

The program must take all required inputs from the user during program execution.

The user should first select the data unit:

Select Data Unit:

1. BIT
2. NIBBLE
3. BYTE
4. HALFWORD
5. WORD

Enter your choice:

Then the program should ask for the corresponding value.

Example:

Enter BYTE value: 170

The program should internally represent the value in binary and perform the requested operation.


2. Operation Menu

After selecting the data unit, display:

Select Operation:

1. SET
2. CLEAR
3. TOGGLE
4. READ
5. WRITE
6. SWAP

Enter your choice:

3. SET

Set the specified bit to 1.

Requirement

The user should enter:

Enter bit position:

The program must set that bit to 1 without modifying other bits.

Example:

Input:
Value = 8
Bit position = 1

Binary:
00001000

After SET:
00001010

4. CLEAR

Clear the specified bit to 0.

Example:

Input:
Value = 15
Bit position = 2

Before:
00001111

After CLEAR:
00001011

5. TOGGLE

Toggle the specified bit.

  • 0 → 1

  • 1 → 0

Example:

Before:
00001101

Toggle bit 2

After:
00001001

6. READ

Read the value of a particular bit.

The program should not modify the original value.

Example:

Value:
10110100

Bit position: 5

Result:
Bit 5 = 1

7. WRITE

Write either 0 or 1 to a specified bit.

The user should provide:

Enter bit position:
Enter bit value (0/1):

Example:

Before:
10110100

Bit position: 2
Bit value: 1

After:
10110100

Another example:

Before:
10110100

Bit position: 2
Bit value: 0

After:
10110000

Important: WRITE is different from SET/CLEAR because the user explicitly decides whether the bit should become 0 or 1.


8. SWAP

Swap the values of two bit positions.

The user should enter:

Enter first bit position:
Enter second bit position:

Example:

Before:
10110010

Swap bit 7 and bit 1

After:
00110011

The program must change only those two bits.


Data Unit Requirements

The program should correctly handle different widths.

Data UnitWidthSuggested C Type
BIT1 bitunsigned int
NIBBLE4 bitsunsigned char
BYTE8 bitsuint8_t
HALFWORD16 bitsuint16_t
WORD32 bitsuint32_t

For an embedded-C-oriented assignment, using:

#include <stdint.h>

and uint8_t, uint16_t, and uint32_t is preferred.


Important Validation Requirements

The program must validate the user's input.

Bit-position validation

For example:

BIT       → position 0
NIBBLE    → 0–3
BYTE      → 0–7
HALFWORD  → 0–15
WORD      → 0–31

If the user enters an invalid position:

Invalid bit position!
Please enter a valid position.

WRITE validation

Only these values are allowed:

0
1

Anything else should produce:

Invalid bit value!
Enter only 0 or 1.

Display Requirement

For every operation, display:

Original Value : XXXXXXXX
Operation      : SET
Bit Position   : 3
Result         : XXXXXXXX

For example:

Enter BYTE value: 10

Binary:
00001010

Operation: SET
Bit position: 2

Result:
00001110

This makes it easier to verify the bit-level operation.


Additional Requirement 

The program should not use separate logic for every bit position.

For example, avoid:

if (bit == 0)
    ...
else if (bit == 1)
    ...

The operation should be implemented using bitwise operators and bit masks.

Students are expected to use:

&
|
^
~
<<
>>

appropriately.


Expected Program Flow

             START
               |
               ↓
       Select Data Unit
               |
               ↓
          Enter Value
               |
               ↓
       Display Binary Value
               |
               ↓
       Select Operation
               |
       ┌───────┼────────┐
       ↓       ↓        ↓
      SET    CLEAR    TOGGLE
       ↓       ↓        ↓
      READ    WRITE    SWAP
       └───────┼────────┘
               ↓
        Validate Input
               |
               ↓
       Perform Operation
               |
               ↓
       Display Before/After
               |
               ↓
        Continue? Y/N
               |
              END

Restrictions

To make this a proper bitwise-operator assignments:

  1. Do not use library functions that directly perform bit manipulation.

  2. Do not convert the number to a binary string for performing operations.

  3. Use bitwise operators and masks for SET, CLEAR, TOGGLE, READ, WRITE and SWAP.

  4. Do not modify bits other than the requested bit(s).

  5. Handle unsigned values.

  6. Validate bit positions according to the selected data width.

Bonus requirement 

Implement the operations as reusable functions:

set_bit()
clear_bit()
toggle_bit()
read_bit()
write_bit()
swap_bits()

and then create the menu-driven application around them.

This turns the task from a basic bitwise exercise into a good Embedded C interview/practical assignment covering masks, shifts, data widths, validation, functions, and runtime input.

Author
BY: Anvesh G

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.