📖 Scenario: Imagine you are working with a simple device that uses an 8-bit register to control its features. Each bit in this register can be turned on (1) or off (0) to enable or disable a feature.You need to write a program that can set, clear, or toggle a specific bit in this 8-bit register based on user commands.
🎯 Goal: Build a C program that starts with an 8-bit register value, then uses bitwise operations to set, clear, or toggle a specific bit as instructed.
📋 What You'll Learn
Create an 8-bit unsigned integer variable called register_value with the initial value 0b00001100 (decimal 12).
Create an integer variable called bit_position and set it to 2.
Write code to set the bit at bit_position in register_value.
Write code to clear the bit at bit_position in register_value.
Write code to toggle the bit at bit_position in register_value.
Print the final value of register_value in binary format after each operation.
💡 Why This Matters
🌍 Real World
Bit manipulation is used in embedded systems, device drivers, and low-level programming to control hardware features efficiently.
💼 Career
Understanding bitwise operations is essential for roles in embedded software development, firmware engineering, and systems programming.
Progress0 / 4 steps
1
DATA SETUP: Create the initial 8-bit register variable
Create an 8-bit unsigned integer variable called register_value and set it to the binary value 0b00001100.
DSA C
Hint
Use uint8_t from stdint.h to create an 8-bit unsigned integer.
2
CONFIGURATION: Define the bit position to modify
Create an integer variable called bit_position and set it to 2.
DSA C
Hint
Use a simple int variable to store the bit position.
3
CORE LOGIC: Set, clear, and toggle the bit at bit_position
Inside main(), write code to set the bit at bit_position in register_value, then clear the same bit, and finally toggle the same bit. Use bitwise operators |=, &=, and ^= respectively.
DSA C
Hint
Use 1 << bit_position to create a mask for the bit.
Use |= to set, &= ~ to clear, and ^= to toggle the bit.
4
OUTPUT: Print the register value in binary after each operation
Add code to print the binary value of register_value after setting, clearing, and toggling the bit. Use a loop to print all 8 bits from most significant to least significant bit.
DSA C
Hint
Create a helper function to print bits from 7 down to 0 using bit shifts and masks.
Practice
(1/5)
1. What does the operation num |= (1 << k) do to the integer num?
easy
A. Toggles the k-th bit of num
B. Clears the k-th bit of num to 0
C. Checks if the k-th bit of num is set
D. Sets the k-th bit of num to 1
Solution
Step 1: Understand the bit shift operation
1 << k creates a number with only the k-th bit set to 1 and others 0.
Step 2: Apply bitwise OR to set the bit
Using num |= (1 << k) sets the k-th bit of num to 1 without changing other bits.
Final Answer:
Sets the k-th bit of num to 1 -> Option D
Quick Check:
Set bit = OR with 1 shifted [OK]
Hint: Use OR with 1 shifted to set a bit [OK]
Common Mistakes:
Confusing set with clear operation
Using AND instead of OR to set bits
Misunderstanding bit positions starting from 0
2. Which of the following C statements correctly clears the k-th bit of an integer num?
easy
A. num = num << k;
B. num &= ~(1 << k);
C. num ^= (1 << k);
D. num |= (1 << k);
Solution
Step 1: Understand clearing a bit
Clearing means setting the bit to 0. We use AND with the complement of the bit mask.
Step 2: Analyze the options
num &= ~(1 << k); clears the k-th bit by ANDing with all 1s except 0 at k-th bit.
Final Answer:
num &= ~(1 << k); -> Option B
Quick Check:
Clear bit = AND with NOT of shifted 1 [OK]
Hint: Use AND with NOT of shifted 1 to clear bit [OK]
Common Mistakes:
Using OR instead of AND to clear bits
Forgetting to negate the mask
Using XOR which toggles instead of clears
3. What is the output of the following C code?
int num = 10; // binary 1010
int k = 1;
num ^= (1 << k);
printf("%d", num);
medium
A. 12
B. 10
C. 8
D. 14
Solution
Step 1: Understand initial value and bit positions
num = 10 (binary 1010), k = 1 means second bit from right (0-based).
Step 2: Apply XOR to toggle the k-th bit
Toggle bit 1: bit at position 1 is 1 in 1010, toggling it changes to 0, so binary becomes 1000 which is 8.