AND for Masking Bits in Embedded C
📖 Scenario: You are working with a microcontroller that reads sensor data packed into a single byte. Each bit in this byte represents a different sensor's status (on or off). You need to extract the status of specific sensors by masking bits.
🎯 Goal: Learn how to use the AND operator to mask bits and extract specific sensor statuses from a byte.
📋 What You'll Learn
Create a variable called
sensor_data with the exact value 0b10101100 (binary literal).Create a variable called
mask with the exact value 0b00000100 to select the third bit.Use the AND operator with
sensor_data and mask to create a variable called masked_result.Print the value of
masked_result as an integer.💡 Why This Matters
🌍 Real World
Microcontrollers often pack multiple sensor statuses into one byte. Masking bits helps read each sensor's status separately.
💼 Career
Embedded systems engineers use bit masking to efficiently handle hardware signals and sensor data.
Progress0 / 4 steps