📖 Scenario: Imagine you are working with a simple electronic device that controls lights. Each light can be turned on or off. You want to control these lights efficiently using numbers where each bit represents a light's state.
🎯 Goal: You will learn how to use bitwise operations to turn lights on and off by changing specific bits in a number.
📋 What You'll Learn
Create an integer variable called lights to represent the state of 8 lights.
Create an integer variable called light_to_turn_on to represent which light to turn on.
Use a bitwise OR operation to turn on the specified light in lights.
Print the value of lights after turning on the light.
💡 Why This Matters
🌍 Real World
Bitwise operations are used in electronics, game programming, and network programming to control flags and states efficiently.
💼 Career
Understanding bitwise operations helps in embedded systems, device drivers, and performance-critical software development.
Progress0 / 4 steps
1
Create the initial lights variable
Create an integer variable called lights and set it to 0 to represent all lights off.
C
Hint
Use int lights = 0; to start with all lights off.
2
Set which light to turn on
Create an integer variable called light_to_turn_on and set it to 3 to represent the third light.
C
Hint
Use int light_to_turn_on = 3; to pick the third light.
3
Turn on the selected light using bitwise OR
Use a bitwise OR operation to turn on the light at position light_to_turn_on in lights. Write lights = lights | (1 << light_to_turn_on);
C
Hint
Use lights = lights | (1 << light_to_turn_on); to turn on the light.
4
Print the lights variable
Print the value of lights using printf to see which lights are on.
C
Hint
Use printf("%d\n", lights); to print the number.
Practice
(1/5)
1. Why are bitwise operations needed in C programming?
easy
A. To handle string concatenation
B. To manipulate individual bits for efficient memory and speed
C. To perform floating-point arithmetic
D. To create graphical user interfaces
Solution
Step 1: Understand bitwise operations purpose
Bitwise operations work on individual bits of data, allowing precise control.
Step 2: Identify their main use
They are used for tasks needing fast and memory-efficient manipulation, like device control.
Final Answer:
To manipulate individual bits for efficient memory and speed -> Option B
Quick Check:
Bitwise operations = bit-level control [OK]
Hint: Bitwise = bit-level control, not math or strings [OK]
Common Mistakes:
Confusing bitwise with arithmetic operations
Thinking bitwise is for string handling
Assuming bitwise is for UI tasks
2. Which of the following is the correct bitwise AND operator in C?
easy
A. &
B. &&
C. |
D. ||
Solution
Step 1: Recall bitwise AND operator
The bitwise AND operator is a single ampersand: &.
Step 2: Differentiate from logical AND
Logical AND uses double ampersand &&, which is not bitwise.
Final Answer:
& -> Option A
Quick Check:
Bitwise AND = & [OK]
Hint: Single & is bitwise AND, double && is logical AND [OK]
Common Mistakes:
Using && instead of & for bitwise AND
Confusing | with &
Using || which is logical OR
3. What is the output of this C code?
unsigned int x = 5; // binary 0101
unsigned int y = 3; // binary 0011
unsigned int z = x & y;
printf("%u", z);
medium
A. 7
B. 3
C. 5
D. 1
Solution
Step 1: Convert numbers to binary
x = 5 is 0101, y = 3 is 0011 in binary.
Step 2: Apply bitwise AND
0101 & 0011 = 0001 (only bits set in both remain).
Step 3: Convert result to decimal
0001 in decimal is 1.
Final Answer:
1 -> Option D
Quick Check:
5 & 3 = 1 [OK]
Hint: AND keeps bits set in both numbers only [OK]
Common Mistakes:
Adding numbers instead of ANDing
Confusing bitwise AND with OR
Misreading binary values
4. Identify the error in this C code snippet using bitwise operations:
int a = 12;
int b = 5;
int c = a && b;
printf("%d", c);
medium
A. Using logical AND instead of bitwise AND
B. Missing semicolon after variable declaration
C. Incorrect printf format specifier
D. Variables not initialized
Solution
Step 1: Check operator used
The code uses && which is logical AND, not bitwise AND.
Step 2: Understand effect on output
Logical AND returns 1 if both non-zero, else 0; bitwise AND would combine bits.
Final Answer:
Using logical AND instead of bitwise AND -> Option A
Quick Check:
Logical AND (&&) ≠ Bitwise AND (&) [OK]
Hint: Use & for bitwise, && for logical [OK]
Common Mistakes:
Confusing && with &
Assuming && works as bitwise
Ignoring operator difference
5. You want to store multiple boolean flags in a single integer variable to save memory. Which bitwise operation helps you set the 3rd bit (bit index 2) to 1 without changing other bits?
int flags = 0;
hard
A. flags = flags && (1 << 2);
B. flags = flags & (1 << 2);
C. flags = flags | (1 << 2);
D. flags = flags ^ (1 << 2);
Solution
Step 1: Understand setting a bit
To set a bit, use OR with a mask that has that bit set to 1.
Step 2: Analyze each option
flags = flags | (1 << 2); uses OR with (1 << 2), which shifts 1 to bit 2, correctly setting it.
Step 3: Exclude other options
AND would clear bits, XOR toggles bits, && is logical and wrong here.
Final Answer:
flags = flags | (1 << 2); -> Option C
Quick Check:
Set bit with OR and shift [OK]
Hint: Use OR with shifted 1 to set a bit [OK]
Common Mistakes:
Using AND which clears bits
Using XOR which toggles bits
Using logical AND (&&) by mistake
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.