The NOT operator flips each bit in a number from 0 to 1 or from 1 to 0. It is used to invert bits in embedded C programming.
NOT for inverting bits in Embedded C
~variable_or_value
The tilde (~) symbol is the NOT operator in C.
It works on each bit individually, flipping 0 to 1 and 1 to 0.
unsigned char a = 0b00001111; unsigned char b = ~a; // b becomes 0b11110000
int x = 5; // binary 00000000 00000000 00000000 00000101 int y = ~x; // y becomes binary 11111111 11111111 11111111 11111010
This program shows how the NOT operator flips bits of an 8-bit value.
#include <stdio.h> int main() { unsigned char val = 0b10101010; unsigned char inverted = ~val; printf("Original: 0x%X\n", val); printf("Inverted: 0x%X\n", inverted); return 0; }
Remember that the NOT operator flips all bits, including unused bits in larger types.
For signed integers, the result may be negative due to two's complement representation.
Use unsigned types to avoid confusion when working with bitwise NOT.
The NOT operator (~) flips every bit in a number.
It is useful for inverting bits in embedded systems.
Use unsigned types to clearly see bit inversion results.
Practice
What does the ~ operator do in embedded C?
Solution
Step 1: Understand the
The~operator~operator is the bitwise NOT operator in C, which flips every bit of its operand.Step 2: Compare with other options
Other options describe different operations: shifting, addition, and comparison, which are not what~does.Final Answer:
It flips every bit of the operand (bitwise NOT). -> Option AQuick Check:
~ operator = bitwise NOT [OK]
- Confusing ~ with shift operators
- Thinking ~ adds or subtracts
- Mixing ~ with logical NOT (!)
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 = ?;
Solution
Step 1: Identify the bit inversion syntax
To invert bits, use the bitwise NOT operator~directly on the variable.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.Final Answer:
y = ~x; -> Option CQuick Check:
Bit inversion syntax = y = ~x; [OK]
- Using ! instead of ~
- Using increment operators by mistake
- Combining & with ~ incorrectly
What is the output of the following code snippet?
unsigned char x = 0b00001111;
unsigned char y = ~x;
printf("%u", y);Solution
Step 1: Understand the initial value of x
x is 0b00001111, which is decimal 15.Step 2: Apply bitwise NOT to x
~x flips bits: 00001111 becomes 11110000 in 8 bits, which is decimal 240.Final Answer:
240 -> Option AQuick Check:
~15 (8-bit) = 240 [OK]
- Confusing logical NOT (!) with bitwise NOT (~)
- Forgetting unsigned type size
- Assuming output is 15 or 255
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);Solution
Step 1: Check the type and format specifier
Variableinvertedis signed int, but printed with %u (unsigned). This mismatch can cause wrong output.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.Final Answer:
Using %u to print a signed int causes incorrect output. -> Option DQuick Check:
Format specifier must match variable type [OK]
- Using wrong printf format specifier
- Thinking ~ can't be used on int
- Ignoring signed vs unsigned mismatch
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 = ?;
Solution
Step 1: Understand the requirement
Invert only the lower 8 bits of val, keep upper 8 bits as is.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).Final Answer:
result = (val & 0xFF00) | (~val & 0x00FF); -> Option BQuick Check:
Mask upper bits, invert lower bits, combine [OK]
- Inverting all bits instead of just lower 8
- Using OR instead of AND for masking
- Forgetting to combine masked parts
Ready to prove it?
Pass the test and this pattern moves unknown
