6.10 – Bitwise operators – negation operator

by subbu on September 30, 2013

Bitwise operators in C language

So far, we have used different data types to select memory of different sizes in bytes to store data of different types and ranges. The minimum size of memory on which we have worked so far is 1 byte that is of char type.

Need of bitwise operators

Some times when we develop system level or embedded applications, we need to work on individual bits. Say for example, some times we need to know the bit value at a required position that is whether 1 or 0 (on/off), some times we need to off (0) a bit at a required position, some times we need to toggle a bit at a position that is placing 1 in place of 0 or 0 in place of 1 and some times we need to extract decimal equivalent of bits from one position to another position etc.

These are mostly used to develop CPU intensive applications but not i/o based applications

Above operations are generally done while

  • Implementing encryption and decryption algorithms for data security,
  • Controlling and interacting with devices through a program,
  • Writing network protocols,
  • Optimizing speed and space,
  • Setting and changing the attributes of a file etc.

Some of these applications will be discoursed in the coming sessions

To perform operations at bit level, we need to first understand bitwise operators. C language has following bitwise operators

Operator Meaning
~ 1’s compliment
<< Left shift
>> Right shift
& Bitwise AND
| Bitwise inclusive OR
^ Bitwise exclusive OR (XOR)

Note: Bitwise operators only work on char and integer types but not with any other types

1’s compliment operator ~ (tilde)

It is a bitwise unary operator changes 1’s as 0’s and 0’s as 1’s in the memory on which we use the operator. Let us go through an example to understand what happens when we use 1’s compliment on the data.

char ch=65;

When we assign 65 to one byte of memory (char) then it’s equivalent binary value 01000001 is stored in the memory

printf("%d",~ch);

The output of ~ch that is the 1’s complement of 65 would be printed as -66. Now we will see the inner process of 1’s compliment and how the 1’s compliment of 65 is -66

negation operator

1’s compliment to 65 would be 10111110. While printing 1’s compliment, the runtime treats it as a –ve value because the first bit of 1’s complement is 1.

Hence the printf() reverts 2’s compliment, 1’s compliment and prints equivalent decimal number with –ve sign. For detailed explanation, refer 3.4 Integer Types in C language – Part2

#include <stdio.h>
int main()
{
 char ch=65;
 printf("%d",~ch);
 return 0;
}

Output
-66

Again the 1’s complement of -66 would be 65

#include <stdio.h>
int main()
{
 char ch=-66;
 printf("%d",~ch);
 return 0;
}

Output:
65

Where do we use ~ operator:

It is generally used while implementing encryption and decryption algorithms. Say for example, the data to be send through the network is 65; we send its 1’s compliment -66 through the network called encrypted data. At the receiving end again 1’s complement of -66 is found, that would be the original data 65 called decrypted data

Any body who tapes the data, receives -66 which is different from the original one

Encryption and decryption

Previous post:

Next post: