Bird
Raised Fist0
Embedded Cprogramming~10 mins

NOT for inverting bits in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - NOT for inverting bits
Start with a number
↓
Apply NOT (~) operator
↓
Each bit flips: 0->1, 1->0
↓
Result is bitwise inverted number
↓
Use or store the result
↓
End
The NOT operator (~) flips every bit of a number, turning 0s into 1s and 1s into 0s, producing the bitwise inverse.
Execution Sample
Embedded C
unsigned char x = 0b10101010;
unsigned char y = ~x;
// y now holds 0b01010101
This code flips all bits of x using ~, storing the inverted bits in y.
Execution Table
StepVariableValue (binary)ActionResult (binary)
1x10101010Initial value10101010
2yN/AApply ~ to x01010101
3y01010101Store inverted bits in y01010101
💡 All bits of x are flipped by ~, producing y with inverted bits.
Variable Tracker
VariableStartAfter NOT (~)Final
x101010101010101010101010
yN/A0101010101010101
Key Moments - 3 Insights
Why does applying ~ to 0b10101010 produce 0b01010101?
Because ~ flips each bit: every 1 becomes 0 and every 0 becomes 1, as shown in execution_table step 2.
Does ~ change the original variable x?
No, ~ creates a new inverted value; x remains unchanged as shown in variable_tracker.
Why is the result stored in a new variable y?
Because ~ returns a new value; to keep the inverted bits, you must assign it to a variable like y.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the binary value of y after applying ~ to x?
A01010101
B10101010
C11111111
D00000000
💡 Hint
Check the 'Result (binary)' column in execution_table row for step 2.
At which step does the variable y get its value assigned?
AStep 1
BStep 2
CStep 3
DNo assignment
💡 Hint
Look at the 'Action' column in execution_table where y is stored.
If x was 0b11110000, what would y be after applying ~?
A11110000
B00001111
C00000000
D11111111
💡 Hint
Remember ~ flips each bit; see variable_tracker for how bits invert.
Concept Snapshot
Use ~ operator to invert bits of a number.
Each 0 bit becomes 1, each 1 bit becomes 0.
Original variable stays unchanged.
Assign result to new variable to keep inverted bits.
Example: ~0b10101010 = 0b01010101.
Full Transcript
This visual trace shows how the NOT operator (~) in embedded C flips bits of a number. Starting with x = 0b10101010, applying ~ creates a new value y = 0b01010101 where every bit is inverted. The original x remains the same. The execution table walks through each step: initial value, applying ~, and storing the result. The variable tracker shows how x stays constant while y changes. Common confusions include understanding that ~ flips bits, does not change the original variable, and requires assignment to store the result. The quiz questions reinforce these points by asking about values at specific steps and what happens if input changes.

Practice

(1/5)
1.

What does the ~ operator do in embedded C?

easy
A. It flips every bit of the operand (bitwise NOT).
B. It shifts bits to the left by one position.
C. It adds one to the operand.
D. It compares two values for equality.

Solution

  1. Step 1: Understand the ~ operator

    The ~ operator is the bitwise NOT operator in C, which flips every bit of its operand.
  2. Step 2: Compare with other options

    Other options describe different operations: shifting, addition, and comparison, which are not what ~ does.
  3. Final Answer:

    It flips every bit of the operand (bitwise NOT). -> Option A
  4. Quick Check:

    ~ operator = bitwise NOT [OK]
Hint: Remember ~ flips bits, like turning 0s to 1s and vice versa [OK]
Common Mistakes:
  • Confusing ~ with shift operators
  • Thinking ~ adds or subtracts
  • Mixing ~ with logical NOT (!)
2.

Which of the following is the correct syntax to invert bits of an unsigned integer x in embedded C?

unsigned int x = 5;
unsigned int y = ?;
easy
A. y = x++;
B. y = !x;
C. y = ~x;
D. y = x & ~x;

Solution

  1. Step 1: Identify the bit inversion syntax

    To invert bits, use the bitwise NOT operator ~ directly on the variable.
  2. Step 2: Check other options for correctness

    y = !x; uses logical NOT, which returns 0 or 1, not bit inversion. y = x++; increments x, not invert bits. y = x & ~x; ANDs x with its inverse, resulting in zero.
  3. Final Answer:

    y = ~x; -> Option C
  4. Quick Check:

    Bit inversion syntax = y = ~x; [OK]
Hint: Use ~ directly on variable to invert bits [OK]
Common Mistakes:
  • Using ! instead of ~
  • Using increment operators by mistake
  • Combining & with ~ incorrectly
3.

What is the output of the following code snippet?

unsigned char x = 0b00001111;
unsigned char y = ~x;
printf("%u", y);
medium
A. 240
B. 15
C. 0
D. 255

Solution

  1. Step 1: Understand the initial value of x

    x is 0b00001111, which is decimal 15.
  2. Step 2: Apply bitwise NOT to x

    ~x flips bits: 00001111 becomes 11110000 in 8 bits, which is decimal 240.
  3. Final Answer:

    240 -> Option A
  4. Quick Check:

    ~15 (8-bit) = 240 [OK]
Hint: Invert bits of 00001111 to get 11110000 = 240 decimal [OK]
Common Mistakes:
  • Confusing logical NOT (!) with bitwise NOT (~)
  • Forgetting unsigned type size
  • Assuming output is 15 or 255
4.

Find the error in the following code that attempts to invert bits of num and print the result:

int num = 5;
int inverted = ~num;
printf("%u", inverted);
medium
A. The variable inverted should be unsigned char.
B. The ~ operator cannot be used on int types.
C. Missing semicolon after variable declaration.
D. Using %u to print a signed int causes incorrect output.

Solution

  1. Step 1: Check the type and format specifier

    Variable inverted is signed int, but printed with %u (unsigned). This mismatch can cause wrong output.
  2. Step 2: Verify other options

    The ~ operator works on int types. Semicolons are present. Changing to unsigned char is not necessary but can clarify output.
  3. Final Answer:

    Using %u to print a signed int causes incorrect output. -> Option D
  4. Quick Check:

    Format specifier must match variable type [OK]
Hint: Match printf format (%d for int, %u for unsigned) [OK]
Common Mistakes:
  • Using wrong printf format specifier
  • Thinking ~ can't be used on int
  • Ignoring signed vs unsigned mismatch
5.

You want to invert bits of a 16-bit unsigned integer val but only for the lower 8 bits, leaving the upper 8 bits unchanged. Which code snippet correctly does this?

unsigned short val = 0xABCD;
unsigned short result = ?;
hard
A. result = ~val & 0xFFFF;
B. result = (val & 0xFF00) | (~val & 0x00FF);
C. result = ~val | 0xFF00;
D. result = val ^ 0xFFFF;

Solution

  1. Step 1: Understand the requirement

    Invert only the lower 8 bits of val, keep upper 8 bits as is.
  2. Step 2: Analyze each option

    result = (val & 0xFF00) | (~val & 0x00FF); keeps upper 8 bits (val & 0xFF00) and inverts lower 8 bits (~val & 0x00FF), then combines them. result = ~val & 0xFFFF; inverts all 16 bits. result = ~val | 0xFF00; incorrectly sets upper bits to 1. result = val ^ 0xFFFF; flips all bits (XOR with 0xFFFF).
  3. Final Answer:

    result = (val & 0xFF00) | (~val & 0x00FF); -> Option B
  4. Quick Check:

    Mask upper bits, invert lower bits, combine [OK]
Hint: Mask upper bits, invert lower bits, then combine [OK]
Common Mistakes:
  • Inverting all bits instead of just lower 8
  • Using OR instead of AND for masking
  • Forgetting to combine masked parts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard