Bitwise NOT Operator in C Language

In C language, the bitwise NOT operation is represented by the tilde symbol (~). This operator flips each binary bit of an integer, changing 0s to 1s and 1s to 0s. Here’s an example of using the bitwise NOT operator:

#include <stdio.h> int main() { int num = 10; // Binary representation: 0000 1010 int result = ~num; // Bitwise NOT operation printf("The result after bitwise NOT is: %d\n", result); // Output will be -11, binary representation: 1111 0101 return 0; }

In this example, we define an integer variable num with a binary representation of 0000 1010. We then apply the bitwise NOT operator ~ on num, storing the resulting value in the variable result. Finally, we use the printf function to output the negated result, which is -11. It’s important to note that applying a bitwise NOT also flips the sign bit of integers. Therefore, while num was originally positive in its binary form, its negated outcome becomes negative. The resulting value represents two's complement and can be converted back to find its original magnitude. In summary, in C language, you use the tilde symbol (~) for performing a bitwise NOT operation on each binary digit of an integer.

Leave a Reply

Your email address will not be published. Required fields are marked *