Bit wise operators Assignment
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: 170The 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:
000010104. CLEAR
Clear the specified bit to 0.
Example:
Input:
Value = 15
Bit position = 2
Before:
00001111
After CLEAR:
000010115. TOGGLE
Toggle the specified bit.
0 → 11 → 0
Example:
Before:
00001101
Toggle bit 2
After:
000010016. 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 = 17. 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:
10110100Another example:
Before:
10110100
Bit position: 2
Bit value: 0
After:
10110000Important: 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:
00110011The program must change only those two bits.
Data Unit Requirements
The program should correctly handle different widths.
| Data Unit | Width | Suggested C Type |
|---|---|---|
| BIT | 1 bit | unsigned int |
| NIBBLE | 4 bits | unsigned char |
| BYTE | 8 bits | uint8_t |
| HALFWORD | 16 bits | uint16_t |
| WORD | 32 bits | uint32_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–31If the user enters an invalid position:
Invalid bit position!
Please enter a valid position.WRITE validation
Only these values are allowed:
0
1Anything 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 : XXXXXXXXFor example:
Enter BYTE value: 10
Binary:
00001010
Operation: SET
Bit position: 2
Result:
00001110This 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
|
ENDRestrictions
To make this a proper bitwise-operator assignments:
Do not use library functions that directly perform bit manipulation.
Do not convert the number to a binary string for performing operations.
Use bitwise operators and masks for SET, CLEAR, TOGGLE, READ, WRITE and SWAP.
Do not modify bits other than the requested bit(s).
Handle unsigned values.
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.