C - ~ (complement) and ! (not) operators

Introduction

Two infrequent binary operators are the ~ (or 1's complement) and the ! (or NOT).

The 1's complement operator flips all the bits in a value, turning a 1 into a 0 and a 0 into a 1. For example:

~01010011 = 10101100 

The ! (NOT) operator affects the entire value - all the bits. It changes any nonzero value to 0, and the value 0 to 1:

!01010011 = 00000000 
!00000000 = 00000001 

Zero and 1 are the only two results possible when using the bitwise ! operator.

Both the ~ and ! operators are unary operators.

Related Topic