Unary complement operator (a.k.a. bitwise not operator)
Learn Unary complement operator (a.k.a. bitwise not operator) step by step with clear examples and exercises.
Why This Matters
The Unary Complement Operator, also known as the Bitwise NOT Operator, is a fundamental concept in C programming that plays a crucial role in manipulating binary data, checking number properties, and performing various arithmetic operations. In this lesson, we will delve into the intricacies of this operator, understand its importance, and learn how to effectively use it in our C programs.
Importance of Unary Complement Operator
The Unary Complement Operator is essential for several reasons:
- Bit Manipulation: It allows us to manipulate individual bits within a number, which is crucial when working with binary data structures and algorithms.
- Parity Checking: The operator can be used to check if a number is odd or even without using modulo division.
- Sign Comparison: By applying the Unary Complement Operator, we can compare two numbers regardless of their signs, which simplifies certain comparisons in our programs.
- Arithmetic Operations: The operator can be used to perform various arithmetic operations like finding the bitwise complement of a number or flipping bits within a binary representation.
Prerequisites
Before proceeding with the Unary Complement Operator, it's essential to have a strong understanding of:
- Basic C programming concepts (variables, data types, operators)
- Bitwise AND (&), OR (|), and XOR (^) operators
- Shift operators (<< and >>)
- Understanding binary representation and number systems
- Familiarity with the concept of bit manipulation
- Knowledge of C data types like
unsigned intandsigned int
Core Concept
The Unary Complement Operator is represented by the ~ symbol in C programming. It operates on a single operand, flipping each bit from 0 to 1 or vice versa. This operator is crucial for various operations like checking if a number is odd or even, comparing two numbers without worrying about their signs, and manipulating binary data.
Bitwise NOT Operation
To grasp the Bitwise NOT operation better, let's take a closer look at how it works. Given an operand in binary representation:
1010
~
0101
Each bit in the result is opposite to its corresponding bit in the operand.
Unary Complement and Two's Complement
Note that that the Unary Complement Operator works on unsigned integers only, while signed integers use two's complement representation. If you apply the Bitwise NOT on a signed integer, it will give you the one's complement value (flipping all bits), but adding 1 to this result will give us the original value in two's complement representation.
Worked Example
Let's write multiple C programs that demonstrate the Unary Complement Operator and its applications:
Program 1: Checking Odd or Even Numbers
#include <stdio.h>
int main() {
int num = 10;
if (num & 1) {
printf("%d is odd.\n", num);
} else {
printf("%d is even.\n", num);
}
return 0;
}
This program uses the Unary Complement Operator indirectly by checking if the least significant bit (LSB) of a number is set or not. If it's set, the number is odd.
Program 2: Swapping Two Numbers Without Temporary Variables
#include <stdio.h>
void swap(unsigned int *a, unsigned int *b) {
*a = (*a) ^ (*b);
*b = (*a) ^ (*b);
*a = (*a) ^ (*b);
}
int main() {
unsigned int x = 10;
unsigned int y = 20;
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
This program uses the XOR operator in combination with the Unary Complement Operator to swap two numbers without using temporary variables.
Common Mistakes
- Forgetting the tilde (~) symbol: Remember that the Unary Complement Operator is represented by the
~symbol. - Using on signed integers: The Unary Complement Operator only works with unsigned integers. If you apply it to a signed integer, you'll get unexpected results. To handle this, use the two's complement representation and add 1 after applying the operator.
- Not understanding binary representation: Familiarize yourself with binary numbers and their conversion to decimal numbers to better understand the Bitwise NOT operation.
- Ignoring the order of operations: Always remember that the bitwise operators have higher precedence than arithmetic operators, so you might need parentheses to correctly express your intentions.
- Not handling negative numbers: When working with signed integers, be aware that the Unary Complement Operator will flip the sign of a number if used on it directly. To handle this, use the two's complement representation and add 1 after applying the operator.
- Confusing the Unary Complement Operator with other operators: Be mindful not to confuse the Unary Complement Operator with the Negation Operator (-) or other bitwise operators like AND (&), OR (|), and XOR (^).
- Not using the correct data type: Use
unsigned intwhen working with the Unary Complement Operator, as it ensures that the operator works correctly without unexpected results due to sign extension in signed integers.
Practice Questions
- Write a C program that checks if a number is odd or even using the Unary Complement Operator.
- Write a program that swaps two numbers without using temporary variables. Use the Bitwise NOT and XOR operators.
- Implement a function in C that calculates the absolute value of an integer using only bitwise operations.
- Write a program that finds the maximum number among three given numbers using the Unary Complement Operator and other bitwise operators.
- Implement a function to check if a given number is a power of 2 using the Unary Complement Operator and bitwise AND (&) operator.
- Write a C program that checks if a given number is prime using only the Unary Complement Operator, bitwise AND (&), and bitwise OR (|).
- Implement a function to find the least common ancestor of two nodes in a binary tree using the Unary Complement Operator and other bitwise operators.
- Write a program that compares two strings lexicographically using only the Unary Complement Operator, bitwise AND (&), and bitwise XOR (^).
- Implement a function to find the number of set bits in an integer using the Unary Complement Operator and bitwise AND (&) operator.
- Write a C program that sorts an array of integers using only the Unary Complement Operator, bitwise OR (|), and bitwise XOR (^).
FAQ
What is the Unary Complement Operator in C?
The Unary Complement Operator in C is represented by the ~ symbol. It operates on a single operand, flipping each bit from 0 to 1 or vice versa.
Why can't we use the Unary Complement Operator with signed integers directly?
Signed integers use two's complement representation, and applying the Bitwise NOT operator will give you the one's complement value (flipping all bits). However, adding 1 to this result will give us the original value in two's complement representation. To handle this, you can convert the signed integer to its unsigned counterpart before applying the Unary Complement Operator and then add 1 if necessary.
How do we check if a number is odd or even using the Unary Complement Operator?
We can check if a number is odd or even by checking if the least significant bit (LSB) of a number is set or not. Since the LSB corresponds to the 0th bit in binary representation, we can use the expression num & 1 to determine if a number is odd or even. If the result is non-zero, the number is odd; otherwise, it's even.
Can we swap two numbers without temporary variables using the Unary Complement Operator and XOR operator?
Yes, we can swap two numbers without using temporary variables by utilizing the XOR operator in combination with the Unary Complement Operator. The following code demonstrates this:
void swap(unsigned int *a, unsigned int *b) {
*a = (*a) ^ (*b);
*b = (*a) ^ (*b);
*a = (*a) ^ (*b);
}
This code swaps the values stored in *a and *b without using any temporary variables.